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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include<bits/stdc++.h> using namespace std;
#define LL long long #define PII pair<int,int> #define MP make_pair #define F first #define S second
int scc_num;
vector<int>graph[100010]; vector<int>rev_graph[100010]; vector<int>new_graph[100010];
int Map[1010]; int SCCMap[1010];
stack<int>stk;
bool visited[1010];
bool flag=true;
void forward_dfs(int node) { visited[node]=true;
for(int next:graph[node]) { if(visited[next]==false) forward_dfs(next); }
stk.push(node); }
void rev_dfs(int node) { visited[node]=true; SCCMap[node]=scc_num;
for(int next:rev_graph[node]) { if(visited[next]==false) rev_dfs(next); } }
void dfs_check(int node) { if(new_graph[node].size()>1) { flag=false; return; }
visited[node]=true; for(int next:new_graph[node]) { if(visited[next]==false) dfs_check(next); }
}
void init() { for(int i=0; i<1010; i++) { graph[i].clear(); rev_graph[i].clear(); new_graph[i].clear(); }
fill(visited,visited+1010,false);
memset(Map,-1,sizeof Map);
while(!stk.empty()) stk.pop();
}
int main() {
int tc; scanf("%d",&tc);
for(int i=1; i<=tc; i++) { init();
int n,serial=0; scanf("%d",&n);
Map[0]=serial++;
for(int j=1; j<=n; j++) { int wish; scanf("%d",&wish);
for(int k=1; k<=wish; k++) { int a,b; scanf("%d %d",&a,&b);
if(Map[a]==-1) Map[a]=serial++; if(Map[b]==-1) Map[b]=serial++;
int u=Map[a]; int v=Map[b];
graph[u].push_back(v); rev_graph[v].push_back(u);
} }
for(int k=0; k<serial; k++) { if(visited[k]==false) forward_dfs(k); }
fill(visited,visited+1010,false);
scc_num=0;
while(!stk.empty()) { int now=stk.top(); stk.pop();
if(visited[now]==false) { rev_dfs(now); scc_num++; } }
for(int now=0; now<serial; now++) { for(int next:graph[now]) { if(SCCMap[now]!=SCCMap[next]) new_graph[SCCMap[now]].push_back(SCCMap[next]); } }
fill(visited,visited+1010,false);
flag=true;
dfs_check(SCCMap[0]);
if(flag) { for(int now=0; now<scc_num; now++) { if(visited[now]==false) { flag=false; break; } } }
if(flag) printf("Case %d: YES\n",i); else printf("Case %d: NO\n",i);
}
return 0; }
|