#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
const int MAXN = 1e5 + 5;
int par[MAXN], sz[MAXN];
long long ret = 0;
set<int> adj[MAXN], radj[MAXN], followers[MAXN];
queue<pair<int, int>> que;
void conn(int u, int v) {
adj[u].insert(v);
radj[v].insert(u);
if (adj[v].count(u))
que.emplace(u, v);
}
int find(int u) {
return par[u] == u ? u : par[u] = find(par[u]);
}
int getSz(int u) {
return int(adj[u].size() + radj[u].size() + followers[u].size());
}
void unite(int u, int v) {
u = find(u);
v = find(v);
if (u == v)
return;
if (getSz(u) < getSz(v))
swap(u, v);
ret += sz[u] * followers[v].size() + sz[v] * followers[u].size();
par[v] = u;
sz[u] += sz[v];
for (int x : followers[v]) {
if (followers[u].count(x))
ret -= sz[u];
else
followers[u].insert(x);
}
adj[u].erase(v);
radj[u].erase(v);
adj[v].erase(u);
radj[v].erase(u);
for (int x : adj[v])
conn(u, x);
for (int x : radj[v])
conn(x, u);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i=0; i<n; i++) {
par[i] = i;
sz[i] = 1;
followers[i].insert(i);
}
for (int i=0; i<m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
b = find(b);
if (find(a) != b && !followers[b].count(a)) {
ret += sz[b];
followers[b].insert(a);
a = find(a);
conn(a, b);
while (!que.empty()) {
auto [u, v] = que.front();
que.pop();
unite(u, v);
}
}
cout << ret << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14544 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14416 KB |
Output is correct |
12 |
Correct |
6 ms |
14412 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14548 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
7 ms |
14424 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
6 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14380 KB |
Output is correct |
29 |
Correct |
7 ms |
14428 KB |
Output is correct |
30 |
Correct |
6 ms |
14420 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
7 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14544 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14416 KB |
Output is correct |
12 |
Correct |
6 ms |
14412 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14548 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
7 ms |
14424 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
6 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14380 KB |
Output is correct |
29 |
Correct |
7 ms |
14428 KB |
Output is correct |
30 |
Correct |
6 ms |
14420 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
7 ms |
14428 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
67 ms |
20452 KB |
Output is correct |
36 |
Correct |
89 ms |
23620 KB |
Output is correct |
37 |
Correct |
87 ms |
23612 KB |
Output is correct |
38 |
Correct |
88 ms |
23244 KB |
Output is correct |
39 |
Correct |
9 ms |
14676 KB |
Output is correct |
40 |
Correct |
12 ms |
15572 KB |
Output is correct |
41 |
Correct |
12 ms |
15580 KB |
Output is correct |
42 |
Correct |
10 ms |
14676 KB |
Output is correct |
43 |
Correct |
10 ms |
14804 KB |
Output is correct |
44 |
Correct |
10 ms |
14804 KB |
Output is correct |
45 |
Correct |
9 ms |
14752 KB |
Output is correct |
46 |
Correct |
8 ms |
14688 KB |
Output is correct |
47 |
Correct |
10 ms |
15060 KB |
Output is correct |
48 |
Correct |
10 ms |
15072 KB |
Output is correct |
49 |
Correct |
17 ms |
16224 KB |
Output is correct |
50 |
Correct |
88 ms |
23756 KB |
Output is correct |
51 |
Correct |
13 ms |
15456 KB |
Output is correct |
52 |
Correct |
74 ms |
21896 KB |
Output is correct |
53 |
Correct |
16 ms |
15956 KB |
Output is correct |
54 |
Correct |
79 ms |
22780 KB |
Output is correct |
55 |
Correct |
15 ms |
15352 KB |
Output is correct |
56 |
Correct |
11 ms |
15444 KB |
Output is correct |
57 |
Correct |
13 ms |
15456 KB |
Output is correct |
58 |
Correct |
11 ms |
15472 KB |
Output is correct |
59 |
Correct |
10 ms |
15200 KB |
Output is correct |
60 |
Correct |
62 ms |
20024 KB |
Output is correct |
61 |
Correct |
11 ms |
15200 KB |
Output is correct |
62 |
Correct |
95 ms |
23036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14408 KB |
Output is correct |
8 |
Correct |
7 ms |
14544 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14416 KB |
Output is correct |
12 |
Correct |
6 ms |
14412 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14424 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14420 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14548 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
7 ms |
14424 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
6 ms |
14420 KB |
Output is correct |
28 |
Correct |
7 ms |
14380 KB |
Output is correct |
29 |
Correct |
7 ms |
14428 KB |
Output is correct |
30 |
Correct |
6 ms |
14420 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
7 ms |
14428 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
67 ms |
20452 KB |
Output is correct |
36 |
Correct |
89 ms |
23620 KB |
Output is correct |
37 |
Correct |
87 ms |
23612 KB |
Output is correct |
38 |
Correct |
88 ms |
23244 KB |
Output is correct |
39 |
Correct |
9 ms |
14676 KB |
Output is correct |
40 |
Correct |
12 ms |
15572 KB |
Output is correct |
41 |
Correct |
12 ms |
15580 KB |
Output is correct |
42 |
Correct |
10 ms |
14676 KB |
Output is correct |
43 |
Correct |
10 ms |
14804 KB |
Output is correct |
44 |
Correct |
10 ms |
14804 KB |
Output is correct |
45 |
Correct |
9 ms |
14752 KB |
Output is correct |
46 |
Correct |
8 ms |
14688 KB |
Output is correct |
47 |
Correct |
10 ms |
15060 KB |
Output is correct |
48 |
Correct |
10 ms |
15072 KB |
Output is correct |
49 |
Correct |
17 ms |
16224 KB |
Output is correct |
50 |
Correct |
88 ms |
23756 KB |
Output is correct |
51 |
Correct |
13 ms |
15456 KB |
Output is correct |
52 |
Correct |
74 ms |
21896 KB |
Output is correct |
53 |
Correct |
16 ms |
15956 KB |
Output is correct |
54 |
Correct |
79 ms |
22780 KB |
Output is correct |
55 |
Correct |
15 ms |
15352 KB |
Output is correct |
56 |
Correct |
11 ms |
15444 KB |
Output is correct |
57 |
Correct |
13 ms |
15456 KB |
Output is correct |
58 |
Correct |
11 ms |
15472 KB |
Output is correct |
59 |
Correct |
10 ms |
15200 KB |
Output is correct |
60 |
Correct |
62 ms |
20024 KB |
Output is correct |
61 |
Correct |
11 ms |
15200 KB |
Output is correct |
62 |
Correct |
95 ms |
23036 KB |
Output is correct |
63 |
Correct |
499 ms |
67592 KB |
Output is correct |
64 |
Correct |
523 ms |
67668 KB |
Output is correct |
65 |
Correct |
487 ms |
67556 KB |
Output is correct |
66 |
Correct |
185 ms |
33664 KB |
Output is correct |
67 |
Correct |
672 ms |
96544 KB |
Output is correct |
68 |
Correct |
164 ms |
33644 KB |
Output is correct |
69 |
Correct |
320 ms |
34712 KB |
Output is correct |
70 |
Correct |
192 ms |
33636 KB |
Output is correct |
71 |
Correct |
181 ms |
33680 KB |
Output is correct |
72 |
Correct |
534 ms |
54112 KB |
Output is correct |
73 |
Correct |
491 ms |
56988 KB |
Output is correct |
74 |
Correct |
1510 ms |
106948 KB |
Output is correct |
75 |
Correct |
628 ms |
46056 KB |
Output is correct |
76 |
Correct |
874 ms |
70548 KB |
Output is correct |
77 |
Correct |
945 ms |
70880 KB |
Output is correct |
78 |
Correct |
215 ms |
40764 KB |
Output is correct |
79 |
Correct |
519 ms |
46536 KB |
Output is correct |
80 |
Correct |
225 ms |
39612 KB |
Output is correct |
81 |
Correct |
336 ms |
43664 KB |
Output is correct |
82 |
Correct |
1058 ms |
65916 KB |
Output is correct |
83 |
Correct |
985 ms |
65876 KB |
Output is correct |
84 |
Correct |
735 ms |
68960 KB |
Output is correct |
85 |
Correct |
766 ms |
68888 KB |
Output is correct |
86 |
Correct |
307 ms |
66388 KB |
Output is correct |
87 |
Correct |
314 ms |
68680 KB |
Output is correct |
88 |
Correct |
527 ms |
55472 KB |
Output is correct |
89 |
Correct |
900 ms |
68144 KB |
Output is correct |