Submission #911175

# Submission time Handle Problem Language Result Execution time Memory
911175 2024-01-18T14:36:40 Z mickey080929 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
2839 ms 224548 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
 
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

ll par[100010];
ll sz[100010];
set<ll> in[100010], out[100010], all[100010];

ll Find(ll x) {
	if (x == par[x]) return x;
	return par[x] = Find(par[x]);
}

ll ans = 0;
vector<pll> mrg;

void add_edge(ll u, ll v) {
	in[v].insert(u);
	out[u].insert(v);
	if (in[u].count(v)) mrg.push_back({u, v});
}

void Union(ll x, ll y) {
	x = Find(x);
	y = Find(y);
	if (x == y) return;
	if (all[x].size() < all[y].size()) swap(x, y);
	ans += all[x].size() * sz[y] + all[y].size() * sz[x];
	sz[x] += sz[y];
	par[y] = x;
	for (auto &v : all[y]) {
		if (all[x].count(v)) ans -= sz[x];
		else all[x].insert(v);
	}
	//in[y].erase(x); out[y].erase(x); in[x].erase(y); out[x].erase(y);
	for (auto &v : in[y]) {
		add_edge(v, x);
	}
	for (auto &v : out[y]) {
		add_edge(x, v);
	}
}

int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	ll N, M;
	cin >> N >> M;
	for (ll i=1; i<=N; i++) {
		par[i] = i;
		sz[i] = 1;
		all[i].insert(i);
	}
	for (ll i=0; i<M; i++) {
		ll u, v;
		cin >> u >> v;
		v = Find(v);
		if (all[v].count(u)) {
			cout << ans << '\n';
			continue;
		}
		all[v].insert(u);
		ans += sz[v];
		u = Find(u);
		add_edge(u, v);
		while (!mrg.empty()) {
			auto t = mrg.back();
			mrg.pop_back();
			Union(t.first, t.second);
		}
		cout << ans << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14940 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 14996 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 6 ms 14940 KB Output is correct
8 Correct 6 ms 14936 KB Output is correct
9 Correct 5 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 5 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14936 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 4 ms 14936 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 15056 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 5 ms 15092 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 5 ms 14936 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 4 ms 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 ms 14936 KB Output is correct
32 Correct 4 ms 14940 KB Output is correct
33 Correct 5 ms 15024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14940 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 14996 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 6 ms 14940 KB Output is correct
8 Correct 6 ms 14936 KB Output is correct
9 Correct 5 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 5 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14936 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 4 ms 14936 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 15056 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 5 ms 15092 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 5 ms 14936 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 4 ms 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 ms 14936 KB Output is correct
32 Correct 4 ms 14940 KB Output is correct
33 Correct 5 ms 15024 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 82 ms 18904 KB Output is correct
36 Correct 107 ms 21920 KB Output is correct
37 Correct 106 ms 21972 KB Output is correct
38 Correct 110 ms 21552 KB Output is correct
39 Correct 7 ms 15708 KB Output is correct
40 Correct 13 ms 17496 KB Output is correct
41 Correct 12 ms 17312 KB Output is correct
42 Correct 7 ms 15452 KB Output is correct
43 Correct 7 ms 15452 KB Output is correct
44 Correct 7 ms 15452 KB Output is correct
45 Correct 7 ms 15960 KB Output is correct
46 Correct 7 ms 15708 KB Output is correct
47 Correct 10 ms 16980 KB Output is correct
48 Correct 10 ms 16456 KB Output is correct
49 Correct 16 ms 17000 KB Output is correct
50 Correct 119 ms 22036 KB Output is correct
51 Correct 10 ms 16216 KB Output is correct
52 Correct 95 ms 19952 KB Output is correct
53 Correct 16 ms 16732 KB Output is correct
54 Correct 100 ms 20940 KB Output is correct
55 Correct 9 ms 15964 KB Output is correct
56 Correct 9 ms 16108 KB Output is correct
57 Correct 9 ms 16220 KB Output is correct
58 Correct 9 ms 16216 KB Output is correct
59 Correct 17 ms 17756 KB Output is correct
60 Correct 87 ms 20232 KB Output is correct
61 Correct 10 ms 16220 KB Output is correct
62 Correct 119 ms 21192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14940 KB Output is correct
2 Correct 3 ms 14940 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 14996 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 6 ms 14940 KB Output is correct
8 Correct 6 ms 14936 KB Output is correct
9 Correct 5 ms 14936 KB Output is correct
10 Correct 5 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 5 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14936 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14940 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 4 ms 14936 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 15056 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 5 ms 15092 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 5 ms 14936 KB Output is correct
26 Correct 4 ms 14940 KB Output is correct
27 Correct 4 ms 14940 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 3 ms 14940 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 4 ms 14936 KB Output is correct
32 Correct 4 ms 14940 KB Output is correct
33 Correct 5 ms 15024 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 82 ms 18904 KB Output is correct
36 Correct 107 ms 21920 KB Output is correct
37 Correct 106 ms 21972 KB Output is correct
38 Correct 110 ms 21552 KB Output is correct
39 Correct 7 ms 15708 KB Output is correct
40 Correct 13 ms 17496 KB Output is correct
41 Correct 12 ms 17312 KB Output is correct
42 Correct 7 ms 15452 KB Output is correct
43 Correct 7 ms 15452 KB Output is correct
44 Correct 7 ms 15452 KB Output is correct
45 Correct 7 ms 15960 KB Output is correct
46 Correct 7 ms 15708 KB Output is correct
47 Correct 10 ms 16980 KB Output is correct
48 Correct 10 ms 16456 KB Output is correct
49 Correct 16 ms 17000 KB Output is correct
50 Correct 119 ms 22036 KB Output is correct
51 Correct 10 ms 16216 KB Output is correct
52 Correct 95 ms 19952 KB Output is correct
53 Correct 16 ms 16732 KB Output is correct
54 Correct 100 ms 20940 KB Output is correct
55 Correct 9 ms 15964 KB Output is correct
56 Correct 9 ms 16108 KB Output is correct
57 Correct 9 ms 16220 KB Output is correct
58 Correct 9 ms 16216 KB Output is correct
59 Correct 17 ms 17756 KB Output is correct
60 Correct 87 ms 20232 KB Output is correct
61 Correct 10 ms 16220 KB Output is correct
62 Correct 119 ms 21192 KB Output is correct
63 Correct 688 ms 64888 KB Output is correct
64 Correct 717 ms 65272 KB Output is correct
65 Correct 712 ms 65068 KB Output is correct
66 Correct 423 ms 51124 KB Output is correct
67 Correct 1905 ms 179036 KB Output is correct
68 Correct 386 ms 51116 KB Output is correct
69 Correct 592 ms 50772 KB Output is correct
70 Correct 441 ms 51048 KB Output is correct
71 Correct 427 ms 51044 KB Output is correct
72 Correct 1306 ms 98128 KB Output is correct
73 Correct 1391 ms 106112 KB Output is correct
74 Correct 2839 ms 144792 KB Output is correct
75 Correct 824 ms 58468 KB Output is correct
76 Correct 1649 ms 92892 KB Output is correct
77 Correct 1784 ms 92924 KB Output is correct
78 Correct 339 ms 51660 KB Output is correct
79 Correct 760 ms 61960 KB Output is correct
80 Correct 368 ms 46024 KB Output is correct
81 Correct 623 ms 54512 KB Output is correct
82 Correct 1107 ms 73980 KB Output is correct
83 Correct 1079 ms 73596 KB Output is correct
84 Correct 1045 ms 77248 KB Output is correct
85 Correct 1020 ms 77244 KB Output is correct
86 Correct 1746 ms 223280 KB Output is correct
87 Correct 1854 ms 224548 KB Output is correct
88 Correct 1434 ms 101924 KB Output is correct
89 Correct 1598 ms 88428 KB Output is correct