![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int N=1010;
6 int f[N],n;
7
8 int Find(int x)
9 {
10 return (x==f[x])?x:(f[x]=Find(f[x]));
11 }
12
13
14 int main()
15 {
16 freopen("bs.in","r",stdin);
17 int t;
18 scanf("%d",&t);
19 while (t--) {
20 scanf("%d",&n);
21 bool flag=1;
22 for (int i=0;i<n;i++) f[i]=i;
23 for (int i=0;i<n;i++) {
24 for (int j=0;j<n;j++) {
25 int k;
26 scanf("%d",&k);
27 if (i==j&&k%2==0) {
28 flag=0;
29 }
30 int x=Find(i),y=Find(j);
31 if (k&&x!=y) f[x]=y;
32 }
33 }
34 if (!flag) {
35 printf("No ");
36 continue;
37 }
38 for (int i=1;i<n;i++) {
39 if (Find(i)!=Find(0)) {
40 flag=0;
41 break;
42 }
43 }
44 if (flag) printf("Yes ");
45 else printf("No ");
46 }
47 return 0;
48 }
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int N=1010;
6 int f[N],n;
7
8 int Find(int x)
9 {
10 return (x==f[x])?x:(f[x]=Find(f[x]));
11 }
12
13
14 int main()
15 {
16 freopen("bs.in","r",stdin);
17 int t;
18 scanf("%d",&t);
19 while (t--) {
20 scanf("%d",&n);
21 bool flag=1;
22 for (int i=0;i<n;i++) f[i]=i;
23 for (int i=0;i<n;i++) {
24 for (int j=0;j<n;j++) {
25 int k;
26 scanf("%d",&k);
27 if (i==j&&k%2==0) {
28 flag=0;
29 }
30 int x=Find(i),y=Find(j);
31 if (k&&x!=y) f[x]=y;
32 }
33 }
34 if (!flag) {
35 printf("No ");
36 continue;
37 }
38 for (int i=1;i<n;i++) {
39 if (Find(i)!=Find(0)) {
40 flag=0;
41 break;
42 }
43 }
44 if (flag) printf("Yes ");
45 else printf("No ");
46 }
47 return 0;
48 }