#include <bits/stdc++.h>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 1e5 + 5;
typedef pair<int, int> ii;
struct DSU {
//For each component, count the amount of inDeg from DISTINCT vertexes from different components.
int p[N], sz[N];
set<int> out[N], in[N];
set<int> distIn[N], have[N];
bool inQ[N];
ll compSum = 0, inSum = 0;
void init(int n) {
fw (i, 0, n) {
p[i] = i, sz[i] = 1;
out[i].clear(), in[i].clear();
inQ[i] = 0;
have[i].insert(i);
}
}
int getp(int u) {
return u == p[u] ? u : p[u] = getp(p[u]);
}
void addEdge(int u, int v) {
int ogu = u;
u = getp(u), v = getp(v);
if (u == v) return;
inSum -= 1ll * distIn[v].size() * sz[v];
distIn[v].insert(ogu);
inSum += 1ll * distIn[v].size() * sz[v];
in[v].insert(u);
out[u].insert(v);
if (!out[v].count(u)) return;
joint(u, v);
}
void joint(int u, int v) {
//Jointing u and v have the side effect of jointing some more vertexes into it. Keep a queue
queue<int> q;
q.push(v);
inQ[v] = 1;
int root = u;
while (!q.empty()) {
int i = q.front(); q.pop();
inQ[i] = 0;
if (sz[root] < sz[i]) swap(root, i);
p[i] = root;
compSum -= 1ll * sz[root] * (sz[root] - 1);
compSum -= 1ll * sz[i] * (sz[i] - 1);
inSum -= 1ll * distIn[i].size() * sz[i];
inSum -= 1ll * distIn[root].size() * sz[root];
sz[root] += sz[i];
compSum += 1ll * sz[root] * (sz[root] - 1);
//Delete the 2 edges between i and root
in[i].erase(root), in[root].erase(i), out[i].erase(root), out[root].erase(i);
fa (j, out[i]) {
out[root].insert(j);
in[j].erase(i);
in[j].insert(root);
if (in[root].count(j)) {
if (!inQ[j]) {
inQ[j] = 1;
q.push(j);
}
}
}
fa (j, in[i]) {
in[root].insert(j);
out[j].erase(i);
out[j].insert(root);
if (out[root].count(j)) {
if (!inQ[j]) {
inQ[j] = 1;
q.push(j);
}
}
}
fa (j, have[i]) {
have[root].insert(j);
if (distIn[root].count(j)) distIn[root].erase(j);
}
fa (j, distIn[i]) {
if (!have[root].count(j)) distIn[root].insert(j);
}
inSum += 1ll * distIn[root].size() * sz[root];
}
}
} dsu;
int n, q;
vector<int> g[N];
bool used[N];
signed main() {
#ifdef BLU
in("blu.inp");
// out("ans1.out");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
dsu.init(n);
while (q--) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].pb(b);
// cout << "Add edge " << a + 1 << " and " << b + 1 << "\n";
dsu.addEdge(a, b);
// int finalAns = 0;
// fw (i, 0, n) {
// finalAns += dsu.sz[dsu.getp(i)] - 1;
// fa (j, g[i]) if (dsu.getp(j) != dsu.getp(i)) {
// if (!used[dsu.getp(j)]) finalAns += dsu.sz[dsu.getp(j)];
// used[dsu.getp(j)] = 1;
// }
// fa (j, g[i]) used[dsu.getp(j)] = 0;
// }
// cout << finalAns << "\n";
cout << dsu.compSum + dsu.inSum << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
19 ms |
21760 KB |
Output is correct |
8 |
Correct |
18 ms |
21632 KB |
Output is correct |
9 |
Correct |
18 ms |
21632 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21556 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
17 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
18 ms |
21632 KB |
Output is correct |
22 |
Correct |
17 ms |
21536 KB |
Output is correct |
23 |
Correct |
17 ms |
21504 KB |
Output is correct |
24 |
Correct |
20 ms |
21632 KB |
Output is correct |
25 |
Correct |
18 ms |
21632 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
17 ms |
21504 KB |
Output is correct |
28 |
Correct |
22 ms |
21504 KB |
Output is correct |
29 |
Correct |
17 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
18 ms |
21632 KB |
Output is correct |
32 |
Correct |
17 ms |
21504 KB |
Output is correct |
33 |
Correct |
17 ms |
21632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
19 ms |
21760 KB |
Output is correct |
8 |
Correct |
18 ms |
21632 KB |
Output is correct |
9 |
Correct |
18 ms |
21632 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21556 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
17 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
18 ms |
21632 KB |
Output is correct |
22 |
Correct |
17 ms |
21536 KB |
Output is correct |
23 |
Correct |
17 ms |
21504 KB |
Output is correct |
24 |
Correct |
20 ms |
21632 KB |
Output is correct |
25 |
Correct |
18 ms |
21632 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
17 ms |
21504 KB |
Output is correct |
28 |
Correct |
22 ms |
21504 KB |
Output is correct |
29 |
Correct |
17 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
18 ms |
21632 KB |
Output is correct |
32 |
Correct |
17 ms |
21504 KB |
Output is correct |
33 |
Correct |
17 ms |
21632 KB |
Output is correct |
34 |
Correct |
19 ms |
21760 KB |
Output is correct |
35 |
Correct |
110 ms |
31864 KB |
Output is correct |
36 |
Correct |
134 ms |
34808 KB |
Output is correct |
37 |
Correct |
148 ms |
34936 KB |
Output is correct |
38 |
Correct |
137 ms |
34424 KB |
Output is correct |
39 |
Correct |
20 ms |
22016 KB |
Output is correct |
40 |
Correct |
22 ms |
22400 KB |
Output is correct |
41 |
Correct |
22 ms |
22272 KB |
Output is correct |
42 |
Correct |
21 ms |
22016 KB |
Output is correct |
43 |
Correct |
22 ms |
22016 KB |
Output is correct |
44 |
Correct |
22 ms |
22016 KB |
Output is correct |
45 |
Correct |
20 ms |
22016 KB |
Output is correct |
46 |
Correct |
20 ms |
22016 KB |
Output is correct |
47 |
Correct |
22 ms |
22144 KB |
Output is correct |
48 |
Correct |
23 ms |
22240 KB |
Output is correct |
49 |
Correct |
30 ms |
23040 KB |
Output is correct |
50 |
Correct |
151 ms |
35084 KB |
Output is correct |
51 |
Correct |
26 ms |
22400 KB |
Output is correct |
52 |
Correct |
118 ms |
33144 KB |
Output is correct |
53 |
Correct |
30 ms |
22904 KB |
Output is correct |
54 |
Correct |
128 ms |
34040 KB |
Output is correct |
55 |
Correct |
24 ms |
22400 KB |
Output is correct |
56 |
Correct |
24 ms |
22400 KB |
Output is correct |
57 |
Correct |
23 ms |
22400 KB |
Output is correct |
58 |
Correct |
23 ms |
22400 KB |
Output is correct |
59 |
Correct |
21 ms |
22400 KB |
Output is correct |
60 |
Correct |
104 ms |
31736 KB |
Output is correct |
61 |
Correct |
23 ms |
22272 KB |
Output is correct |
62 |
Correct |
136 ms |
34296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
19 ms |
21760 KB |
Output is correct |
8 |
Correct |
18 ms |
21632 KB |
Output is correct |
9 |
Correct |
18 ms |
21632 KB |
Output is correct |
10 |
Correct |
18 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21556 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
17 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
18 ms |
21632 KB |
Output is correct |
22 |
Correct |
17 ms |
21536 KB |
Output is correct |
23 |
Correct |
17 ms |
21504 KB |
Output is correct |
24 |
Correct |
20 ms |
21632 KB |
Output is correct |
25 |
Correct |
18 ms |
21632 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
17 ms |
21504 KB |
Output is correct |
28 |
Correct |
22 ms |
21504 KB |
Output is correct |
29 |
Correct |
17 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
18 ms |
21632 KB |
Output is correct |
32 |
Correct |
17 ms |
21504 KB |
Output is correct |
33 |
Correct |
17 ms |
21632 KB |
Output is correct |
34 |
Correct |
19 ms |
21760 KB |
Output is correct |
35 |
Correct |
110 ms |
31864 KB |
Output is correct |
36 |
Correct |
134 ms |
34808 KB |
Output is correct |
37 |
Correct |
148 ms |
34936 KB |
Output is correct |
38 |
Correct |
137 ms |
34424 KB |
Output is correct |
39 |
Correct |
20 ms |
22016 KB |
Output is correct |
40 |
Correct |
22 ms |
22400 KB |
Output is correct |
41 |
Correct |
22 ms |
22272 KB |
Output is correct |
42 |
Correct |
21 ms |
22016 KB |
Output is correct |
43 |
Correct |
22 ms |
22016 KB |
Output is correct |
44 |
Correct |
22 ms |
22016 KB |
Output is correct |
45 |
Correct |
20 ms |
22016 KB |
Output is correct |
46 |
Correct |
20 ms |
22016 KB |
Output is correct |
47 |
Correct |
22 ms |
22144 KB |
Output is correct |
48 |
Correct |
23 ms |
22240 KB |
Output is correct |
49 |
Correct |
30 ms |
23040 KB |
Output is correct |
50 |
Correct |
151 ms |
35084 KB |
Output is correct |
51 |
Correct |
26 ms |
22400 KB |
Output is correct |
52 |
Correct |
118 ms |
33144 KB |
Output is correct |
53 |
Correct |
30 ms |
22904 KB |
Output is correct |
54 |
Correct |
128 ms |
34040 KB |
Output is correct |
55 |
Correct |
24 ms |
22400 KB |
Output is correct |
56 |
Correct |
24 ms |
22400 KB |
Output is correct |
57 |
Correct |
23 ms |
22400 KB |
Output is correct |
58 |
Correct |
23 ms |
22400 KB |
Output is correct |
59 |
Correct |
21 ms |
22400 KB |
Output is correct |
60 |
Correct |
104 ms |
31736 KB |
Output is correct |
61 |
Correct |
23 ms |
22272 KB |
Output is correct |
62 |
Correct |
136 ms |
34296 KB |
Output is correct |
63 |
Correct |
653 ms |
80800 KB |
Output is correct |
64 |
Correct |
641 ms |
80892 KB |
Output is correct |
65 |
Correct |
629 ms |
80760 KB |
Output is correct |
66 |
Correct |
333 ms |
45564 KB |
Output is correct |
67 |
Correct |
618 ms |
72784 KB |
Output is correct |
68 |
Correct |
318 ms |
45548 KB |
Output is correct |
69 |
Correct |
664 ms |
46500 KB |
Output is correct |
70 |
Correct |
374 ms |
45560 KB |
Output is correct |
71 |
Correct |
401 ms |
45856 KB |
Output is correct |
72 |
Correct |
677 ms |
58008 KB |
Output is correct |
73 |
Correct |
676 ms |
59640 KB |
Output is correct |
74 |
Correct |
1383 ms |
83668 KB |
Output is correct |
75 |
Correct |
940 ms |
53228 KB |
Output is correct |
76 |
Correct |
1083 ms |
67320 KB |
Output is correct |
77 |
Correct |
1088 ms |
67448 KB |
Output is correct |
78 |
Correct |
317 ms |
47848 KB |
Output is correct |
79 |
Correct |
591 ms |
54248 KB |
Output is correct |
80 |
Correct |
341 ms |
47604 KB |
Output is correct |
81 |
Correct |
624 ms |
52468 KB |
Output is correct |
82 |
Correct |
1118 ms |
65912 KB |
Output is correct |
83 |
Correct |
1117 ms |
65912 KB |
Output is correct |
84 |
Correct |
883 ms |
64572 KB |
Output is correct |
85 |
Correct |
869 ms |
64504 KB |
Output is correct |
86 |
Correct |
472 ms |
79144 KB |
Output is correct |
87 |
Correct |
537 ms |
82720 KB |
Output is correct |
88 |
Correct |
715 ms |
59000 KB |
Output is correct |
89 |
Correct |
1057 ms |
65356 KB |
Output is correct |