-
Notifications
You must be signed in to change notification settings - Fork 185
/
11770.cc
55 lines (55 loc) · 1.12 KB
/
11770.cc
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
// https://uva.onlinejudge.org/external/117/11770.pdf
#include<bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using qi=queue<int>;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t,n,m,u,v;
cin>>t;
for(int T=1;T<=t;T++){
cin>>n>>m;
vvi g(n),h(n),c;
for(int i=0;i<m;i++){
cin>>u>>v;
u--;v--;
g[u].push_back(v);
h[v].push_back(u);
}
vi a,s(n),t(n),r(n);
function<void(int)>dfs1=[&](int i){
s[i]=1;
for(int j:g[i])
if(!s[j])dfs1(j);
a.push_back(i);
};
for(int i=0;i<n;i++)
if(!s[i])dfs1(i);
reverse(a.begin(),a.end());
function<void(int)>dfs2=[&](int i){
t[i]=1;
c.back().push_back(i);
for(int j:h[i])
if(!t[j])dfs2(j);
};
for(int i=0;i<a.size();i++)
if(!t[a[i]]){
c.push_back(vi());
dfs2(a[i]);
}
function<void(int)>dfs3=[&](int i){
r[i]=1;
for(int j:g[i])
if(!r[j])dfs3(j);
};
int k=0;
for(vi &x:c)
if(!r[x[0]]){
k++;
dfs3(x[0]);
}
cout<<"Case "<<T<<": "<<k<<"\n";
}
}