forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
139 lines (120 loc) · 3.62 KB
/
E.cpp
File metadata and controls
139 lines (120 loc) · 3.62 KB
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
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 100111;
int V;
vector<int> G[MN];
struct UndirectedDfs {
vector<int> low, num, parent;
vector<bool> articulation;
int counter, root, children;
vector< pair<int,int> > bridges;
vector<int> cuts;
UndirectedDfs() : low(V, 0), num(V, -1), parent(V, 0), articulation(V, false),
counter(0), children(0) {
for(int i = 0; i < V; ++i) if (num[i] == -1) {
root = i; children = 0;
dfs(i);
articulation[root] = (children > 1);
}
for(int i = 0; i < V; ++i)
if (articulation[i]) cuts.push_back(i);
}
private:
void dfs(int u) {
low[u] = num[u] = counter++;
for(int j = 0; j < G[u].size(); ++j) {
int v = G[u][j];
if (num[v] == -1) {
parent[v] = u;
if (u == root) children++;
dfs(v);
if (low[v] >= num[u])
articulation[u] = true;
if (low[v] > num[u]) bridges.push_back(make_pair(u, v));
low[u] = min(low[u], low[v]);
} else if (v != parent[u])
low[u] = min(low[u], num[v]);
}
}
};
struct DSU {
int lab[MN];
void init(int n) {
REP(i,n+1) lab[i] = -1;
}
int getRoot(int u) {
if (lab[u] < 0) return u;
return lab[u] = getRoot(lab[u]);
}
bool merge(int u, int v) {
u = getRoot(u); v = getRoot(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
set< pair<int,int> > bridges;
int visited[MN];
void dfs(int u) {
visited[u] = true;
for(int v : G[u]) {
if (!visited[v]) dfs(v);
}
}
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
int m;
while (scanf("%lld%lld", &V, &m) == 2) {
REP(i,V) G[i].clear();
REP(i,m) {
int u, v; scanf("%lld%lld", &u, &v);
--u; --v;
G[u].push_back(v);
G[v].push_back(u);
}
UndirectedDfs tree;
bridges.clear();
for(auto b : tree.bridges) {
bridges.insert(b);
bridges.insert(make_pair(b.second, b.first));
}
int n = V;
DSU dsu;
dsu.init(n);
REP(u,n) for(int v : G[u]) {
if (bridges.find(make_pair(u, v)) != bridges.end()) continue;
dsu.merge(u, v);
}
int res = 0;
memset(visited, 0, sizeof visited);
// first, we DFS from all good vertices
REP(u,n) {
int r = dsu.getRoot(u);
int cnt = -dsu.lab[r];
if (cnt > 1 && !visited[u]) {
dfs(u);
}
}
REP(u,n) if (!visited[u]) {
++res;
dfs(u);
}
cout << res << endl;
}
}