#include <bits/extc++.h>
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct chash { // large odd number for C
const uint64_t C = (int)(4e18 * acos(0)) | 71;
int operator()(int x) const { return __builtin_bswap64(x * C); }
};
using hash_set = __gnu_pbds::gp_hash_table<int, __gnu_pbds::null_type, chash>;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbSommet, nbAretes;
cin >> nbSommet >> nbAretes;
vector<map<int, hash_set>> outEdges(nbSommet), inEdges(nbSommet);
vector<int> inDeg(nbSommet);
vector<int> idCC(nbSommet), szCC(nbSommet, 1);
vector<vector<int>> inCC(nbSommet);
for (int i = 0; i < nbSommet; ++i)
inCC[i].push_back(i);
iota(idCC.begin(), idCC.end(), 0LL);
int nbFollows = 0;
for (int iArete = 0; iArete < nbAretes; ++iArete) {
int u, v;
cin >> u >> v;
--u, --v;
v = idCC[v];
if (idCC[u] == idCC[v] or
(inEdges[v].count(idCC[u]) and
inEdges[v][idCC[u]].find(u) != inEdges[v][idCC[u]].end())) {
cout << nbFollows << '\n';
continue;
}
nbFollows += szCC[v];
inEdges[v][idCC[u]].insert(u);
outEdges[idCC[u]][v].insert(u);
inDeg[v]++;
u = idCC[u];
queue<pair<int, int>> toMerge;
if (outEdges[v].count(u))
toMerge.emplace(u, v);
while (!toMerge.empty()) {
auto [A, B] = toMerge.front();
toMerge.pop();
A = idCC[A], B = idCC[B];
if (A == B)
continue;
dbg(A, B);
if (szCC[A] < szCC[B])
swap(A, B);
if (inEdges[A].count(B)) {
nbFollows -= szCC[A] * inEdges[A][B].size();
inDeg[A] -= inEdges[A][B].size();
inEdges[A].erase(B);
outEdges[B].erase(A);
}
if (outEdges[A].count(B)) {
nbFollows -= szCC[B] * outEdges[A][B].size();
inDeg[B] -= outEdges[A][B].size();
outEdges[A].erase(B);
inEdges[B].erase(A);
}
nbFollows += 2 * szCC[A] * szCC[B];
nbFollows -= inDeg[A] * szCC[A] + inDeg[B] * szCC[B];
for (auto &[cc, s] : inEdges[B]) {
for (int y : s) {
outEdges[cc][B].erase(y);
if (!inEdges[A].count(cc) or
inEdges[A][cc].find(y) == inEdges[A][cc].end()) {
inEdges[A][cc].insert(y), inDeg[A]++;
outEdges[cc][A].insert(y);
}
}
if (outEdges[A].count(cc))
toMerge.emplace(A, cc);
}
for (auto &[cc, s] : outEdges[B]) {
for (int y : s) {
inEdges[cc][B].erase(y);
inEdges[cc][A].insert(y);
outEdges[A][cc].insert(y);
}
if (inEdges[A].count(cc))
toMerge.emplace(A, cc);
}
szCC[A] += szCC[B];
nbFollows += szCC[A] * inDeg[A];
for (int x : inCC[B])
inCC[A].push_back(x), idCC[x] = A;
}
cout << nbFollows << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
77 ms |
11540 KB |
Output is correct |
36 |
Correct |
138 ms |
22344 KB |
Output is correct |
37 |
Correct |
153 ms |
22784 KB |
Output is correct |
38 |
Correct |
122 ms |
20904 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
5 ms |
1748 KB |
Output is correct |
41 |
Correct |
5 ms |
1748 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
5 ms |
1364 KB |
Output is correct |
44 |
Correct |
6 ms |
1320 KB |
Output is correct |
45 |
Correct |
3 ms |
720 KB |
Output is correct |
46 |
Correct |
3 ms |
724 KB |
Output is correct |
47 |
Correct |
8 ms |
1748 KB |
Output is correct |
48 |
Correct |
6 ms |
1804 KB |
Output is correct |
49 |
Correct |
18 ms |
6356 KB |
Output is correct |
50 |
Correct |
127 ms |
23348 KB |
Output is correct |
51 |
Correct |
14 ms |
2516 KB |
Output is correct |
52 |
Correct |
93 ms |
15132 KB |
Output is correct |
53 |
Correct |
16 ms |
5844 KB |
Output is correct |
54 |
Correct |
109 ms |
18956 KB |
Output is correct |
55 |
Correct |
11 ms |
3796 KB |
Output is correct |
56 |
Correct |
11 ms |
3688 KB |
Output is correct |
57 |
Correct |
9 ms |
3436 KB |
Output is correct |
58 |
Correct |
9 ms |
3540 KB |
Output is correct |
59 |
Correct |
4 ms |
900 KB |
Output is correct |
60 |
Correct |
62 ms |
5756 KB |
Output is correct |
61 |
Correct |
8 ms |
2388 KB |
Output is correct |
62 |
Correct |
142 ms |
20288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
77 ms |
11540 KB |
Output is correct |
36 |
Correct |
138 ms |
22344 KB |
Output is correct |
37 |
Correct |
153 ms |
22784 KB |
Output is correct |
38 |
Correct |
122 ms |
20904 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
5 ms |
1748 KB |
Output is correct |
41 |
Correct |
5 ms |
1748 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
5 ms |
1364 KB |
Output is correct |
44 |
Correct |
6 ms |
1320 KB |
Output is correct |
45 |
Correct |
3 ms |
720 KB |
Output is correct |
46 |
Correct |
3 ms |
724 KB |
Output is correct |
47 |
Correct |
8 ms |
1748 KB |
Output is correct |
48 |
Correct |
6 ms |
1804 KB |
Output is correct |
49 |
Correct |
18 ms |
6356 KB |
Output is correct |
50 |
Correct |
127 ms |
23348 KB |
Output is correct |
51 |
Correct |
14 ms |
2516 KB |
Output is correct |
52 |
Correct |
93 ms |
15132 KB |
Output is correct |
53 |
Correct |
16 ms |
5844 KB |
Output is correct |
54 |
Correct |
109 ms |
18956 KB |
Output is correct |
55 |
Correct |
11 ms |
3796 KB |
Output is correct |
56 |
Correct |
11 ms |
3688 KB |
Output is correct |
57 |
Correct |
9 ms |
3436 KB |
Output is correct |
58 |
Correct |
9 ms |
3540 KB |
Output is correct |
59 |
Correct |
4 ms |
900 KB |
Output is correct |
60 |
Correct |
62 ms |
5756 KB |
Output is correct |
61 |
Correct |
8 ms |
2388 KB |
Output is correct |
62 |
Correct |
142 ms |
20288 KB |
Output is correct |
63 |
Correct |
637 ms |
201536 KB |
Output is correct |
64 |
Correct |
681 ms |
201460 KB |
Output is correct |
65 |
Correct |
664 ms |
201564 KB |
Output is correct |
66 |
Correct |
180 ms |
22944 KB |
Output is correct |
67 |
Correct |
443 ms |
74428 KB |
Output is correct |
68 |
Correct |
180 ms |
22804 KB |
Output is correct |
69 |
Correct |
508 ms |
52448 KB |
Output is correct |
70 |
Correct |
188 ms |
22852 KB |
Output is correct |
71 |
Correct |
169 ms |
22784 KB |
Output is correct |
72 |
Correct |
533 ms |
78100 KB |
Output is correct |
73 |
Correct |
511 ms |
78676 KB |
Output is correct |
74 |
Correct |
1398 ms |
252472 KB |
Output is correct |
75 |
Correct |
723 ms |
78200 KB |
Output is correct |
76 |
Correct |
1016 ms |
163864 KB |
Output is correct |
77 |
Correct |
1016 ms |
164948 KB |
Output is correct |
78 |
Correct |
288 ms |
65656 KB |
Output is correct |
79 |
Correct |
474 ms |
69260 KB |
Output is correct |
80 |
Correct |
302 ms |
65776 KB |
Output is correct |
81 |
Correct |
443 ms |
69312 KB |
Output is correct |
82 |
Correct |
1291 ms |
173536 KB |
Output is correct |
83 |
Correct |
1128 ms |
172980 KB |
Output is correct |
84 |
Correct |
876 ms |
163012 KB |
Output is correct |
85 |
Correct |
872 ms |
163116 KB |
Output is correct |
86 |
Correct |
240 ms |
32940 KB |
Output is correct |
87 |
Correct |
240 ms |
35192 KB |
Output is correct |
88 |
Correct |
536 ms |
81064 KB |
Output is correct |
89 |
Correct |
996 ms |
152864 KB |
Output is correct |