http://codeforces.com/contest/1131/problem/D
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.
题意:第一天有n个菜,第二天有m个菜,給一个n*m矩阵,含有>=<,a(i,j)指的是:第一天的第i件菜的美味度>=<第二天的第j件菜的美味度。求出满足这个矩阵的n+m件菜的各自的美味度,要求使用的最大数字尽量小。
思路:看成n+m个点,先用并查集把相等的点并起来,然后如果是<,i向n+j点连一条边,如果是>,n+j点向i连一条边,进行拓扑排序,有解等价于存在拓扑序,无解等价于有环。
使用的最大数字尽量小这个条件确保了答案的唯一性,因为这个图比较特殊,在纸上尝试找反例可以大致理解。
使用的数字按照拓扑排序bfs遍历的顺序依次递增。
以后写拓扑排序都要bfs吧,dfs的使用范围貌似只有单纯的最朴素的拓扑排序可以,要求字典序或者这道题这样的就不行了,bfs的适用范围很广泛。
这题注意拓扑排序第一次入队时一个集合里除代表元以外的其他点无边相连,入度也为0,但是这些点不应该入队,只有是集合代表元并且入度为0的点才要入队。
注意清楚什么时候用i,什么时候用i所在集合的代表元。
#include<bits/stdc++.h>
using namespace std;
#define maxn 2000+100
int n,m,p[maxn],ans[maxn];
vector<int> G[maxn];
char str[1010][1010];
vector<int> topo;
int in[maxn];
void AddEdge(int i,int j)
{
G[i].push_back(j);
in[j]++;
}
int findset(int x){return x==p[x]?x:p[x]=findset(p[x]);}
bool toposort()
{
queue<int> Q;
for(int i=1;i<=n+m;i++)if(i==findset(i)&&in[findset(i)]==0)
Q.push(findset(i)),ans[findset(i)]=1;
while(!Q.empty())
{
int u=Q.front();Q.pop();
topo.push_back(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
in[v]--;
if(in[v]==0)Q.push(v),ans[v]=ans[u]+1;
}
}
for(int i=1;i<=n+m;i++)if(!ans[findset(i)])return false;
return true;
}
int main()
{
// freopen("input.in","r",stdin);
cin>>n>>m;//the first day marked 1~n ,the second day marked n+1~n+m
for(int i=1;i<=n+m;i++)p[i]=i;
for(int i=1;i<=n;i++)
{
scanf("%s",str[i]+1);
for(int j=1;j<=m;j++)
{
if(str[i][j]=='=')
{
int x=findset(i),y=findset(n+j);
p[x]=y;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(str[i][j]=='<')AddEdge(findset(i),findset(n+j));
else if(str[i][j]=='>')AddEdge(findset(n+j),findset(i));
}
}
if(!toposort())
{
puts("No");
}
else
{
puts("Yes");
for(int i=1;i<=n;i++)printf("%d ",ans[findset(i)]);
puts("");
for(int i=n+1;i<=n+m;i++)printf("%d ",ans[findset(i)]);
puts("");
}
return 0;
}