#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define V vector
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "LINE(" << __LINE__ << ") ->" << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e5 + 7;
unordered_set<int> G[N], rG[N], ch[N];
int p[N], sz[N];
queue<pi> q;
ll ans = 0;
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void add_edge(int u, int v) {
G[u].insert(v);
rG[v].insert(u);
if(G[v].count(u)) {
q.push({u, v});
}
}
int get_size(int u) {
return G[u].size() + rG[u].size() + ch[u].size();
}
void unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return;
if(get_size(x) > get_size(y)) swap(x, y);
ans += (ll) sz[x] * ch[y].size() + (ll) sz[y] * ch[x].size();
p[x] = y;
sz[y] += sz[x];
// merge x into y
for(int i:ch[x]) {
if(ch[y].count(i)) ans -= sz[y];
else ch[y].insert(i);
}
G[x].erase(y), G[y].erase(x);
rG[x].erase(y), rG[y].erase(x);
for(int i:G[x]) {
G[y].insert(i);
add_edge(y, i);
}
for(int i:rG[x]) {
rG[y].insert(i);
add_edge(i, y);
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
p[i] = i, sz[i] = 1;
ch[i].insert(i);
}
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
v = find(v);
if(find(u) != v && ch[v].count(u) == 0) {
ch[v].insert(u);
ans += sz[v]; // u will connect to everyone in v's component
u = find(u);
add_edge(u, v);
while(q.size()) {
pi p = q.front(); q.pop();
unite(p.F, p.S);
}
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16748 KB |
Output is correct |
2 |
Correct |
14 ms |
16748 KB |
Output is correct |
3 |
Correct |
13 ms |
16748 KB |
Output is correct |
4 |
Correct |
14 ms |
16748 KB |
Output is correct |
5 |
Correct |
14 ms |
16748 KB |
Output is correct |
6 |
Correct |
13 ms |
16748 KB |
Output is correct |
7 |
Correct |
14 ms |
16876 KB |
Output is correct |
8 |
Correct |
14 ms |
16876 KB |
Output is correct |
9 |
Correct |
14 ms |
16876 KB |
Output is correct |
10 |
Correct |
14 ms |
16748 KB |
Output is correct |
11 |
Correct |
14 ms |
16748 KB |
Output is correct |
12 |
Correct |
13 ms |
16748 KB |
Output is correct |
13 |
Correct |
14 ms |
16748 KB |
Output is correct |
14 |
Correct |
13 ms |
16748 KB |
Output is correct |
15 |
Correct |
15 ms |
16748 KB |
Output is correct |
16 |
Correct |
14 ms |
16748 KB |
Output is correct |
17 |
Correct |
14 ms |
16748 KB |
Output is correct |
18 |
Correct |
13 ms |
16748 KB |
Output is correct |
19 |
Correct |
13 ms |
16748 KB |
Output is correct |
20 |
Correct |
14 ms |
16876 KB |
Output is correct |
21 |
Correct |
14 ms |
17004 KB |
Output is correct |
22 |
Correct |
13 ms |
16748 KB |
Output is correct |
23 |
Correct |
14 ms |
16876 KB |
Output is correct |
24 |
Correct |
14 ms |
16876 KB |
Output is correct |
25 |
Correct |
15 ms |
17004 KB |
Output is correct |
26 |
Correct |
13 ms |
16748 KB |
Output is correct |
27 |
Correct |
13 ms |
16748 KB |
Output is correct |
28 |
Correct |
14 ms |
16748 KB |
Output is correct |
29 |
Correct |
13 ms |
16748 KB |
Output is correct |
30 |
Correct |
13 ms |
16748 KB |
Output is correct |
31 |
Correct |
14 ms |
16876 KB |
Output is correct |
32 |
Correct |
13 ms |
16876 KB |
Output is correct |
33 |
Correct |
15 ms |
16876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16748 KB |
Output is correct |
2 |
Correct |
14 ms |
16748 KB |
Output is correct |
3 |
Correct |
13 ms |
16748 KB |
Output is correct |
4 |
Correct |
14 ms |
16748 KB |
Output is correct |
5 |
Correct |
14 ms |
16748 KB |
Output is correct |
6 |
Correct |
13 ms |
16748 KB |
Output is correct |
7 |
Correct |
14 ms |
16876 KB |
Output is correct |
8 |
Correct |
14 ms |
16876 KB |
Output is correct |
9 |
Correct |
14 ms |
16876 KB |
Output is correct |
10 |
Correct |
14 ms |
16748 KB |
Output is correct |
11 |
Correct |
14 ms |
16748 KB |
Output is correct |
12 |
Correct |
13 ms |
16748 KB |
Output is correct |
13 |
Correct |
14 ms |
16748 KB |
Output is correct |
14 |
Correct |
13 ms |
16748 KB |
Output is correct |
15 |
Correct |
15 ms |
16748 KB |
Output is correct |
16 |
Correct |
14 ms |
16748 KB |
Output is correct |
17 |
Correct |
14 ms |
16748 KB |
Output is correct |
18 |
Correct |
13 ms |
16748 KB |
Output is correct |
19 |
Correct |
13 ms |
16748 KB |
Output is correct |
20 |
Correct |
14 ms |
16876 KB |
Output is correct |
21 |
Correct |
14 ms |
17004 KB |
Output is correct |
22 |
Correct |
13 ms |
16748 KB |
Output is correct |
23 |
Correct |
14 ms |
16876 KB |
Output is correct |
24 |
Correct |
14 ms |
16876 KB |
Output is correct |
25 |
Correct |
15 ms |
17004 KB |
Output is correct |
26 |
Correct |
13 ms |
16748 KB |
Output is correct |
27 |
Correct |
13 ms |
16748 KB |
Output is correct |
28 |
Correct |
14 ms |
16748 KB |
Output is correct |
29 |
Correct |
13 ms |
16748 KB |
Output is correct |
30 |
Correct |
13 ms |
16748 KB |
Output is correct |
31 |
Correct |
14 ms |
16876 KB |
Output is correct |
32 |
Correct |
13 ms |
16876 KB |
Output is correct |
33 |
Correct |
15 ms |
16876 KB |
Output is correct |
34 |
Correct |
16 ms |
17004 KB |
Output is correct |
35 |
Correct |
106 ms |
21100 KB |
Output is correct |
36 |
Correct |
128 ms |
23660 KB |
Output is correct |
37 |
Correct |
129 ms |
23788 KB |
Output is correct |
38 |
Correct |
126 ms |
23404 KB |
Output is correct |
39 |
Correct |
17 ms |
17260 KB |
Output is correct |
40 |
Correct |
24 ms |
18028 KB |
Output is correct |
41 |
Correct |
22 ms |
18028 KB |
Output is correct |
42 |
Correct |
17 ms |
17260 KB |
Output is correct |
43 |
Correct |
17 ms |
17260 KB |
Output is correct |
44 |
Correct |
18 ms |
17260 KB |
Output is correct |
45 |
Correct |
18 ms |
17260 KB |
Output is correct |
46 |
Correct |
17 ms |
17260 KB |
Output is correct |
47 |
Correct |
19 ms |
17644 KB |
Output is correct |
48 |
Correct |
19 ms |
17644 KB |
Output is correct |
49 |
Correct |
27 ms |
18540 KB |
Output is correct |
50 |
Correct |
140 ms |
23916 KB |
Output is correct |
51 |
Correct |
21 ms |
17772 KB |
Output is correct |
52 |
Correct |
115 ms |
21996 KB |
Output is correct |
53 |
Correct |
27 ms |
18412 KB |
Output is correct |
54 |
Correct |
137 ms |
23084 KB |
Output is correct |
55 |
Correct |
21 ms |
17772 KB |
Output is correct |
56 |
Correct |
21 ms |
17772 KB |
Output is correct |
57 |
Correct |
20 ms |
17900 KB |
Output is correct |
58 |
Correct |
20 ms |
17900 KB |
Output is correct |
59 |
Correct |
19 ms |
17644 KB |
Output is correct |
60 |
Correct |
101 ms |
20556 KB |
Output is correct |
61 |
Correct |
20 ms |
17644 KB |
Output is correct |
62 |
Correct |
126 ms |
23276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16748 KB |
Output is correct |
2 |
Correct |
14 ms |
16748 KB |
Output is correct |
3 |
Correct |
13 ms |
16748 KB |
Output is correct |
4 |
Correct |
14 ms |
16748 KB |
Output is correct |
5 |
Correct |
14 ms |
16748 KB |
Output is correct |
6 |
Correct |
13 ms |
16748 KB |
Output is correct |
7 |
Correct |
14 ms |
16876 KB |
Output is correct |
8 |
Correct |
14 ms |
16876 KB |
Output is correct |
9 |
Correct |
14 ms |
16876 KB |
Output is correct |
10 |
Correct |
14 ms |
16748 KB |
Output is correct |
11 |
Correct |
14 ms |
16748 KB |
Output is correct |
12 |
Correct |
13 ms |
16748 KB |
Output is correct |
13 |
Correct |
14 ms |
16748 KB |
Output is correct |
14 |
Correct |
13 ms |
16748 KB |
Output is correct |
15 |
Correct |
15 ms |
16748 KB |
Output is correct |
16 |
Correct |
14 ms |
16748 KB |
Output is correct |
17 |
Correct |
14 ms |
16748 KB |
Output is correct |
18 |
Correct |
13 ms |
16748 KB |
Output is correct |
19 |
Correct |
13 ms |
16748 KB |
Output is correct |
20 |
Correct |
14 ms |
16876 KB |
Output is correct |
21 |
Correct |
14 ms |
17004 KB |
Output is correct |
22 |
Correct |
13 ms |
16748 KB |
Output is correct |
23 |
Correct |
14 ms |
16876 KB |
Output is correct |
24 |
Correct |
14 ms |
16876 KB |
Output is correct |
25 |
Correct |
15 ms |
17004 KB |
Output is correct |
26 |
Correct |
13 ms |
16748 KB |
Output is correct |
27 |
Correct |
13 ms |
16748 KB |
Output is correct |
28 |
Correct |
14 ms |
16748 KB |
Output is correct |
29 |
Correct |
13 ms |
16748 KB |
Output is correct |
30 |
Correct |
13 ms |
16748 KB |
Output is correct |
31 |
Correct |
14 ms |
16876 KB |
Output is correct |
32 |
Correct |
13 ms |
16876 KB |
Output is correct |
33 |
Correct |
15 ms |
16876 KB |
Output is correct |
34 |
Correct |
16 ms |
17004 KB |
Output is correct |
35 |
Correct |
106 ms |
21100 KB |
Output is correct |
36 |
Correct |
128 ms |
23660 KB |
Output is correct |
37 |
Correct |
129 ms |
23788 KB |
Output is correct |
38 |
Correct |
126 ms |
23404 KB |
Output is correct |
39 |
Correct |
17 ms |
17260 KB |
Output is correct |
40 |
Correct |
24 ms |
18028 KB |
Output is correct |
41 |
Correct |
22 ms |
18028 KB |
Output is correct |
42 |
Correct |
17 ms |
17260 KB |
Output is correct |
43 |
Correct |
17 ms |
17260 KB |
Output is correct |
44 |
Correct |
18 ms |
17260 KB |
Output is correct |
45 |
Correct |
18 ms |
17260 KB |
Output is correct |
46 |
Correct |
17 ms |
17260 KB |
Output is correct |
47 |
Correct |
19 ms |
17644 KB |
Output is correct |
48 |
Correct |
19 ms |
17644 KB |
Output is correct |
49 |
Correct |
27 ms |
18540 KB |
Output is correct |
50 |
Correct |
140 ms |
23916 KB |
Output is correct |
51 |
Correct |
21 ms |
17772 KB |
Output is correct |
52 |
Correct |
115 ms |
21996 KB |
Output is correct |
53 |
Correct |
27 ms |
18412 KB |
Output is correct |
54 |
Correct |
137 ms |
23084 KB |
Output is correct |
55 |
Correct |
21 ms |
17772 KB |
Output is correct |
56 |
Correct |
21 ms |
17772 KB |
Output is correct |
57 |
Correct |
20 ms |
17900 KB |
Output is correct |
58 |
Correct |
20 ms |
17900 KB |
Output is correct |
59 |
Correct |
19 ms |
17644 KB |
Output is correct |
60 |
Correct |
101 ms |
20556 KB |
Output is correct |
61 |
Correct |
20 ms |
17644 KB |
Output is correct |
62 |
Correct |
126 ms |
23276 KB |
Output is correct |
63 |
Correct |
745 ms |
65132 KB |
Output is correct |
64 |
Correct |
750 ms |
65132 KB |
Output is correct |
65 |
Correct |
750 ms |
65328 KB |
Output is correct |
66 |
Correct |
278 ms |
39952 KB |
Output is correct |
67 |
Correct |
1234 ms |
97040 KB |
Output is correct |
68 |
Correct |
293 ms |
40848 KB |
Output is correct |
69 |
Correct |
426 ms |
41232 KB |
Output is correct |
70 |
Correct |
309 ms |
40848 KB |
Output is correct |
71 |
Correct |
317 ms |
41108 KB |
Output is correct |
72 |
Correct |
847 ms |
58512 KB |
Output is correct |
73 |
Correct |
870 ms |
61072 KB |
Output is correct |
74 |
Correct |
1694 ms |
103572 KB |
Output is correct |
75 |
Correct |
655 ms |
49936 KB |
Output is correct |
76 |
Correct |
1102 ms |
70632 KB |
Output is correct |
77 |
Correct |
1116 ms |
70976 KB |
Output is correct |
78 |
Correct |
302 ms |
45072 KB |
Output is correct |
79 |
Correct |
474 ms |
50448 KB |
Output is correct |
80 |
Correct |
252 ms |
44340 KB |
Output is correct |
81 |
Correct |
423 ms |
47796 KB |
Output is correct |
82 |
Correct |
1031 ms |
66344 KB |
Output is correct |
83 |
Correct |
1025 ms |
66344 KB |
Output is correct |
84 |
Correct |
868 ms |
69464 KB |
Output is correct |
85 |
Correct |
871 ms |
69336 KB |
Output is correct |
86 |
Correct |
549 ms |
71336 KB |
Output is correct |
87 |
Correct |
602 ms |
72404 KB |
Output is correct |
88 |
Correct |
845 ms |
59024 KB |
Output is correct |
89 |
Correct |
1020 ms |
68008 KB |
Output is correct |