Codeforces-1131D:Gourmet choice(拓撲+並查集)

Mitsuha_發表於2019-02-24

D. Gourmet choice
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn’t like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer.

Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he’s confused about evaluating dishes.

The gourmet tasted a set of n dishes on the first day and a set of m dishes on the second day. He made a table a of size n×m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then aija_{ij} is equal to “>”, in the opposite case aij is equal to “<”. Dishes also may be equally good, in this case aija_{ij} is “=”.

Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if aija_{ij} is “<”, then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if aija_{ij} is “>”, then it should be greater, and finally if aija_{ij} is “=”, then the numbers should be the same.

Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible.

Input
The first line contains integers n and m (1n,m1000)(1≤n,m≤1000) — the number of dishes in both days.

Each of the next n lines contains a string of m symbols. The j-th symbol on i-th line is aija_{ij}. All strings consist only of “<”, “>” and “=”.

Output
The first line of output should contain “Yes”, if it’s possible to do a correct evaluation for all the dishes, or “No” otherwise.

If case an answer exist, on the second line print n integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set.

Examples
input
3 4
>>>>
>>>>
>>>>
output
Yes
2 2 2
1 1 1 1
input
3 3
>>>
<<<
>>>
output
Yes
3 1 3
2 2 2

思路:假設沒有=,那麼按小向大連有向邊,構成的圖若是一個DGA,則存在解,且最大值是這個DGA中最長的一條鏈。

那麼如果有=,把所有值相等的點縮成一個點,這樣不會影響建成的圖。

縮點可以用並查集,然後判斷圖是否是一個DGA,以及求DGA中的最長鏈可以用拓撲排序。

#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
const int MAX=2e3+10;
const double PI=acos(-1.0);
typedef long long ll;
vector<int>e[MAX];
char s[MAX][MAX];
int d[MAX];
int in[MAX];
int n,m;
int p[MAX];
int f(int x){return p[x]==x?x:p[x]=f(p[x]);}
int check()
{
    for(int i=1;i<=n+m;i++)p[i]=i;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)if(s[i][j]=='=')p[f(i)]=f(j+n);//縮點
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        if(s[i][j]=='=')continue;
        if(f(i)==f(j+n))return 0;//判斷是否矛盾
        if(s[i][j]=='<')e[f(i)].push_back(f(j+n));//小向大連邊
        if(s[i][j]=='>')e[f(j+n)].push_back(f(i));
    }
    for(int i=1;i<=n+m;i++)sort(e[i].begin(),e[i].end());
    for(int i=1;i<=n+m;i++)e[i].erase(unique(e[i].begin(),e[i].end()),e[i].end());
    for(int i=1;i<=n+m;i++)
    for(int j=0;j<e[i].size();j++)in[e[i][j]]++;
    queue<int>q;
    for(int i=1;i<=n+m;i++)if(in[i]==0)q.push(i),d[i]=1;//拓撲排序
    while(!q.empty())
    {
        int now=q.front();q.pop();
        for(int i=0;i<e[now].size();i++)
        {
            int nex=e[now][i];
            in[nex]--;
            d[nex]=max(d[nex],d[now]+1);
            if(in[nex]==0)q.push(nex);
        }
    }
    if(*max_element(in+1,in+n+m+1)>0)return 0;//存在環
    return 1;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
    if(check())
    {
        puts("Yes");
        for(int i=1;i<=n;i++)printf("%d ",d[f(i)]);cout<<endl;
        for(int i=1;i<=m;i++)printf("%d ",d[f(i+n)]);cout<<endl;
    }
    else puts("No");
    return 0;
}

相關文章