Submission #856875

# Submission time Handle Problem Language Result Execution time Memory
856875 2023-10-04T18:28:25 Z serifefedartar Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
923 ms 65836 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 18; 
const ll INF = 1e15;
const ll MAXN = 1e5 + 5;
 
vector<set<int>> graph, rev;
queue<pair<int,int>> q;
set<int> in[MAXN];
int par[MAXN];
ll sz[MAXN];
ll ans = 0;
int find(int node) {
	if (par[node] == node)
		return node;
	return par[node] = find(par[node]);
}
 
void conn(int from, int to) {
	graph[from].insert(to);
	rev[to].insert(from);
	if (graph[to].count(from))
		q.push({from, to});	
}
 
void merge(int x, int y) {
	if (x == y)
		return ;
	if (in[x].size() < in[y].size())
 		swap(x, y);
 
	ans += in[y].size() * sz[x] + in[x].size() * sz[y]; 
	sz[x] += sz[y];
	par[y] = x;
 		
	for (auto u : in[y]) {
		if (in[x].count(u) == 0)
			in[x].insert(u);
		else
			ans -= sz[x];
	}
 
	graph[x].erase(y);
	graph[y].erase(x);
	rev[x].erase(y); 
	rev[y].erase(x);
	for (auto u : graph[y]) {
		rev[u].erase(y);
		conn(x, u); 
	}
	for (auto u : rev[y]) {
		graph[u].erase(y);
		conn(u, x);
	}
}
 
int main() {
	fast
	int N, M, a, b;
	cin >> N >> M;
 
	for (int i = 1; i <= N; i++) {
		par[i] = i, sz[i] = 1;
		in[i].insert(i);
	}
	graph = vector<set<int>>(N+1, set<int>());
	rev = vector<set<int>>(N+1, set<int>());
 
	for (int i = 1; i <= M; i++) {
		cin >> a >> b;
		int par_a = find(a);
		int par_b = find(b);
 
		if (par_b == par_a) {
			cout << ans << "\n";
			continue;
		}
		if (in[par_b].count(a) == 0) {
			in[par_b].insert(a);
			ans += sz[par_b];
 
			conn(par_a, par_b);
			while (!q.empty()) {
				int from = q.front().f;
				int to = q.front().s;
				q.pop();
				merge(find(from), find(to));
			}
		}
		cout << ans << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5976 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 1 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 5980 KB Output is correct
26 Correct 2 ms 5980 KB Output is correct
27 Correct 2 ms 5980 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 2 ms 5980 KB Output is correct
31 Correct 2 ms 6068 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5976 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 1 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 5980 KB Output is correct
26 Correct 2 ms 5980 KB Output is correct
27 Correct 2 ms 5980 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 2 ms 5980 KB Output is correct
31 Correct 2 ms 6068 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 3 ms 6236 KB Output is correct
35 Correct 56 ms 9692 KB Output is correct
36 Correct 92 ms 12296 KB Output is correct
37 Correct 78 ms 12368 KB Output is correct
38 Correct 72 ms 12068 KB Output is correct
39 Correct 4 ms 6492 KB Output is correct
40 Correct 5 ms 7004 KB Output is correct
41 Correct 5 ms 7004 KB Output is correct
42 Correct 3 ms 6488 KB Output is correct
43 Correct 4 ms 6492 KB Output is correct
44 Correct 4 ms 6492 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 4 ms 6488 KB Output is correct
47 Correct 5 ms 6748 KB Output is correct
48 Correct 5 ms 6880 KB Output is correct
49 Correct 9 ms 7564 KB Output is correct
50 Correct 77 ms 12548 KB Output is correct
51 Correct 6 ms 6748 KB Output is correct
52 Correct 66 ms 10592 KB Output is correct
53 Correct 9 ms 7260 KB Output is correct
54 Correct 68 ms 11456 KB Output is correct
55 Correct 6 ms 7004 KB Output is correct
56 Correct 6 ms 7004 KB Output is correct
57 Correct 6 ms 7004 KB Output is correct
58 Correct 5 ms 7072 KB Output is correct
59 Correct 4 ms 7004 KB Output is correct
60 Correct 72 ms 9308 KB Output is correct
61 Correct 5 ms 6748 KB Output is correct
62 Correct 79 ms 11920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5976 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 1 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 5980 KB Output is correct
26 Correct 2 ms 5980 KB Output is correct
27 Correct 2 ms 5980 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 2 ms 5980 KB Output is correct
31 Correct 2 ms 6068 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 3 ms 6236 KB Output is correct
35 Correct 56 ms 9692 KB Output is correct
36 Correct 92 ms 12296 KB Output is correct
37 Correct 78 ms 12368 KB Output is correct
38 Correct 72 ms 12068 KB Output is correct
39 Correct 4 ms 6492 KB Output is correct
40 Correct 5 ms 7004 KB Output is correct
41 Correct 5 ms 7004 KB Output is correct
42 Correct 3 ms 6488 KB Output is correct
43 Correct 4 ms 6492 KB Output is correct
44 Correct 4 ms 6492 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 4 ms 6488 KB Output is correct
47 Correct 5 ms 6748 KB Output is correct
48 Correct 5 ms 6880 KB Output is correct
49 Correct 9 ms 7564 KB Output is correct
50 Correct 77 ms 12548 KB Output is correct
51 Correct 6 ms 6748 KB Output is correct
52 Correct 66 ms 10592 KB Output is correct
53 Correct 9 ms 7260 KB Output is correct
54 Correct 68 ms 11456 KB Output is correct
55 Correct 6 ms 7004 KB Output is correct
56 Correct 6 ms 7004 KB Output is correct
57 Correct 6 ms 7004 KB Output is correct
58 Correct 5 ms 7072 KB Output is correct
59 Correct 4 ms 7004 KB Output is correct
60 Correct 72 ms 9308 KB Output is correct
61 Correct 5 ms 6748 KB Output is correct
62 Correct 79 ms 11920 KB Output is correct
63 Correct 478 ms 64596 KB Output is correct
64 Correct 446 ms 64504 KB Output is correct
65 Correct 431 ms 64432 KB Output is correct
66 Correct 153 ms 31924 KB Output is correct
67 Correct 349 ms 59068 KB Output is correct
68 Correct 176 ms 32296 KB Output is correct
69 Correct 302 ms 33020 KB Output is correct
70 Correct 210 ms 31828 KB Output is correct
71 Correct 164 ms 31828 KB Output is correct
72 Correct 386 ms 43092 KB Output is correct
73 Correct 330 ms 44828 KB Output is correct
74 Correct 923 ms 65836 KB Output is correct
75 Correct 464 ms 38784 KB Output is correct
76 Correct 647 ms 51108 KB Output is correct
77 Correct 607 ms 51140 KB Output is correct
78 Correct 185 ms 38224 KB Output is correct
79 Correct 373 ms 41168 KB Output is correct
80 Correct 165 ms 34672 KB Output is correct
81 Correct 282 ms 36872 KB Output is correct
82 Correct 673 ms 52428 KB Output is correct
83 Correct 723 ms 52636 KB Output is correct
84 Correct 473 ms 51284 KB Output is correct
85 Correct 489 ms 51224 KB Output is correct
86 Correct 269 ms 64988 KB Output is correct
87 Correct 271 ms 65812 KB Output is correct
88 Correct 338 ms 44376 KB Output is correct
89 Correct 565 ms 48764 KB Output is correct