-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathFlight Route Requests.cpp
64 lines (55 loc) · 1.29 KB
/
Flight Route Requests.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
int N, M, ds[maxN], vis[maxN];
vector<int> G[maxN], CC[maxN];
int find(int u){
if(ds[u] < 0) return u;
ds[u] = find(ds[u]);
return ds[u];
}
bool merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return false;
if(ds[u] < ds[v]) swap(u, v);
ds[v] += ds[u];
ds[u] = v;
return true;
}
bool dfs(int u){
vis[u] = -1;
bool hascycle = false;
for(int v : G[u]){
if(vis[v] == -1) return true;
else if(vis[v] == 0) hascycle |= dfs(v);
}
vis[u] = 1;
return hascycle;
}
int main(){
scanf("%d %d", &N, &M);
fill(ds+1, ds+N+1, -1);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
merge(a, b);
}
int K = 0;
unordered_map<int,int> getID;
for(int u = 1; u <= N; u++){
int rep = find(u);
if(!getID[rep])
getID[rep] = ++K;
CC[getID[rep]].push_back(u);
}
int ans = 0;
for(int k = 1; k <= K; k++){
int sz = (int) CC[k].size();
bool hascycle = false;
for(int u : CC[k])
if(!hascycle && vis[u] == 0)
hascycle |= dfs(u);
ans += (hascycle ? sz : sz-1);
}
printf("%d\n", ans);
}