Submission #957854

# Submission time Handle Problem Language Result Execution time Memory
957854 2024-04-04T12:15:31 Z Trisanu_Das Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
917 ms 69204 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 (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;
	if (dsu_size(A) < dsu_size(B)) swap(A, B);
	ans += sz[B] * child[A].size() + sz[A] * child[B].size();

	cmp[B] = A;
	sz[A] += sz[B];

	for (int i : child[B]) {
		if (child[A].count(i)) ans -= sz[A];
		else child[A].insert(i);
	}
	graph[A].erase(B), rgraph[A].erase(B);
	graph[B].erase(A), rgraph[B].erase(A);
	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 (find(A) != B && !child[B].count(A)) {
			child[B].insert(A);
			ans += sz[B];
			A = find(A);
			insert_weak_connection(A, B);
			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 3 ms 14940 KB Output is correct
2 Correct 3 ms 14992 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 4 ms 15040 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 15000 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 4 ms 15000 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 15012 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15016 KB Output is correct
23 Correct 4 ms 14940 KB Output is correct
24 Correct 3 ms 15000 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 4 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14992 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 4 ms 15040 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 15000 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 4 ms 15000 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 15012 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15016 KB Output is correct
23 Correct 4 ms 14940 KB Output is correct
24 Correct 3 ms 15000 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 4 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 58 ms 20596 KB Output is correct
36 Correct 79 ms 23632 KB Output is correct
37 Correct 79 ms 23952 KB Output is correct
38 Correct 79 ms 23344 KB Output is correct
39 Correct 5 ms 15196 KB Output is correct
40 Correct 7 ms 15708 KB Output is correct
41 Correct 6 ms 15708 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 6 ms 15332 KB Output is correct
44 Correct 6 ms 15196 KB Output is correct
45 Correct 5 ms 15364 KB Output is correct
46 Correct 5 ms 15196 KB Output is correct
47 Correct 7 ms 15452 KB Output is correct
48 Correct 6 ms 15452 KB Output is correct
49 Correct 11 ms 16216 KB Output is correct
50 Correct 83 ms 23808 KB Output is correct
51 Correct 8 ms 15452 KB Output is correct
52 Correct 68 ms 21844 KB Output is correct
53 Correct 11 ms 15960 KB Output is correct
54 Correct 79 ms 23048 KB Output is correct
55 Correct 13 ms 15704 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 8 ms 15696 KB Output is correct
58 Correct 7 ms 15660 KB Output is correct
59 Correct 7 ms 15708 KB Output is correct
60 Correct 57 ms 20680 KB Output is correct
61 Correct 8 ms 15452 KB Output is correct
62 Correct 74 ms 23148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14940 KB Output is correct
2 Correct 3 ms 14992 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 15004 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 4 ms 15040 KB Output is correct
8 Correct 4 ms 14940 KB Output is correct
9 Correct 4 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 15000 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 4 ms 15000 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 15012 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 4 ms 14940 KB Output is correct
22 Correct 3 ms 15016 KB Output is correct
23 Correct 4 ms 14940 KB Output is correct
24 Correct 3 ms 15000 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 4 ms 14940 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 58 ms 20596 KB Output is correct
36 Correct 79 ms 23632 KB Output is correct
37 Correct 79 ms 23952 KB Output is correct
38 Correct 79 ms 23344 KB Output is correct
39 Correct 5 ms 15196 KB Output is correct
40 Correct 7 ms 15708 KB Output is correct
41 Correct 6 ms 15708 KB Output is correct
42 Correct 5 ms 15196 KB Output is correct
43 Correct 6 ms 15332 KB Output is correct
44 Correct 6 ms 15196 KB Output is correct
45 Correct 5 ms 15364 KB Output is correct
46 Correct 5 ms 15196 KB Output is correct
47 Correct 7 ms 15452 KB Output is correct
48 Correct 6 ms 15452 KB Output is correct
49 Correct 11 ms 16216 KB Output is correct
50 Correct 83 ms 23808 KB Output is correct
51 Correct 8 ms 15452 KB Output is correct
52 Correct 68 ms 21844 KB Output is correct
53 Correct 11 ms 15960 KB Output is correct
54 Correct 79 ms 23048 KB Output is correct
55 Correct 13 ms 15704 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 8 ms 15696 KB Output is correct
58 Correct 7 ms 15660 KB Output is correct
59 Correct 7 ms 15708 KB Output is correct
60 Correct 57 ms 20680 KB Output is correct
61 Correct 8 ms 15452 KB Output is correct
62 Correct 74 ms 23148 KB Output is correct
63 Correct 534 ms 68136 KB Output is correct
64 Correct 491 ms 68128 KB Output is correct
65 Correct 516 ms 68128 KB Output is correct
66 Correct 176 ms 34132 KB Output is correct
67 Correct 386 ms 61056 KB Output is correct
68 Correct 154 ms 34132 KB Output is correct
69 Correct 349 ms 35332 KB Output is correct
70 Correct 188 ms 34216 KB Output is correct
71 Correct 187 ms 34112 KB Output is correct
72 Correct 358 ms 45140 KB Output is correct
73 Correct 371 ms 46856 KB Output is correct
74 Correct 917 ms 68588 KB Output is correct
75 Correct 442 ms 42104 KB Output is correct
76 Correct 624 ms 53876 KB Output is correct
77 Correct 660 ms 54160 KB Output is correct
78 Correct 193 ms 37796 KB Output is correct
79 Correct 370 ms 42036 KB Output is correct
80 Correct 220 ms 36576 KB Output is correct
81 Correct 291 ms 40080 KB Output is correct
82 Correct 677 ms 54792 KB Output is correct
83 Correct 694 ms 54868 KB Output is correct
84 Correct 572 ms 53468 KB Output is correct
85 Correct 494 ms 53332 KB Output is correct
86 Correct 272 ms 66896 KB Output is correct
87 Correct 290 ms 69204 KB Output is correct
88 Correct 402 ms 46140 KB Output is correct
89 Correct 608 ms 51940 KB Output is correct