拓扑排序,如果中间遇见环了就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[N],a[N][N];
char b[N];
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;
}