#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 3e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;
int n, m;
map<pii,int> cnt, dd;
queue<pii> pq;
set<int> inS[N], outS[N], in[N];
ll numin[N];
ll res = 0;
struct Disjoin_set {
ll lab[N], sz[N];
void init (int n) {
rep (i, 1, n) lab[i] = i, sz[i] = 1;
}
int Find (int u) {
return u == lab[u] ? lab[u] = u : Find(lab[u]);
}
void add_edge (int u, int v) {
int uu = Find(u), vv = Find(v);
if (uu == vv) return;
// cout << u<<" "<<v<<" "<<uu<<" "<<dd[{u, vv}]<<" hihi\n";
if (dd[{u, vv}] == 0) {
dd[{u, vv}] = 1;
cnt[{uu, vv}]++;
numin[vv]++;
res += sz[vv];
}
if (inS[uu].find(vv) != inS[uu].end()) pq.push({uu, vv});
inS[vv].insert(uu);
outS[uu].insert(vv);
in[vv].insert(u);
}
void Join (int u, int v) {
u = Find(u), v = Find(v);
if (u == v) return;
if (SZ(in[u]) + SZ(outS[u]) + SZ(inS[u]) < SZ(in[v]) + SZ(outS[v]) + SZ(inS[v])) swap(u, v);
// cout << u <<" "<<v<<" hihi\n";
numin[u] -= cnt[{v, u}];
numin[v] -= cnt[{u, v}];
res -= sz[u] * (sz[u] - 1) + sz[v] * (sz[v] - 1);
res -= sz[v] * cnt[{u, v}] + sz[u] * cnt[{v, u}];
// cout << res<<" "<<cnt[{3, 1}]<<" "<<cnt[{1, 3}]<<"\n";
res += (sz[u] + sz[v]) * (sz[u] + sz[v] - 1);
res += numin[v] * sz[u] + numin[u] * sz[v];
numin[u] += numin[v];
iter (&id, in[v]) {
if (Find(id) == v || Find(id) == u) continue;
if (in[u].find(id) != in[u].end()) {
// cout << id <<"\n";
res -= sz[u] + sz[v];
numin[u] --;
}
if (dd[{id, u}] == 0) {
dd[{id, u}] = 1;
cnt[{Find(id), u}]++;
}
}
iter (&id, inS[v]) {
if (id == u) continue;
if (outS[u].find(id) != outS[u].end()) pq.push({id, u});
}
iter (&id, outS[v]) {
if (id == u) continue;
if (inS[u].find(id) != inS[u].end()) pq.push({id, u});
}
iter (&id, in[v]) {
if (Find(id) == v || Find(id) == u) continue;
dd[{id, u}] = 1;
in[u].insert(id);
}
iter (&id, outS[v]) {
if (id == u) continue;
cnt[{u, id}] += cnt[{v, id}];
inS[id].erase(v), inS[id].insert(u);
outS[u].insert(id);
}
iter (&id, inS[v]) {
if (id == u) continue;
outS[id].erase(v), outS[id].insert(u);
inS[u].insert(id);
}
sz[u] += sz[v];
lab[v] = u;
}
void simulate () {
while (!pq.empty()) {
pii u = pq.front();
pq.pop();
Join(u.fs, u.se);
}
}
}DSU;
void solution () {
cin >> n >> m;
DSU.init(n);
rep (i, 1, m) {
int u, v;
cin >> u >> v;
DSU.add_edge(u, v);
DSU.simulate();
cout << res <<"\n";
}
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +4
mình có muốn dùng mảng này không?
9 9 3
.........
.........
....###..
...v#....
..###....
...##...v
...##....
.........
v........
1 1
9 1
5 7
return->break
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
47448 KB |
Output is correct |
2 |
Correct |
8 ms |
47464 KB |
Output is correct |
3 |
Correct |
7 ms |
47452 KB |
Output is correct |
4 |
Correct |
7 ms |
47704 KB |
Output is correct |
5 |
Correct |
7 ms |
47452 KB |
Output is correct |
6 |
Correct |
7 ms |
47452 KB |
Output is correct |
7 |
Correct |
8 ms |
47452 KB |
Output is correct |
8 |
Correct |
8 ms |
47484 KB |
Output is correct |
9 |
Correct |
9 ms |
47452 KB |
Output is correct |
10 |
Correct |
7 ms |
47452 KB |
Output is correct |
11 |
Correct |
8 ms |
47580 KB |
Output is correct |
12 |
Correct |
8 ms |
47452 KB |
Output is correct |
13 |
Correct |
7 ms |
47452 KB |
Output is correct |
14 |
Correct |
7 ms |
47468 KB |
Output is correct |
15 |
Correct |
7 ms |
47452 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
8 ms |
47452 KB |
Output is correct |
18 |
Correct |
7 ms |
47556 KB |
Output is correct |
19 |
Correct |
7 ms |
47452 KB |
Output is correct |
20 |
Correct |
7 ms |
47612 KB |
Output is correct |
21 |
Correct |
8 ms |
47448 KB |
Output is correct |
22 |
Correct |
7 ms |
47452 KB |
Output is correct |
23 |
Correct |
8 ms |
47556 KB |
Output is correct |
24 |
Correct |
8 ms |
47452 KB |
Output is correct |
25 |
Correct |
8 ms |
47452 KB |
Output is correct |
26 |
Correct |
8 ms |
47452 KB |
Output is correct |
27 |
Correct |
8 ms |
47452 KB |
Output is correct |
28 |
Correct |
9 ms |
47596 KB |
Output is correct |
29 |
Correct |
7 ms |
47452 KB |
Output is correct |
30 |
Correct |
7 ms |
47452 KB |
Output is correct |
31 |
Correct |
7 ms |
47448 KB |
Output is correct |
32 |
Correct |
8 ms |
47452 KB |
Output is correct |
33 |
Correct |
7 ms |
47452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
47448 KB |
Output is correct |
2 |
Correct |
8 ms |
47464 KB |
Output is correct |
3 |
Correct |
7 ms |
47452 KB |
Output is correct |
4 |
Correct |
7 ms |
47704 KB |
Output is correct |
5 |
Correct |
7 ms |
47452 KB |
Output is correct |
6 |
Correct |
7 ms |
47452 KB |
Output is correct |
7 |
Correct |
8 ms |
47452 KB |
Output is correct |
8 |
Correct |
8 ms |
47484 KB |
Output is correct |
9 |
Correct |
9 ms |
47452 KB |
Output is correct |
10 |
Correct |
7 ms |
47452 KB |
Output is correct |
11 |
Correct |
8 ms |
47580 KB |
Output is correct |
12 |
Correct |
8 ms |
47452 KB |
Output is correct |
13 |
Correct |
7 ms |
47452 KB |
Output is correct |
14 |
Correct |
7 ms |
47468 KB |
Output is correct |
15 |
Correct |
7 ms |
47452 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
8 ms |
47452 KB |
Output is correct |
18 |
Correct |
7 ms |
47556 KB |
Output is correct |
19 |
Correct |
7 ms |
47452 KB |
Output is correct |
20 |
Correct |
7 ms |
47612 KB |
Output is correct |
21 |
Correct |
8 ms |
47448 KB |
Output is correct |
22 |
Correct |
7 ms |
47452 KB |
Output is correct |
23 |
Correct |
8 ms |
47556 KB |
Output is correct |
24 |
Correct |
8 ms |
47452 KB |
Output is correct |
25 |
Correct |
8 ms |
47452 KB |
Output is correct |
26 |
Correct |
8 ms |
47452 KB |
Output is correct |
27 |
Correct |
8 ms |
47452 KB |
Output is correct |
28 |
Correct |
9 ms |
47596 KB |
Output is correct |
29 |
Correct |
7 ms |
47452 KB |
Output is correct |
30 |
Correct |
7 ms |
47452 KB |
Output is correct |
31 |
Correct |
7 ms |
47448 KB |
Output is correct |
32 |
Correct |
8 ms |
47452 KB |
Output is correct |
33 |
Correct |
7 ms |
47452 KB |
Output is correct |
34 |
Correct |
9 ms |
47704 KB |
Output is correct |
35 |
Correct |
68 ms |
54596 KB |
Output is correct |
36 |
Correct |
124 ms |
59476 KB |
Output is correct |
37 |
Correct |
108 ms |
59616 KB |
Output is correct |
38 |
Correct |
102 ms |
58828 KB |
Output is correct |
39 |
Correct |
16 ms |
48476 KB |
Output is correct |
40 |
Correct |
18 ms |
49868 KB |
Output is correct |
41 |
Correct |
19 ms |
49608 KB |
Output is correct |
42 |
Correct |
12 ms |
48476 KB |
Output is correct |
43 |
Correct |
14 ms |
48472 KB |
Output is correct |
44 |
Correct |
13 ms |
48476 KB |
Output is correct |
45 |
Correct |
12 ms |
48604 KB |
Output is correct |
46 |
Correct |
13 ms |
48476 KB |
Output is correct |
47 |
Correct |
17 ms |
49240 KB |
Output is correct |
48 |
Correct |
17 ms |
49244 KB |
Output is correct |
49 |
Correct |
24 ms |
50264 KB |
Output is correct |
50 |
Correct |
110 ms |
59992 KB |
Output is correct |
51 |
Correct |
18 ms |
49244 KB |
Output is correct |
52 |
Correct |
106 ms |
56656 KB |
Output is correct |
53 |
Correct |
23 ms |
48076 KB |
Output is correct |
54 |
Correct |
87 ms |
54100 KB |
Output is correct |
55 |
Correct |
15 ms |
47196 KB |
Output is correct |
56 |
Correct |
15 ms |
47196 KB |
Output is correct |
57 |
Correct |
18 ms |
47196 KB |
Output is correct |
58 |
Correct |
16 ms |
47100 KB |
Output is correct |
59 |
Correct |
17 ms |
45912 KB |
Output is correct |
60 |
Correct |
74 ms |
52820 KB |
Output is correct |
61 |
Correct |
15 ms |
45148 KB |
Output is correct |
62 |
Correct |
108 ms |
56484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
47448 KB |
Output is correct |
2 |
Correct |
8 ms |
47464 KB |
Output is correct |
3 |
Correct |
7 ms |
47452 KB |
Output is correct |
4 |
Correct |
7 ms |
47704 KB |
Output is correct |
5 |
Correct |
7 ms |
47452 KB |
Output is correct |
6 |
Correct |
7 ms |
47452 KB |
Output is correct |
7 |
Correct |
8 ms |
47452 KB |
Output is correct |
8 |
Correct |
8 ms |
47484 KB |
Output is correct |
9 |
Correct |
9 ms |
47452 KB |
Output is correct |
10 |
Correct |
7 ms |
47452 KB |
Output is correct |
11 |
Correct |
8 ms |
47580 KB |
Output is correct |
12 |
Correct |
8 ms |
47452 KB |
Output is correct |
13 |
Correct |
7 ms |
47452 KB |
Output is correct |
14 |
Correct |
7 ms |
47468 KB |
Output is correct |
15 |
Correct |
7 ms |
47452 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
8 ms |
47452 KB |
Output is correct |
18 |
Correct |
7 ms |
47556 KB |
Output is correct |
19 |
Correct |
7 ms |
47452 KB |
Output is correct |
20 |
Correct |
7 ms |
47612 KB |
Output is correct |
21 |
Correct |
8 ms |
47448 KB |
Output is correct |
22 |
Correct |
7 ms |
47452 KB |
Output is correct |
23 |
Correct |
8 ms |
47556 KB |
Output is correct |
24 |
Correct |
8 ms |
47452 KB |
Output is correct |
25 |
Correct |
8 ms |
47452 KB |
Output is correct |
26 |
Correct |
8 ms |
47452 KB |
Output is correct |
27 |
Correct |
8 ms |
47452 KB |
Output is correct |
28 |
Correct |
9 ms |
47596 KB |
Output is correct |
29 |
Correct |
7 ms |
47452 KB |
Output is correct |
30 |
Correct |
7 ms |
47452 KB |
Output is correct |
31 |
Correct |
7 ms |
47448 KB |
Output is correct |
32 |
Correct |
8 ms |
47452 KB |
Output is correct |
33 |
Correct |
7 ms |
47452 KB |
Output is correct |
34 |
Correct |
9 ms |
47704 KB |
Output is correct |
35 |
Correct |
68 ms |
54596 KB |
Output is correct |
36 |
Correct |
124 ms |
59476 KB |
Output is correct |
37 |
Correct |
108 ms |
59616 KB |
Output is correct |
38 |
Correct |
102 ms |
58828 KB |
Output is correct |
39 |
Correct |
16 ms |
48476 KB |
Output is correct |
40 |
Correct |
18 ms |
49868 KB |
Output is correct |
41 |
Correct |
19 ms |
49608 KB |
Output is correct |
42 |
Correct |
12 ms |
48476 KB |
Output is correct |
43 |
Correct |
14 ms |
48472 KB |
Output is correct |
44 |
Correct |
13 ms |
48476 KB |
Output is correct |
45 |
Correct |
12 ms |
48604 KB |
Output is correct |
46 |
Correct |
13 ms |
48476 KB |
Output is correct |
47 |
Correct |
17 ms |
49240 KB |
Output is correct |
48 |
Correct |
17 ms |
49244 KB |
Output is correct |
49 |
Correct |
24 ms |
50264 KB |
Output is correct |
50 |
Correct |
110 ms |
59992 KB |
Output is correct |
51 |
Correct |
18 ms |
49244 KB |
Output is correct |
52 |
Correct |
106 ms |
56656 KB |
Output is correct |
53 |
Correct |
23 ms |
48076 KB |
Output is correct |
54 |
Correct |
87 ms |
54100 KB |
Output is correct |
55 |
Correct |
15 ms |
47196 KB |
Output is correct |
56 |
Correct |
15 ms |
47196 KB |
Output is correct |
57 |
Correct |
18 ms |
47196 KB |
Output is correct |
58 |
Correct |
16 ms |
47100 KB |
Output is correct |
59 |
Correct |
17 ms |
45912 KB |
Output is correct |
60 |
Correct |
74 ms |
52820 KB |
Output is correct |
61 |
Correct |
15 ms |
45148 KB |
Output is correct |
62 |
Correct |
108 ms |
56484 KB |
Output is correct |
63 |
Correct |
656 ms |
132272 KB |
Output is correct |
64 |
Correct |
677 ms |
130900 KB |
Output is correct |
65 |
Correct |
746 ms |
132180 KB |
Output is correct |
66 |
Correct |
464 ms |
104528 KB |
Output is correct |
67 |
Correct |
1417 ms |
189880 KB |
Output is correct |
68 |
Correct |
504 ms |
103252 KB |
Output is correct |
69 |
Correct |
542 ms |
103248 KB |
Output is correct |
70 |
Correct |
560 ms |
104692 KB |
Output is correct |
71 |
Correct |
464 ms |
103248 KB |
Output is correct |
72 |
Correct |
1062 ms |
138524 KB |
Output is correct |
73 |
Correct |
1070 ms |
144060 KB |
Output is correct |
74 |
Correct |
1970 ms |
187224 KB |
Output is correct |
75 |
Correct |
857 ms |
116304 KB |
Output is correct |
76 |
Correct |
1350 ms |
148304 KB |
Output is correct |
77 |
Correct |
1470 ms |
148608 KB |
Output is correct |
78 |
Correct |
373 ms |
99060 KB |
Output is correct |
79 |
Correct |
707 ms |
118056 KB |
Output is correct |
80 |
Correct |
439 ms |
96076 KB |
Output is correct |
81 |
Correct |
642 ms |
111184 KB |
Output is correct |
82 |
Correct |
1164 ms |
139344 KB |
Output is correct |
83 |
Correct |
1167 ms |
139432 KB |
Output is correct |
84 |
Correct |
999 ms |
139444 KB |
Output is correct |
85 |
Correct |
996 ms |
139340 KB |
Output is correct |
86 |
Correct |
1332 ms |
218452 KB |
Output is correct |
87 |
Correct |
1373 ms |
220752 KB |
Output is correct |
88 |
Correct |
1085 ms |
144136 KB |
Output is correct |
89 |
Correct |
1313 ms |
147168 KB |
Output is correct |