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 nlines 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
美食家苹果在对一家餐厅进行评价时,有特殊的技巧。他在第一天去这个餐厅点N个菜,过几天再点M个,这两套中没有重复的菜品。然后他列出一个NxM的表格,表格中 aij表示第一次的第i道菜与第二次的第j道菜相比的感觉,只有 <(j比i好) >(i比j好) 和 =(i和j一样) 。
现在要根据这个表给每道菜一个具体的数值,要求最小值尽可能的小;如果不能给出具体数值,输出No。
因为首先要判断菜品之间关系是否有冲突,所以一开始是想用带权并查集来推算任意两点之间的关系。但是有时根据现有信息,不能确定两点之间关系,这样无法确定边的权值。
同样在这个图中我们也能发现,如果一组有向边组成了环,那么就会出现矛盾。
然后每个节点的入度表示有多少个节点比他大。这样可以一次拓扑排序得出每个节点的大小关系,再由这个关系赋值就得到答案了。
此外,还要注意=关系的处理。=关系表示这两道菜的评分一样,反映到图中就是这两个点的连接关系完全相同,所以我们可以用并查集来压缩相等的点。
#include <stdio.h>
#include <climits>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define STAR N
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Record{
int index;
int num;
};
int deg[2010],level[2010];
int par[2010];
char store[1005][1005];
vector<int> adj[2010];
queue<Record> Q;
int seekRoot(int x);
int main(){
ios::sync_with_stdio(false);
int N,M;
cin>>N>>M;
rep(i,1,N+1)
rep(j,1,M+1)
cin>>store[i][j];
int all=N+M;
rep(i,1,N+M+1){
par[i]=i;
deg[i]=0;
level[i]=0;
}
rep(i,1,N+1)
rep(j,1,M+1){
if(store[i][j]=='='){
int num=j+STAR;
int rootA=seekRoot(num);
int rootB=seekRoot(i);
if(rootA==rootB){
;
}else{
par[rootA]=rootB;
all--;
}
}
}
rep(i,1,N+1){
rep(j,1,M+1){
int rootA=seekRoot(i);
int rootB=seekRoot(j+STAR);
switch(store[i][j]){
case '<':
adj[rootB].Push(rootA);
deg[rootA]++;
break;
case '>':
adj[rootA].Push(rootB);
deg[rootB]++;
break;
default:
break;
}
}
}
//push in queue
rep(i,1,N+M+1){
if(par[i]==i && deg[i]==0)
Q.push((Record){i,N+M+1});
}
int use=0;
while(!Q.empty()){
Record &Re=Q.front();
int v=Re.index;
level[v]=Re.num;
use++;
rep(i,0,getLen(adj,v)){
int u=adj[v][i];
deg[u]--;
if(deg[u]==0){
Q.push((Record){u,Re.num-1});
}
}
Q.pop();
}
//finf arrange
bool can=true;
rep(i,1,N+M+1){
if(deg[i]!=0){
can=false;
break;
}
}
if(can){
cout<<"Yes"<<endl;
int minn=INT_MAX;
rep(i,1,N+M+1){
int root=seekRoot(i);
minn=min(minn,level[root]);
}
minn--;
rep(i,1,N+1){
int root=seekRoot(i);
cout<<level[root]-minn<<" ";
}
cout<<endl;
rep(i,N+1,M+1+N){
int root=seekRoot(i);
cout<<level[root]-minn<<" ";
}
cout<<endl;
}else{
cout<<"No"<<endl;
}
re 0;
}
int seekRoot(int x){
if(x==par[x])
re x;
else{
int r=seekRoot(par[x]);
re par[x]=r;
}
}