Submission #1020553

# Submission time Handle Problem Language Result Execution time Memory
1020553 2024-07-12T06:55:17 Z vjudge1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
719 ms 69016 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int cmp[100001];
ll sz[100001], ans = 0;
set<int> child[100001], graph[100001], rgraph[100001];
queue<pair<int, int>> to_merge;

void insert_weak_connection(int A, int B) {
	graph[A].insert(B);
	rgraph[B].insert(A);
	// If there's A new strong connection between A's and B's components, merge
	// them
	if (graph[B].count(A)) to_merge.push({A, B});
}

int dsu_size(int A) {
	return child[A].size() + graph[A].size() + rgraph[A].size();
}

int find(int A) { return (A == cmp[A] ? A : cmp[A] = find(cmp[A])); }

void onion(int A, int B) {
	if (A == B) return;
	// Merge the smaller component into the larger
	if (dsu_size(A) < dsu_size(B)) swap(A, B);

	// Add new contribution
	ans += sz[B] * child[A].size() + sz[A] * child[B].size();

	// DSU stuff
	cmp[B] = A;
	sz[A] += sz[B];

	// Merge children of B into A
	for (int i : child[B]) {
		if (child[A].count(i)) ans -= sz[A];
		else child[A].insert(i);
	}
	// Erase the connections
	graph[A].erase(B), rgraph[A].erase(B);
	graph[B].erase(A), rgraph[B].erase(A);
	// Merge the weak connections to other components
	for (int i : graph[B]) {
		rgraph[i].erase(B);
		insert_weak_connection(A, i);
	}
	for (int i : rgraph[B]) {
		graph[i].erase(B);
		insert_weak_connection(i, A);
	}
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cmp[i] = i;
		sz[i] = 1;
		child[i].insert(i);
	}
	while (m--) {
		int A, B;
		cin >> A >> B;

		B = find(B);
		// If A isn't in B's component and doesn't already follow someone in B's
		// component...
		if (find(A) != B && !child[B].count(A)) {
			// We insert A as A follower of B and add sz[find(B)] to the answer
			child[B].insert(A);
			ans += sz[B];

			A = find(A);
			// Add connections between components
			insert_weak_connection(A, B);
			// We may have to merge multiple components for each new event
			while (to_merge.size()) {
				tie(A, B) = to_merge.front();
				to_merge.pop();
				onion(find(A), find(B));
			}
		}
		cout << ans << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 4 ms 14940 KB Output is correct
3 Correct 5 ms 14936 KB Output is correct
4 Correct 4 ms 14944 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 5 ms 14992 KB Output is correct
7 Correct 5 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 5 ms 14812 KB Output is correct
13 Correct 4 ms 14924 KB Output is correct
14 Correct 5 ms 14936 KB Output is correct
15 Correct 5 ms 15000 KB Output is correct
16 Correct 5 ms 14776 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 4 ms 14936 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 5 ms 14984 KB Output is correct
25 Correct 5 ms 14940 KB Output is correct
26 Correct 5 ms 14940 KB Output is correct
27 Correct 6 ms 14940 KB Output is correct
28 Correct 5 ms 14936 KB Output is correct
29 Correct 4 ms 14776 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 5 ms 14940 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 4 ms 14940 KB Output is correct
3 Correct 5 ms 14936 KB Output is correct
4 Correct 4 ms 14944 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 5 ms 14992 KB Output is correct
7 Correct 5 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 5 ms 14812 KB Output is correct
13 Correct 4 ms 14924 KB Output is correct
14 Correct 5 ms 14936 KB Output is correct
15 Correct 5 ms 15000 KB Output is correct
16 Correct 5 ms 14776 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 4 ms 14936 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 5 ms 14984 KB Output is correct
25 Correct 5 ms 14940 KB Output is correct
26 Correct 5 ms 14940 KB Output is correct
27 Correct 6 ms 14940 KB Output is correct
28 Correct 5 ms 14936 KB Output is correct
29 Correct 4 ms 14776 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 5 ms 14940 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 4 ms 14940 KB Output is correct
34 Correct 6 ms 14936 KB Output is correct
35 Correct 55 ms 20816 KB Output is correct
36 Correct 98 ms 23632 KB Output is correct
37 Correct 74 ms 23544 KB Output is correct
38 Correct 77 ms 23168 KB Output is correct
39 Correct 6 ms 15192 KB Output is correct
40 Correct 8 ms 15708 KB Output is correct
41 Correct 8 ms 15708 KB Output is correct
42 Correct 7 ms 15196 KB Output is correct
43 Correct 6 ms 15196 KB Output is correct
44 Correct 7 ms 15196 KB Output is correct
45 Correct 7 ms 15196 KB Output is correct
46 Correct 6 ms 15196 KB Output is correct
47 Correct 7 ms 15452 KB Output is correct
48 Correct 7 ms 15452 KB Output is correct
49 Correct 11 ms 16236 KB Output is correct
50 Correct 74 ms 23776 KB Output is correct
51 Correct 10 ms 15452 KB Output is correct
52 Correct 72 ms 22008 KB Output is correct
53 Correct 13 ms 16028 KB Output is correct
54 Correct 71 ms 22732 KB Output is correct
55 Correct 9 ms 15704 KB Output is correct
56 Correct 8 ms 15708 KB Output is correct
57 Correct 8 ms 15708 KB Output is correct
58 Correct 8 ms 15556 KB Output is correct
59 Correct 7 ms 15708 KB Output is correct
60 Correct 53 ms 20656 KB Output is correct
61 Correct 8 ms 15452 KB Output is correct
62 Correct 75 ms 22956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 4 ms 14940 KB Output is correct
3 Correct 5 ms 14936 KB Output is correct
4 Correct 4 ms 14944 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 5 ms 14992 KB Output is correct
7 Correct 5 ms 14940 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14940 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 5 ms 14812 KB Output is correct
13 Correct 4 ms 14924 KB Output is correct
14 Correct 5 ms 14936 KB Output is correct
15 Correct 5 ms 15000 KB Output is correct
16 Correct 5 ms 14776 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 4 ms 14936 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 5 ms 14940 KB Output is correct
24 Correct 5 ms 14984 KB Output is correct
25 Correct 5 ms 14940 KB Output is correct
26 Correct 5 ms 14940 KB Output is correct
27 Correct 6 ms 14940 KB Output is correct
28 Correct 5 ms 14936 KB Output is correct
29 Correct 4 ms 14776 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 5 ms 14940 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 4 ms 14940 KB Output is correct
34 Correct 6 ms 14936 KB Output is correct
35 Correct 55 ms 20816 KB Output is correct
36 Correct 98 ms 23632 KB Output is correct
37 Correct 74 ms 23544 KB Output is correct
38 Correct 77 ms 23168 KB Output is correct
39 Correct 6 ms 15192 KB Output is correct
40 Correct 8 ms 15708 KB Output is correct
41 Correct 8 ms 15708 KB Output is correct
42 Correct 7 ms 15196 KB Output is correct
43 Correct 6 ms 15196 KB Output is correct
44 Correct 7 ms 15196 KB Output is correct
45 Correct 7 ms 15196 KB Output is correct
46 Correct 6 ms 15196 KB Output is correct
47 Correct 7 ms 15452 KB Output is correct
48 Correct 7 ms 15452 KB Output is correct
49 Correct 11 ms 16236 KB Output is correct
50 Correct 74 ms 23776 KB Output is correct
51 Correct 10 ms 15452 KB Output is correct
52 Correct 72 ms 22008 KB Output is correct
53 Correct 13 ms 16028 KB Output is correct
54 Correct 71 ms 22732 KB Output is correct
55 Correct 9 ms 15704 KB Output is correct
56 Correct 8 ms 15708 KB Output is correct
57 Correct 8 ms 15708 KB Output is correct
58 Correct 8 ms 15556 KB Output is correct
59 Correct 7 ms 15708 KB Output is correct
60 Correct 53 ms 20656 KB Output is correct
61 Correct 8 ms 15452 KB Output is correct
62 Correct 75 ms 22956 KB Output is correct
63 Correct 382 ms 67920 KB Output is correct
64 Correct 417 ms 67992 KB Output is correct
65 Correct 400 ms 67924 KB Output is correct
66 Correct 154 ms 34020 KB Output is correct
67 Correct 293 ms 61076 KB Output is correct
68 Correct 146 ms 34100 KB Output is correct
69 Correct 287 ms 35164 KB Output is correct
70 Correct 173 ms 34132 KB Output is correct
71 Correct 155 ms 34132 KB Output is correct
72 Correct 328 ms 45112 KB Output is correct
73 Correct 290 ms 46660 KB Output is correct
74 Correct 681 ms 68788 KB Output is correct
75 Correct 431 ms 42004 KB Output is correct
76 Correct 523 ms 54096 KB Output is correct
77 Correct 513 ms 54096 KB Output is correct
78 Correct 162 ms 37712 KB Output is correct
79 Correct 322 ms 42064 KB Output is correct
80 Correct 152 ms 36688 KB Output is correct
81 Correct 266 ms 40104 KB Output is correct
82 Correct 719 ms 54932 KB Output is correct
83 Correct 530 ms 54536 KB Output is correct
84 Correct 433 ms 53332 KB Output is correct
85 Correct 456 ms 53328 KB Output is correct
86 Correct 250 ms 66900 KB Output is correct
87 Correct 248 ms 69016 KB Output is correct
88 Correct 331 ms 45908 KB Output is correct
89 Correct 527 ms 52052 KB Output is correct