博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces #541 D. Gourmet choice(拓扑+并查集)
阅读量:4668 次
发布时间:2019-06-09

本文共 4403 字,大约阅读时间需要 14 分钟。

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 mmintegers — evaluations of dishes from the second set.

Examples
input
3 4>>>>>>>>>>>>
output
Yes2 2 2 1 1 1 1
input
3 3>>><<<>>>
output
Yes3 1 3 2 2 2
input
3 2===<==
output
No
 
题意:

第一天有n个菜,第二天有m个菜。

给一个n*m矩阵,含有(或=、<),a(i,j)指的是:第一天的第i件菜的美味度>(或=、<)第二天的第j件菜的美味度。求出满足这个矩阵的n+m件菜的各自的美味度,要求使用的最大数字尽量小。

思路:如果是等号就用并查集连在一起(缩点) 其他的就拓扑排序

#include 
#include
#include
#include
#include
#define ll long long int#define M 6using namespace std;inline ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){
return a/gcd(a,b)*b;}int moth[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};int dir[4][2]={
1,0 ,0,1 ,-1,0 ,0,-1};int dirs[8][2]={
1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};const int inf=0x3f3f3f3f;const ll mod=1e9+7;int n,m;int f[3000]; char G[1007][1007];vector
v[3000];int ru[3000];int ans[3000];bool flag;int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x];}void join(int x,int y){ int xx=find(x); int yy=find(y); if(xx!=yy){ f[yy]=xx; }}void toposort(){ queue
> q; for(int i=1;i<=n+m;i++) if(!ru[i]&&i==find(i)) q.push(make_pair(i,1)); while(!q.empty()){ int x=q.front().first; int y=q.front().second; q.pop(); ans[x]=y; int len=v[x].size(); for(int i=0;i
>n>>m; for(int i=1;i<=n+m;i++) f[i]=i; flag=1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ cin>>G[i][j]; if(G[i][j]=='=') //合并 join(i,j+n); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ if(G[i][j]=='=') continue; int xx=find(i); int yy=find(n+j); if(xx==yy){ flag=0; break; } if(G[i][j]=='>') v[yy].push_back(xx),ru[xx]++; //计算入度 if(G[i][j]=='<') v[xx].push_back(yy),ru[yy]++; if(!flag) break; } if(!flag) cout<<"No"<

 

转载于:https://www.cnblogs.com/wmj6/p/10439935.html

你可能感兴趣的文章
Convert DataTable to Html Table
查看>>
JavaEE复习三
查看>>
全局ajax事件
查看>>
javascript二维数组
查看>>
JavaScript 字符串属性和方法
查看>>
opencv新手注意
查看>>
Source InSight context 窗口丢失的解决办法
查看>>
cut point and bridge总结
查看>>
(5)Oracle基础--约束
查看>>
vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”...
查看>>
【Nginx】磁盘文件写入飞地发
查看>>
默认情况下安装的应用程序C盘后提示权限不足,当你开始介意。。。
查看>>
su root 后还是不能使用useradd ,useradd 等命令
查看>>
URL.createObjectURL图片预览
查看>>
js 中exec、test、match、search、replace、split用法
查看>>
Android开发笔记(一)手势识别
查看>>
mybatis 复习笔记03
查看>>
zoj 3703(背包)
查看>>
一种新的子波域滤波算法
查看>>
cookie之三天免登录代码
查看>>