codeforces1131D. Gourmet choice(并查集)

版权声明:Amove? https://blog.csdn.net/Amovement/article/details/87904730

                                                   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 (1≤n,m≤10001≤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 mmintegers — 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.

一、原题地址

点我传送

二、大致题意

第一天有n个蛋糕,第二天有m个蛋糕,给出一个n*m的矩阵,表示第一天的第i个蛋糕比第二天的第j个蛋糕(美味程度)大或者小或者等于。

若存在这样的矩阵情况,则输出美味程度最大值最小的方案。

三、大致思路

对于相等的蛋糕,我们采取并查集将他们看作是一个点,其他的点建图,记录每个点的度数,剩下的就是很简单的寻找最深的树。

四、代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;



int father[2005],ind[2005],dp[2005],n,m;
char mmp[1005][1005];
vector<int>e[2005];
bool use[2005],vis[2005];

int find(int x)
{
    if(father[x] == x) return x;
    else return father[x] =find(father[x]);
}

void Union(int x,int y)
{
    int rx,ry;
    rx = find(x);
    ry = find(y);
    father[rx] = ry;
}

bool flag;

void DFS(int nx,int dep)
{
    if(flag)return ;
    dp[nx]=max(dp[nx],dep);
    for(int i=0;i<e[nx].size();i++)
    {
        int to =e[nx][i];
        if(vis[to])
        {
            flag=true;return ;
        }
        if(dep+1>dp[to])
        {
            vis[to]=true;
            DFS(to,dep+1);
            vis[to]=false;
        }
    }
    return ;
}

int main()
{
    scanf("%d %d",&n,&m);
    flag=false;
    for(int i=1;i<=n+m;i++)father[i]=i,dp[i]=ind[i]=0,vis[i]=use[i]=false;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",mmp[i]+1);
        for(int j=1;j<=m;j++)
        {
            if(mmp[i][j]=='=')Union(i,n+j);
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int fx=find(i),fy=find(n+j);
            if(mmp[i][j]=='<'){e[fx].push_back(fy);ind[fy]++;use[fx]=true;}
            else if(mmp[i][j]=='>'){e[fy].push_back(fx);ind[fx]++;use[fy]=true;}
            else use[fx]=true;
        }
    }

    bool tag=false;
    for(int i=1;i<=n+m;i++)
    {
        if(use[i]&&ind[i]==0)
        {
            tag=true;
            vis[i]=true;
            DFS(i,1);
            vis[i]=false;
        }
    }
    if(!tag||flag)printf("No\n");
    else
    {
        printf("Yes\n");
        for(int i=1;i<=n;i++)printf("%d ",dp[find(i)]);
        printf("\n");
        for(int i=1+n;i<=n+m;i++)printf("%d ",dp[find(i)]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Amovement/article/details/87904730