1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int head[2001], in[2001], tote;
struct EDGE{
int to, next;
}edge[2001];
void addedge(int from, int to)
{
edge[ ++tote]. to = to;
edge[tote]. next = head[from];
head[from] = tote;
} /*
init:
memset(head, 0, sizeof(head))
tote = 0;
*/
int main()
{
int ti;
scanf("%d",&ti);
for(int it = 1;it <= ti;it++)
{
int n;
scanf("%d",&n);
queue<int> q;
for(int i = 1;i <= n;i++)
{
char str[20000];
getchar();
scanf("%s", str);
for(int j = 0;j < n;j++)
{
if(str[j] == '1')
{
in[j + 1] ++;
addedge(i, j + 1);
}
}
}
for(int i = 1;i <= n;i++)
{
if(in[i] == 0)
q.push(i);
}
int cnt = 0;
while(!q.empty())
{
cnt++;
int k = q.front();
q.pop();
for(int i = head[k];i != 0;i = edge[i]. next)
{
in[edge[i]. to] --;
if(in[edge[i]. to] == 0)
q.push(edge[i]. to);
}
}
if(cnt == n)
{
printf("Case #%d: Yes",it);
}
else
printf("Case #%d: No",it);
memset(head, 0, sizeof(head));
memset(in, 0, sizeof(in));
memset(edge, 0, sizeof(edge));
tote = 0;
}
return 0;
}
|