Submission #766875

# Submission time Handle Problem Language Result Execution time Memory
766875 2023-06-26T08:27:05 Z onjo0127 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1287 ms 160660 KB
#include <bits/stdc++.h>
#define sz(V) ((int)V.size())
using namespace std;
using pii = pair<int, int>;
using ll = long long;

int P[100009];
set<int> I[100009], A[100009], II[100009], OO[100009];
ll ans;

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

ll f(int x) {
	return 1LL * sz(A[x]) * (sz(I[x]) + sz(A[x]) - 1);
}

void merg(int u, int v) {
	u = root(u); v = root(v);
	if(u == v) return;
	if(sz(A[u]) + sz(I[u]) + sz(II[u]) + sz(OO[u]) > sz(A[v]) + sz(I[v]) + sz(II[v]) + sz(OO[v])) swap(u, v);
	ans -= f(u) + f(v);
	for(auto& it: A[u]) if(I[v].find(it) != I[v].end()) I[v].erase(it);
	for(auto& it: I[u]) if(A[v].find(it) == A[v].end()) I[v].insert(it);
	for(auto& it: A[u]) A[v].insert(it);
	set<int> Q;
	for(auto& it: II[u]) if(it != u && it != v) {
		OO[it].erase(u);
		OO[it].insert(v);
		if(OO[v].find(it) != OO[v].end()) Q.insert(it);
		II[v].insert(it);
	}
	for(auto& it: OO[u]) if(it != u && it != v) {
		II[it].erase(u);
		II[it].insert(v);
		if(II[v].find(it) != II[v].end()) Q.insert(it);
		OO[v].insert(it);
	}
	ans += f(v);
	P[u] = v;
	for(auto& it: Q) merg(v, it);
}

int main() {
	int N, M; scanf("%d%d", &N, &M);
	for(int i=1; i<=N; i++) {
		P[i] = i;
		A[i] = {i};
	}
	while(M--) {
		int u, v; scanf("%d%d", &u, &v);
		int U = root(u), V = root(v);
		if(U != V) {
			ans -= f(V);
			I[V].insert(u);
			II[V].insert(U);
			OO[U].insert(V);
			ans += f(V);
			if(II[U].find(V) != II[U].end()) merg(U, V);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:47:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  int N, M; scanf("%d%d", &N, &M);
      |            ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:53:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |   int u, v; scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19024 KB Output is correct
2 Correct 8 ms 19028 KB Output is correct
3 Correct 8 ms 19028 KB Output is correct
4 Correct 8 ms 19100 KB Output is correct
5 Correct 8 ms 19092 KB Output is correct
6 Correct 8 ms 19096 KB Output is correct
7 Correct 8 ms 19108 KB Output is correct
8 Correct 9 ms 19156 KB Output is correct
9 Correct 9 ms 19152 KB Output is correct
10 Correct 8 ms 19100 KB Output is correct
11 Correct 8 ms 19028 KB Output is correct
12 Correct 10 ms 19076 KB Output is correct
13 Correct 8 ms 19000 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 8 ms 19028 KB Output is correct
16 Correct 9 ms 19100 KB Output is correct
17 Correct 9 ms 19028 KB Output is correct
18 Correct 9 ms 19104 KB Output is correct
19 Correct 9 ms 19028 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 9 ms 19156 KB Output is correct
22 Correct 9 ms 19052 KB Output is correct
23 Correct 9 ms 19028 KB Output is correct
24 Correct 8 ms 19028 KB Output is correct
25 Correct 9 ms 19152 KB Output is correct
26 Correct 8 ms 19100 KB Output is correct
27 Correct 8 ms 19104 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
29 Correct 9 ms 19028 KB Output is correct
30 Correct 9 ms 19028 KB Output is correct
31 Correct 10 ms 19104 KB Output is correct
32 Correct 9 ms 19028 KB Output is correct
33 Correct 9 ms 19068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19024 KB Output is correct
2 Correct 8 ms 19028 KB Output is correct
3 Correct 8 ms 19028 KB Output is correct
4 Correct 8 ms 19100 KB Output is correct
5 Correct 8 ms 19092 KB Output is correct
6 Correct 8 ms 19096 KB Output is correct
7 Correct 8 ms 19108 KB Output is correct
8 Correct 9 ms 19156 KB Output is correct
9 Correct 9 ms 19152 KB Output is correct
10 Correct 8 ms 19100 KB Output is correct
11 Correct 8 ms 19028 KB Output is correct
12 Correct 10 ms 19076 KB Output is correct
13 Correct 8 ms 19000 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 8 ms 19028 KB Output is correct
16 Correct 9 ms 19100 KB Output is correct
17 Correct 9 ms 19028 KB Output is correct
18 Correct 9 ms 19104 KB Output is correct
19 Correct 9 ms 19028 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 9 ms 19156 KB Output is correct
22 Correct 9 ms 19052 KB Output is correct
23 Correct 9 ms 19028 KB Output is correct
24 Correct 8 ms 19028 KB Output is correct
25 Correct 9 ms 19152 KB Output is correct
26 Correct 8 ms 19100 KB Output is correct
27 Correct 8 ms 19104 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
29 Correct 9 ms 19028 KB Output is correct
30 Correct 9 ms 19028 KB Output is correct
31 Correct 10 ms 19104 KB Output is correct
32 Correct 9 ms 19028 KB Output is correct
33 Correct 9 ms 19068 KB Output is correct
34 Correct 10 ms 19216 KB Output is correct
35 Correct 80 ms 25024 KB Output is correct
36 Correct 99 ms 28040 KB Output is correct
37 Correct 103 ms 28192 KB Output is correct
38 Correct 99 ms 27748 KB Output is correct
39 Correct 12 ms 19752 KB Output is correct
40 Correct 17 ms 20820 KB Output is correct
41 Correct 16 ms 20648 KB Output is correct
42 Correct 11 ms 19800 KB Output is correct
43 Correct 13 ms 19816 KB Output is correct
44 Correct 12 ms 19776 KB Output is correct
45 Correct 12 ms 19756 KB Output is correct
46 Correct 12 ms 19716 KB Output is correct
47 Correct 14 ms 20212 KB Output is correct
48 Correct 17 ms 20328 KB Output is correct
49 Correct 19 ms 20904 KB Output is correct
50 Correct 104 ms 28284 KB Output is correct
51 Correct 15 ms 20000 KB Output is correct
52 Correct 91 ms 26568 KB Output is correct
53 Correct 18 ms 20640 KB Output is correct
54 Correct 94 ms 27332 KB Output is correct
55 Correct 13 ms 20052 KB Output is correct
56 Correct 13 ms 20052 KB Output is correct
57 Correct 15 ms 20420 KB Output is correct
58 Correct 14 ms 20436 KB Output is correct
59 Correct 17 ms 21076 KB Output is correct
60 Correct 84 ms 25964 KB Output is correct
61 Correct 15 ms 20128 KB Output is correct
62 Correct 99 ms 27692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19024 KB Output is correct
2 Correct 8 ms 19028 KB Output is correct
3 Correct 8 ms 19028 KB Output is correct
4 Correct 8 ms 19100 KB Output is correct
5 Correct 8 ms 19092 KB Output is correct
6 Correct 8 ms 19096 KB Output is correct
7 Correct 8 ms 19108 KB Output is correct
8 Correct 9 ms 19156 KB Output is correct
9 Correct 9 ms 19152 KB Output is correct
10 Correct 8 ms 19100 KB Output is correct
11 Correct 8 ms 19028 KB Output is correct
12 Correct 10 ms 19076 KB Output is correct
13 Correct 8 ms 19000 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 8 ms 19028 KB Output is correct
16 Correct 9 ms 19100 KB Output is correct
17 Correct 9 ms 19028 KB Output is correct
18 Correct 9 ms 19104 KB Output is correct
19 Correct 9 ms 19028 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 9 ms 19156 KB Output is correct
22 Correct 9 ms 19052 KB Output is correct
23 Correct 9 ms 19028 KB Output is correct
24 Correct 8 ms 19028 KB Output is correct
25 Correct 9 ms 19152 KB Output is correct
26 Correct 8 ms 19100 KB Output is correct
27 Correct 8 ms 19104 KB Output is correct
28 Correct 9 ms 19104 KB Output is correct
29 Correct 9 ms 19028 KB Output is correct
30 Correct 9 ms 19028 KB Output is correct
31 Correct 10 ms 19104 KB Output is correct
32 Correct 9 ms 19028 KB Output is correct
33 Correct 9 ms 19068 KB Output is correct
34 Correct 10 ms 19216 KB Output is correct
35 Correct 80 ms 25024 KB Output is correct
36 Correct 99 ms 28040 KB Output is correct
37 Correct 103 ms 28192 KB Output is correct
38 Correct 99 ms 27748 KB Output is correct
39 Correct 12 ms 19752 KB Output is correct
40 Correct 17 ms 20820 KB Output is correct
41 Correct 16 ms 20648 KB Output is correct
42 Correct 11 ms 19800 KB Output is correct
43 Correct 13 ms 19816 KB Output is correct
44 Correct 12 ms 19776 KB Output is correct
45 Correct 12 ms 19756 KB Output is correct
46 Correct 12 ms 19716 KB Output is correct
47 Correct 14 ms 20212 KB Output is correct
48 Correct 17 ms 20328 KB Output is correct
49 Correct 19 ms 20904 KB Output is correct
50 Correct 104 ms 28284 KB Output is correct
51 Correct 15 ms 20000 KB Output is correct
52 Correct 91 ms 26568 KB Output is correct
53 Correct 18 ms 20640 KB Output is correct
54 Correct 94 ms 27332 KB Output is correct
55 Correct 13 ms 20052 KB Output is correct
56 Correct 13 ms 20052 KB Output is correct
57 Correct 15 ms 20420 KB Output is correct
58 Correct 14 ms 20436 KB Output is correct
59 Correct 17 ms 21076 KB Output is correct
60 Correct 84 ms 25964 KB Output is correct
61 Correct 15 ms 20128 KB Output is correct
62 Correct 99 ms 27692 KB Output is correct
63 Correct 487 ms 71796 KB Output is correct
64 Correct 454 ms 71756 KB Output is correct
65 Correct 514 ms 71876 KB Output is correct
66 Correct 287 ms 56756 KB Output is correct
67 Correct 1159 ms 130892 KB Output is correct
68 Correct 290 ms 56708 KB Output is correct
69 Correct 434 ms 56612 KB Output is correct
70 Correct 339 ms 56720 KB Output is correct
71 Correct 319 ms 56648 KB Output is correct
72 Correct 802 ms 82380 KB Output is correct
73 Correct 856 ms 87180 KB Output is correct
74 Correct 1287 ms 110732 KB Output is correct
75 Correct 629 ms 62924 KB Output is correct
76 Correct 853 ms 80944 KB Output is correct
77 Correct 829 ms 81100 KB Output is correct
78 Correct 263 ms 51212 KB Output is correct
79 Correct 424 ms 62144 KB Output is correct
80 Correct 254 ms 50036 KB Output is correct
81 Correct 504 ms 59272 KB Output is correct
82 Correct 676 ms 69940 KB Output is correct
83 Correct 626 ms 69972 KB Output is correct
84 Correct 629 ms 91944 KB Output is correct
85 Correct 662 ms 91972 KB Output is correct
86 Correct 1100 ms 158288 KB Output is correct
87 Correct 1140 ms 160660 KB Output is correct
88 Correct 800 ms 84760 KB Output is correct
89 Correct 911 ms 78256 KB Output is correct