D. Gourmet choice-Codeforces Round #541 (div2)--(tarjan或并查集+拓扑排序)

题目链接:传送门

D. Gourmet choice

description

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个数两两之间之间的大小关系,求满足最大值最小的两个序列

解法一:并查集+拓扑排序

先用并查集把相同大小的数缩成一个点,这样图中的路径表示的关系只有大小关系,没有等于关系,然后在新的图上拓扑排序就能解决。

附代码:

#include<bits/stdc++.h>

using namespace std;
const int maxn=1005;

int n,m,ru[2*maxn],len=0,ans[2*maxn],fa[2*maxn],tot=0;
char s[maxn][maxn];
bool check[maxn][maxn];
vector<int> vec[2*maxn];

int fin(int cur)
{
	return fa[cur]==cur?cur:fin(fa[cur]);
}

bool toposort()
{
	queue<int> que;
	for(int i=1;i<=n+m;i++) if(!ru[i]&&fin(i)==i) {
		que.push(i);
		ans[i]=1;
	}
	while(!que.empty()){
		int cur=que.front();que.pop();
		len++;
		for(int i=0;i<vec[cur].size();i++){
			ru[vec[cur][i]]--;
			if(!ru[vec[cur][i]]) {
				que.push(vec[cur][i]);
				ans[vec[cur][i]]=max(ans[vec[cur][i]],ans[cur]+1);
			}
		}
	}
	return len==tot;
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n+m;i++) fa[i]=i;
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=m;j++){
			if(s[i][j]=='='){
				fa[fin(n+j)]=fa[fin(i)];
			}
		}
	} 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='>'){
				vec[fin(n+j)].push_back(fin(i));
				ru[fin(i)]++;
			}else if(s[i][j]=='<'){
				vec[fin(i)].push_back(fin(n+j));
				ru[fin(n+j)]++;
			}
		}
	}
	for(int i=1;i<=m+n;i++) if(fin(i)==i) tot++;
	
	if(!toposort()) printf("No\n");
	else {
		printf("Yes\n");
		for(int i=1;i<=n;i++) printf("%d%c",ans[fin(i)],i==n?'\n':' ');
		for(int i=n+1;i<=n+m;i++) printf("%d%c",ans[fin(i)],i==n+m?'\n':' ');
	}
}

解法二:tarjan+拓扑排序

其实和上面用并查集的思想差不多,都是把相等的数缩成一个点

附代码:

#include<bits/stdc++.h>

using namespace std;
const int maxn=2005;

int n,m,dfn[maxn],head[maxn],low[maxn],num=0,dfs_time=0,sta[maxn],tot,index=0,belong[maxn],ru[maxn],ans[maxn];
char s[maxn][maxn];
bool vis[maxn],equ[maxn][maxn];

vector<int> vec[maxn];

struct node{
	int to,nxt;
}edge[2000005];

void add_edge(int u,int v)
{
	edge[++num].to=v;
	edge[num].nxt=head[u];
	head[u]=num;
}

void tarjan(int cur)
{
	vis[cur]=true;
	sta[++tot]=cur;
	dfn[cur]=low[cur]=++dfs_time;
	for(int i=head[cur];i;i=edge[i].nxt){
		if(!dfn[edge[i].to]){
			tarjan(edge[i].to);
			low[cur]=min(low[cur],low[edge[i].to]);
		}else if(vis[edge[i].to]) low[cur]=min(low[cur],dfn[edge[i].to]);
	}
	if(dfn[cur]==low[cur]){
		index++;
		while(sta[tot]!=cur){
			belong[sta[tot]]=index;
			vis[sta[tot]]=false;
			tot--;
		}
		vis[sta[tot]]=false;belong[cur]=index;
		tot--;
	}
}

bool toposort()
{
	queue<int> que;int res=0;
	for(int i=1;i<=index;i++) if(!ru[i]){
		que.push(i);
		ans[i]=1;
	}
	while(!que.empty()){
		int cur=que.front();que.pop();
		res++;
		for(int i=0;i<vec[cur].size();i++){
			ru[vec[cur][i]]--;
			if(!ru[vec[cur][i]]){
				que.push(vec[cur][i]);
				ans[vec[cur][i]]=max(ans[vec[cur][i]],ans[cur]+1);
			}
		}
	}
	return res==index;
}

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=m;j++){
			if(s[i][j]=='>') add_edge(n+j,i);
			else if(s[i][j]=='<') add_edge(i,n+j);
			else {
				add_edge(i,n+j);
				add_edge(n+j,i); 
				equ[i][n+j]=equ[n+j][i]=true;
			}
		}
	} 
	for(int i=1;i<=n+m;i++) if(!dfn[i]) tarjan(i);
	
	for(int i=1;i<=n+m;i++){
		for(int j=head[i];j;j=edge[j].nxt){
			if(!equ[i][edge[j].to]){
				vec[belong[i]].push_back(belong[edge[j].to]);
				ru[belong[edge[j].to]]++;
			}
		}
	}
	
	if(!toposort()) printf("No\n");
	else{
		printf("Yes\n");
		for(int i=1;i<=n;i++) printf("%d%c",ans[belong[i]],i==n?'\n':' ');
		for(int i=n+1;i<=n+m;i++) printf("%d%c",ans[belong[i]],i==n+m?'\n':' ');
	}
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值