D. Gourmet choice

time limit per test

2 seconds

memory limit per test

256 megabytes

 
题目链接:

Discribe

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 aij is equal to ">", in the opposite case aij is equal to "<". Dishes also may be equally good, in this case aij

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 aij

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 aij is ">", then it should be greater, and finally if aij

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 (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 aij

. 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
Input
3 2
==
=<
==
Output
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 2

, 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个点,告诉你第一个集合里的所有点的权值和第二个集合里的所有点的权值的大小关系。

问是否能给每个点赋一个值,使得满足题目所给的大小关系,如果可以,输出任意一组合法解。

题解:

对于任意两个点ai和bj,如果ai<bj则,连一条从j到i的单向边,问题即为每个点的最长路。

如果相等,我们缩点即可。如果有环则说明无解。

具体步骤:

先缩点,再判环,我直接tarjan求环,然后再在DAG上dfs求最长路即可。

Debug:

1.一开始先不管相等的边,判环再缩点,导致可能缩点之后又有新的环产生。

2.一看n<1000,平时做多了n和边是一个数量级的题目,于是边只开了10000,还以为够了,真是惯性思维惹的祸,还是太菜了。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define N 2000050
char road[][];
int n,m,fa[N],f[N],ans[N];
bool flag=;
struct Edge{int from,to,s;}edges[N<<],edges2[N<<];
int tot,last[N];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if(c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while((c=getchar())!='<'&&c!='>'&&c!='='&&c!=EOF);}
int gf(int x)
{
if (x==fa[x])return x;
fa[x]=gf(fa[x]);
return fa[x];
}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
int low[N],dfn[N],sk[N],at_sk[N],top,Time;
void tarjan(int x)
{
dfn[x]=++Time;
low[x]=Time;
sk[++top]=x;
at_sk[x]=;
f[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (gf(x)==gf(e.to))flag=true;
if (!f[e.to])tarjan(e.to);
if (at_sk[e.to])low[x]=min(low[x],low[e.to]);
}
if (sk[top]!=x)flag=true;
if (low[x]==dfn[x])
{
while(sk[top+]!=x&&top)
at_sk[sk[top--]]=;
}
}
void dfs(int x)
{
f[x]=;
ans[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if(!ans[e.to]&&!f[e.to])dfs(e.to);
ans[x]=max(ans[x],ans[e.to]);
}
ans[x]++;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
read(n);read(m);
char c;
for(int i=;i<=n+m+n;i++)fa[i]=i;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
read_char(c);
//if (c=='<')AddEdge(j+n,i);
//if (c=='>')AddEdge(i,j+n);
road[i][j]=c;
if (c=='=')fa[gf(i)]=gf(j+n);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if (road[i][j]=='<')AddEdge(gf(j+n),gf(i));
else if (road[i][j]=='>')AddEdge(gf(i),gf(j+n)); for(int i=;i<=n+m;i++) if(!f[gf(i)]&&!flag)tarjan(gf(i));
if (flag)
{
printf("No");
return ;
}
printf("Yes\n");
memset(f,,sizeof(f));
for(int i=;i<=n+m;i++) if (!f[gf(i)]) dfs(gf(i));
for(int i=;i<=n;i++) printf("%d ",ans[gf(i)]);
putchar('\n');
for(int i=;i<=m;i++) printf("%d ",ans[gf(i+n)]);
}

coderfoces D. Gourmet choice的更多相关文章

  1. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  2. D. Gourmet choice并查集,拓扑结构

    D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

  4. CF1131D Gourmet choice

    题目链接 题意 有两组菜,第一组有\(n\)种,第二组有\(m\)种.给出一个\(n\times m\)的矩阵,第\(i\)行第\(j\)列表示第一组中的第\(i\)种菜与第二组中的第\(j\)种菜好 ...

  5. CF1131D Gourmet choice(并查集,拓扑排序)

    这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...

  6. 【CF #541 D】 Gourmet choice

    link:https://codeforces.com/contest/1131 题意: 给定一些大小比较,输出排名. 思路: 这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集. 结束后这道 ...

  7. CF - 1131 D Gourmet choice

    题目传送门 先把 = 的人用并查集合并在一起. 然后 < > 的建边, 跑一遍 toposort 之后就好了. 入度为0点的值肯定为1, 然后就是因为这个是按照时间线走过来的,所以一个点的 ...

  8. CF#541 D. Gourmet choice /// BFS 拓扑

    题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...

  9. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

随机推荐

  1. Innobackupex MySQL 全备、增备及恢复

    简介: 在这之前都是通过 mysqldump 来备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易. 凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗 ...

  2. Mysql 中的伪列用法

    SELECT ( @rowNO := @rowNo + 1 ) AS rowno, A.*FROM ( SELECT * FROM t_user ) a, ( SELECT @rowNO := 0 ) ...

  3. 如何设置tomcat,直接通过IP 访问

    找到tomcat的主目录,进入conf文件夹,找到server.xml文件,并打开:   修改tomcat的监听端口为80端口: 在server.xml文件中找到: <Connector por ...

  4. loadView 和 viewDidLoad、viewDidunload 的区别

    loadView 和 viewDidLoad 是 iPhone 开发中肯定要用到的两个方法. 他们都可以用来在视图载入的时候初始化一些内容. 但是他们有什么区别呢? viewDidLoad 方法只有当 ...

  5. Linux实战教学笔记20:初级阶段结束,中级阶段起航

    第二十节 第一阶段结束第二阶段起航 标签(空格分隔): Linux实战教学笔记-陈思齐 一,承上 Linux实战教学笔记的基础核心能力阶段也就是第一阶段到此也就告一段落了.如果同学们能基本全都掌握,再 ...

  6. Android 4.x 获取存储卡路径的方式

    http://blog.sina.com.cn/s/blog_8daaa9ea0101mx7f.html     以前的Android(4.1之前的版本)中,SDcard跟路径通过“/sdcard”或 ...

  7. built-in SpecularType of Unity

    [built-in SpecularType of Unity] 1.声明变量. 注意并没有在Shader中声明_SpecColor,因为Lighting.cginc中已经帮我们声明. 2.声明使用B ...

  8. [hdu4347]The Closest M Points(线段树形式kd-tree)

    解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...

  9. 钉钉开发笔记(5)android系统中html软键盘的适配

    最近项目中发现个别Android手机中存在弹出的软键盘会遮挡输入框的现象,最后自己写了一个方法(如下),问题基本解决. 记录下来,防止忘记.有什么不对的地方欢迎指正.O(∩_∩)O 1 //键盘适配 ...

  10. 云计算 Restfull API 设计之旅

    http://fedoraproject.org/wiki/Cloud_APIs_REST_Style_Guide#Introduction_to_REST   http://docs.spring. ...