您的位置:首页 > 其它

hdu 4324 Triangle LOVE 拓扑排序 多校联合赛(三)第四题

2012-08-01 19:35 204 查看
拓扑排序,如果中间遇见环了就NO啦!if A don’t love B, then B must love A,说明两点之间必有连线,而且And there is no possibility that
two people love each other, what a crazy world!所以只要遇见环,一定能换成三角关系

#include<iostream>
#include<cstdio>
using namespace std;
#define N 2010
int indegree
,a

;
char b
;
int main(){
//    freopen("in.txt","r",stdin);
int n,t,x,p=1;
scanf("%d",&t);
while(t--){
int sign1=0;
scanf("%d",&n);
for(int i=0;i<=n;i++){
indegree[i]=0;
}
for(int i=1;i<=n;i++){
scanf("%s",b);
for(int j=0;j<n;j++){
a[i][j+1]=b[j]-'0';
if(a[i][j+1]==1)
indegree[j+1]++;//入度数组
}
}
printf("Case #%d: ",p++);
for(int i=1;i<=n;i++){
int temp;
int sign=0;
for(int j=1;j<=n;j++){
if(indegree[j]==0){
temp=j;
indegree[j]=-1;
sign=1;
break;
}
}
if(sign==0){//如果没有找到定点,那一定有环了
printf("Yes\n");
sign1=1;
break;
}
else{
for(int j=1;j<=n;j++){
if(a[temp][j]&&temp!=j)
indegree[j]--;
}
}
}
if(sign1==0){
printf("No\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: