Submission #1059153

# Submission time Handle Problem Language Result Execution time Memory
1059153 2024-08-14T17:55:49 Z 0npata Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1197 ms 130252 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define vec vector


struct DSU {
	vec<int> par;
	vec<int> sz;
	vec<map<int, set<int>>> outc;
	vec<map<int, set<int>>> inc;
	vec<pair<int, int>> to_merge;
	vec<int> total_inc_size;
	int ans = 0;

	DSU(int n) {
		par = vec<int>(n);
		iota(par.begin(), par.end(), 0);
		sz = vec<int>(n, 1);
		inc = vec<map<int, set<int>>>(n);
		outc = vec<map<int, set<int>>>(n);
		total_inc_size = vec<int>(n);
		to_merge = {};
	}

	int root(int x) {
		if(par[x] == x) return x;
		par[x] = root(par[x]);
		return par[x];
	}

	bool join(int x, int y) {
		x = root(x);
		y = root(y);
		if(x==y) return false;
		//cerr << "MERGING: " << x << ' ' << y << '\n';

		if(sz[x] < sz[y]) swap(x, y);

		ans -= total_inc_size[x] * sz[x];
		ans -= total_inc_size[y] * sz[y];
		total_inc_size[x] -= inc[x][y].size();
		total_inc_size[y] = 0;
		inc[x].erase(y);
		inc[y].erase(x);
		outc[x].erase(y);
		outc[y].erase(x);
		ans -= sz[x]*(sz[x]-1);
		ans -= sz[y]*(sz[y]-1);

		for(auto [c, _] : outc[y]) {
			for(int vertex : inc[c][y]) {
				inc[c][x].insert(vertex);
			}
			inc[c].erase(y);
		}
		for(auto [c, _] : inc[y]) {
			for(int vertex : outc[c][y]) {
				outc[c][x].insert(vertex);
			}
			outc[c].erase(y);
		}

		for(auto [c, vertices] : outc[y]) {
			for(int v : vertices) {
				outc[x][c].insert(v);
				//assert(root(v) == c);
				if(inc[x][c].size() > 0) {
					to_merge.push_back({x, c});
				}
			}
		}
		for(auto [c, vertices] : inc[y]) {
			for(int v : vertices) {
				total_inc_size[x] -= inc[x][c].size();
				inc[x][c].insert(v);
				assert(root(v) == c);
				if(outc[x][c].size() > 0) {
					to_merge.push_back({x, v});
				}
				total_inc_size[x] += inc[x][c].size();
			}
		}

		inc[y] = {};
		outc[y] = {};

		par[y] = x;
		sz[x] += sz[y];
		sz[y] = 0;

		ans += total_inc_size[x] * sz[x];
		ans += sz[x]*(sz[x]-1);

		return true;
	}

	void merge_all() {
		while(to_merge.size() > 0) {
			auto [x, y] = to_merge.back();
			to_merge.pop_back();
			join(x, y);
		}
	}

	void make_edge(int u, int v) {

		if(root(u) == root(v)) return;
		ans -= total_inc_size[root(v)] * sz[root(v)];

		outc[root(u)][root(v)].insert(u);

		total_inc_size[root(v)] -= inc[root(v)][root(u)].size();
		inc[root(v)][root(u)].insert(u);
		total_inc_size[root(v)] += inc[root(v)][root(u)].size();

		ans += total_inc_size[root(v)] * sz[root(v)];
		if(outc[root(u)][root(v)].size() > 0 && inc[root(u)][root(v)].size() > 0) {
			to_merge.push_back({u, v});
			merge_all();
		}
	}
};

const int MXN = 100'005;


int32_t main() {
	int N, M;
	cin >> N >> M;

	DSU dsu(N);

	while(M--) {
		int u, v;
		cin >> u >> v;
		u--;v--;

		dsu.make_edge(u, v);

		cout << dsu.ans << '\n';
	}

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 12 ms 604 KB Output is correct
35 Correct 364 ms 6284 KB Output is correct
36 Correct 441 ms 14496 KB Output is correct
37 Correct 425 ms 15112 KB Output is correct
38 Correct 402 ms 14032 KB Output is correct
39 Correct 6 ms 604 KB Output is correct
40 Correct 7 ms 940 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 604 KB Output is correct
43 Correct 9 ms 1112 KB Output is correct
44 Correct 7 ms 1112 KB Output is correct
45 Correct 8 ms 704 KB Output is correct
46 Correct 6 ms 732 KB Output is correct
47 Correct 8 ms 924 KB Output is correct
48 Correct 8 ms 1116 KB Output is correct
49 Correct 22 ms 2628 KB Output is correct
50 Correct 421 ms 15168 KB Output is correct
51 Correct 17 ms 1628 KB Output is correct
52 Correct 375 ms 10836 KB Output is correct
53 Correct 28 ms 2704 KB Output is correct
54 Correct 393 ms 12916 KB Output is correct
55 Correct 9 ms 2140 KB Output is correct
56 Correct 9 ms 2040 KB Output is correct
57 Correct 10 ms 2192 KB Output is correct
58 Correct 10 ms 2140 KB Output is correct
59 Correct 6 ms 736 KB Output is correct
60 Correct 368 ms 5588 KB Output is correct
61 Correct 10 ms 1240 KB Output is correct
62 Correct 419 ms 13552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 12 ms 604 KB Output is correct
35 Correct 364 ms 6284 KB Output is correct
36 Correct 441 ms 14496 KB Output is correct
37 Correct 425 ms 15112 KB Output is correct
38 Correct 402 ms 14032 KB Output is correct
39 Correct 6 ms 604 KB Output is correct
40 Correct 7 ms 940 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 604 KB Output is correct
43 Correct 9 ms 1112 KB Output is correct
44 Correct 7 ms 1112 KB Output is correct
45 Correct 8 ms 704 KB Output is correct
46 Correct 6 ms 732 KB Output is correct
47 Correct 8 ms 924 KB Output is correct
48 Correct 8 ms 1116 KB Output is correct
49 Correct 22 ms 2628 KB Output is correct
50 Correct 421 ms 15168 KB Output is correct
51 Correct 17 ms 1628 KB Output is correct
52 Correct 375 ms 10836 KB Output is correct
53 Correct 28 ms 2704 KB Output is correct
54 Correct 393 ms 12916 KB Output is correct
55 Correct 9 ms 2140 KB Output is correct
56 Correct 9 ms 2040 KB Output is correct
57 Correct 10 ms 2192 KB Output is correct
58 Correct 10 ms 2140 KB Output is correct
59 Correct 6 ms 736 KB Output is correct
60 Correct 368 ms 5588 KB Output is correct
61 Correct 10 ms 1240 KB Output is correct
62 Correct 419 ms 13552 KB Output is correct
63 Correct 925 ms 130140 KB Output is correct
64 Correct 858 ms 130168 KB Output is correct
65 Correct 912 ms 130252 KB Output is correct
66 Correct 338 ms 16368 KB Output is correct
67 Correct 539 ms 34912 KB Output is correct
68 Correct 319 ms 16516 KB Output is correct
69 Correct 602 ms 35336 KB Output is correct
70 Correct 324 ms 16468 KB Output is correct
71 Correct 292 ms 16440 KB Output is correct
72 Correct 468 ms 35412 KB Output is correct
73 Correct 457 ms 35368 KB Output is correct
74 Correct 1197 ms 67156 KB Output is correct
75 Correct 725 ms 47440 KB Output is correct
76 Correct 952 ms 63396 KB Output is correct
77 Correct 914 ms 63532 KB Output is correct
78 Correct 392 ms 34408 KB Output is correct
79 Correct 626 ms 37460 KB Output is correct
80 Correct 332 ms 34132 KB Output is correct
81 Correct 553 ms 37452 KB Output is correct
82 Correct 773 ms 90708 KB Output is correct
83 Correct 788 ms 90900 KB Output is correct
84 Correct 692 ms 90924 KB Output is correct
85 Correct 695 ms 90692 KB Output is correct
86 Correct 284 ms 15700 KB Output is correct
87 Correct 407 ms 18004 KB Output is correct
88 Correct 502 ms 35672 KB Output is correct
89 Correct 822 ms 61776 KB Output is correct