CodeForces - 1131 D Gourmet choice【拓扑排序】

一位美食评论家在尝试了过多的菜品后,面临着如何公正且严谨地评价每一道菜的难题。通过使用并查集和拓扑排序算法,解决了一个n*m的关系图问题,确保了每道菜的评分既符合评论家的感受,又尽可能地使用小的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 jjfrom 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的关系图,判断是否存在对应值使其成立。

 

用并查集处理‘=’符号,之后直接拓扑排序即可,判断一下是否成立。

#include "bits/stdc++.h"
using namespace std;
char a[1004][1004];
int fa[2004];
int ans[2004];
set<int>las[2004];//大于他的点
set<int>pre[2004];//小于他的点  size为0时入队
vector<int>v[2004];//记录相等的元素
queue<int>q;
int Find(int a)
{
    if(a==fa[a])return a;
    else return fa[a]=Find(fa[a]);
}
int sum;
void dfs(int u,int cnt)//将所有与u相等的点赋值
{
    ans[u]=cnt;
    sum++;
    for (int i = 0; i < v[u].size(); ++i) {
        dfs(v[u][i],cnt);
    }
}
int main()
{
    int n,m;
    sum=0;
    scanf("%d%d\n",&n,&m);
    for (int i = 0; i < n; ++i) {
        scanf("%s",a[i]);
    }
    for (int i = 0; i < n+m; ++i) {
        fa[i]=i;
    }
    for (int i = 0; i < n; ++i) {//处理等于符号
        for (int j = 0; j < m; ++j) {
            if(a[i][j]=='=')
            {
                int fx=Find(i),fy=Find(n+j);
                if(fx!=fy){
                    fa[fx]=fy;
                    v[fy].push_back(fx);
                }
            }
        }
    }
    int ok=1;
    for (int i = 0; i < n&&ok; ++i) {//建边
        for (int j = 0; j < m&&ok; ++j) {
            if(a[i][j]=='<')
            {
                int fx=Find(i),fy=Find(n+j);
                if(fx==fy)ok=0;
                las[fx].insert(fy);
                pre[fy].insert(fx);
            }
            if(a[i][j]=='>')
            {
                int fx=Find(i),fy=Find(n+j);
                if(fx==fy)ok=0;
                pre[fx].insert(fy);
                las[fy].insert(fx);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        if(fa[i]!=i)continue;
        if(pre[i].size()==0)q.push(i);
    }
    for (int i = n; i < n+m; ++i) {
        if(fa[i]!=i)continue;
        if(pre[i].size()==0)q.push(i);
    }
    int cnt=1;
    while(!q.empty())
    {
        int x=q.size();
        while(x--)//当前队列中的点值为cnt,新入队的不能操作
        {
            int t =q.front();
            q.pop();
            dfs(t,cnt);
            while(las[t].size())
            {
                int id=Find(*las[t].begin());
                pre[id].erase(t);
                if(pre[id].size()==0)
                {
                    q.push(id);
                }
                las[t].erase(las[t].begin());
            }
        }
        cnt++;
    }
    if(ok&&sum==n+m)
    {
        puts("Yes");
        for (int i = 0; i < n-1; ++i) {
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[n-1]);
        for (int i = n; i < m+n-1; ++i) {
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[m+n-1]);
    }
    else puts("No");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值