D. Gourmet choice

题目链接:https://codeforces.com/problemset/problem/1131/D

D. Gourmet choice
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 nn dishes on the first day and a set of mm dishes on the second day. He made a table aa of size n×mn×m, in which he described his impressions. If, according to the expert, dish ii from the first set was better than dish jj from the second set, then aijaij is equal to ">", in the opposite case aijaij is equal to "<". Dishes also may be equally good, in this case aijaij 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 aijaij is "<", then the number assigned to dish ii from the first set should be less than the number of dish jj from the second set, if aijaij is ">", then it should be greater, and finally if aijaij 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 nn and mm (1n,m10001≤n,m≤1000) — the number of dishes in both days.

Each of the next nn lines contains a string of mm symbols. The jj-th symbol on ii-th line is aijaij. 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 nn integers — evaluations of dishes from the first set, and on the third line print mm integers — evaluations of dishes from the second set.

Examples
input
Copy
3 4
>>>>
>>>>
>>>>
output
Copy
Yes
2 2 2 
1 1 1 1 
input
Copy
3 3
>>>
<<<
>>>
output
Copy
Yes
3 1 3 
2 2 2 
input
Copy
3 2
==
=<
==
output
Copy
No
Note

In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be 22, for all dishes of the first day.

In the third sample, the table is contradictory — there is no possible evaluation of the dishes that satisfies it.

题目大意:有两个正整数序列a,b,长度分别为 n,m。给出所有 ai 和 bj(1in,1jm的大小关系(大于,小于或者等于),

请构造出符合条件的 a 和 b。如果无解,输出NO。如果有多个解,输出 a,b 中最大元素最小的方案。

思路:如果没有=符号的话,其实就是一道简单的拓扑排序,其实有=也是一样的,我们可以把所有=号的归为一个节点,一类的结点用一个节点来处理,

因为他们都是等价的,如果到最后没有得到N+M个结点,证明存在环,也就是输出No的情况

看代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<queue>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e8+7;
const int maxn=1e3+50;
char a[maxn][maxn];
vector<int>edge[maxn<<1];
int du[maxn<<1],fa[maxn<<1];
int ans[maxn<<1];
int flag=0;
int N,M;
int sum=0;
int Find(int x)
{
    if(fa[x]==x) return x;
    return fa[x]=Find(fa[x]);
}
void solve()
{
    queue<int>q;
    for(int i=1;i<=N+M;i++)
    {
        if(du[i]==0&&fa[i]==i)//入度为0 并且 它的父亲等于本身 代表这个点是一个集合的代表
        {
            sum++;//这个点之前没有算过 所以要算
            ans[i]=1;//以集合中的祖先代表这个集合
            q.push(i);
        }
    }

    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int i=0;i<edge[v].size();i++)//去掉当前节点
        {
            int to=edge[v][i];
            du[to]--;
            if(du[to]==0)
            {
                sum++;
                ans[to]=ans[v]+1;
                q.push(to);
            }
        }
    }
}
int main()
{
    scanf("%d%d",&N,&M);
    memset(ans,-1,sizeof(ans));
    for(int i=1;i<=N+M;i++) fa[i]=i;
    getchar();
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            scanf("%c",&a[i][j]);
            if(a[i][j]=='=')//先把所有一类的结点放到并查集中 并用一个节点代替所有结点进行操作
            {
                int x=Find(i);
                int y=Find(N+j);
                if(x==y) continue;//已经在同一个集合中 不需要操作
                fa[y]=x;
                sum++;//存总共有多少个结点有大小了
            }
        }
        getchar();
    }
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            if(a[i][j]=='=') continue;
            int x=Find(i);
            int y=Find(N+j);
            if(a[i][j]=='<')
            {
                edge[x].push_back(y);//同一类结点使用他们的代表结点来处理
                du[y]++;
            }
            else
            {
                edge[y].push_back(x);
                du[x]++;
            }
        }
    }
//    cout<<"sum:"<<sum<<endl;
    solve();
//    cout<<"*sum:"<<sum<<endl;
    if(sum!=N+M) printf("No
");
    else
    {
        printf("Yes
");
        for(int i=1;i<=N;i++) printf("%d ",ans[Find(i)]);printf("
");
        for(int i=N+1;i<=N+M;i++) printf("%d ",ans[Find(i)]);printf("
");
    }
    return 0;
}
/**
3 3
<<<
<<=
<<=
sum:2
**sum:3
v:1
v:6
*sum:4
*/
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/11624034.html