Submission #404387

# Submission time Handle Problem Language Result Execution time Memory
404387 2021-05-14T10:15:27 Z ntabc05101 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1232 ms 69000 KB
#include<bits/stdc++.h>
using namespace std;

#define f first
#define s second

using ll = long long;

const int mxN = 100000;

int par[mxN + 5];
ll sz[mxN + 5];
ll result = 0;

set<int> ccs[mxN + 5], ou[mxN + 5], in[mxN + 5];
queue< pair<int, int> > to_merge;

void weak_connection(int x, int y) {
	ou[x].insert(y); in[y].insert(x);
	if (ou[y].count(x)) to_merge.emplace(x, y);
}

int tot_sz(int x) {
	return ccs[x].size() + in[x].size() + ou[x].size();
}

int find(int x) {
	return (x == par[x] ? x: par[x] = find(par[x]));
}

void unite_set(int x, int y) {
	if (x == y) return ;
	if (tot_sz(x) < tot_sz(y)) swap(x, y);
	
	result += sz[y] * ccs[x].size() + sz[x] * ccs[y].size();
	
	par[y] = x; sz[x] += sz[y];
	
	for (int u: ccs[y]) {
		if (ccs[x].count(u)) result -= sz[x];
		else ccs[x].emplace(u);
	}
	
	ou[x].erase(y); in[x].erase(y);
	ou[y].erase(x); in[y].erase(x);
	for (int u: ou[y]) {
		in[u].erase(y);
		weak_connection(x, u);
	}
	for (int u: in[y]) {
		ou[u].erase(y);
		weak_connection(u, x);
	}
}


int main() {
        cin.tie(0)->sync_with_stdio(0);
        
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; i++) {
        	par[i] = i;
        	sz[i] = 1;
        	ccs[i].insert(i);
        }
        
        int x, y;
        while (m--) {
        	cin >> x >> y;
        	
        	y = find(y);
        	if (find(x) != y && !ccs[y].count(x)) {
	       		ccs[y].emplace(x);
        		result += sz[y];
        		
        		x = find(x);
        		weak_connection(x, y);
        		while (!to_merge.empty()) {
        			tie(x, y) = to_merge.front();
        			to_merge.pop();
        			
        			unite_set(find(x), find(y));
        		}
        	}
        	
        	cout << result << "\n";
        }

        return 0;
}

/*
4 6
1 2
2 3
3 2
1 3
3 4
4 3
*/
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 10 ms 14412 KB Output is correct
3 Correct 8 ms 14408 KB Output is correct
4 Correct 8 ms 14376 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 8 ms 14336 KB Output is correct
7 Correct 9 ms 14428 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 8 ms 14336 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14360 KB Output is correct
16 Correct 9 ms 14408 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 8 ms 14416 KB Output is correct
19 Correct 8 ms 14336 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14424 KB Output is correct
22 Correct 8 ms 14408 KB Output is correct
23 Correct 8 ms 14416 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 11 ms 14412 KB Output is correct
26 Correct 9 ms 14408 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 8 ms 14412 KB Output is correct
29 Correct 8 ms 14368 KB Output is correct
30 Correct 8 ms 14344 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 8 ms 14332 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 10 ms 14412 KB Output is correct
3 Correct 8 ms 14408 KB Output is correct
4 Correct 8 ms 14376 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 8 ms 14336 KB Output is correct
7 Correct 9 ms 14428 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 8 ms 14336 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14360 KB Output is correct
16 Correct 9 ms 14408 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 8 ms 14416 KB Output is correct
19 Correct 8 ms 14336 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14424 KB Output is correct
22 Correct 8 ms 14408 KB Output is correct
23 Correct 8 ms 14416 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 11 ms 14412 KB Output is correct
26 Correct 9 ms 14408 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 8 ms 14412 KB Output is correct
29 Correct 8 ms 14368 KB Output is correct
30 Correct 8 ms 14344 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 8 ms 14332 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 11 ms 14532 KB Output is correct
35 Correct 119 ms 20164 KB Output is correct
36 Correct 131 ms 22992 KB Output is correct
37 Correct 129 ms 23292 KB Output is correct
38 Correct 126 ms 22700 KB Output is correct
39 Correct 14 ms 14688 KB Output is correct
40 Correct 13 ms 15056 KB Output is correct
41 Correct 13 ms 15052 KB Output is correct
42 Correct 11 ms 14680 KB Output is correct
43 Correct 12 ms 14804 KB Output is correct
44 Correct 12 ms 14796 KB Output is correct
45 Correct 11 ms 14680 KB Output is correct
46 Correct 11 ms 14796 KB Output is correct
47 Correct 13 ms 14904 KB Output is correct
48 Correct 13 ms 14936 KB Output is correct
49 Correct 20 ms 15692 KB Output is correct
50 Correct 130 ms 23248 KB Output is correct
51 Correct 16 ms 15060 KB Output is correct
52 Correct 116 ms 21412 KB Output is correct
53 Correct 23 ms 15600 KB Output is correct
54 Correct 122 ms 22292 KB Output is correct
55 Correct 15 ms 15188 KB Output is correct
56 Correct 15 ms 15208 KB Output is correct
57 Correct 14 ms 15064 KB Output is correct
58 Correct 14 ms 15060 KB Output is correct
59 Correct 13 ms 15180 KB Output is correct
60 Correct 103 ms 20196 KB Output is correct
61 Correct 14 ms 14924 KB Output is correct
62 Correct 129 ms 22584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 10 ms 14412 KB Output is correct
3 Correct 8 ms 14408 KB Output is correct
4 Correct 8 ms 14376 KB Output is correct
5 Correct 10 ms 14412 KB Output is correct
6 Correct 8 ms 14336 KB Output is correct
7 Correct 9 ms 14428 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 8 ms 14412 KB Output is correct
12 Correct 8 ms 14412 KB Output is correct
13 Correct 8 ms 14336 KB Output is correct
14 Correct 8 ms 14412 KB Output is correct
15 Correct 9 ms 14360 KB Output is correct
16 Correct 9 ms 14408 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 8 ms 14416 KB Output is correct
19 Correct 8 ms 14336 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14424 KB Output is correct
22 Correct 8 ms 14408 KB Output is correct
23 Correct 8 ms 14416 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 11 ms 14412 KB Output is correct
26 Correct 9 ms 14408 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 8 ms 14412 KB Output is correct
29 Correct 8 ms 14368 KB Output is correct
30 Correct 8 ms 14344 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 8 ms 14332 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 11 ms 14532 KB Output is correct
35 Correct 119 ms 20164 KB Output is correct
36 Correct 131 ms 22992 KB Output is correct
37 Correct 129 ms 23292 KB Output is correct
38 Correct 126 ms 22700 KB Output is correct
39 Correct 14 ms 14688 KB Output is correct
40 Correct 13 ms 15056 KB Output is correct
41 Correct 13 ms 15052 KB Output is correct
42 Correct 11 ms 14680 KB Output is correct
43 Correct 12 ms 14804 KB Output is correct
44 Correct 12 ms 14796 KB Output is correct
45 Correct 11 ms 14680 KB Output is correct
46 Correct 11 ms 14796 KB Output is correct
47 Correct 13 ms 14904 KB Output is correct
48 Correct 13 ms 14936 KB Output is correct
49 Correct 20 ms 15692 KB Output is correct
50 Correct 130 ms 23248 KB Output is correct
51 Correct 16 ms 15060 KB Output is correct
52 Correct 116 ms 21412 KB Output is correct
53 Correct 23 ms 15600 KB Output is correct
54 Correct 122 ms 22292 KB Output is correct
55 Correct 15 ms 15188 KB Output is correct
56 Correct 15 ms 15208 KB Output is correct
57 Correct 14 ms 15064 KB Output is correct
58 Correct 14 ms 15060 KB Output is correct
59 Correct 13 ms 15180 KB Output is correct
60 Correct 103 ms 20196 KB Output is correct
61 Correct 14 ms 14924 KB Output is correct
62 Correct 129 ms 22584 KB Output is correct
63 Correct 648 ms 68036 KB Output is correct
64 Correct 624 ms 67928 KB Output is correct
65 Correct 620 ms 68120 KB Output is correct
66 Correct 294 ms 34008 KB Output is correct
67 Correct 532 ms 61092 KB Output is correct
68 Correct 289 ms 33992 KB Output is correct
69 Correct 505 ms 35104 KB Output is correct
70 Correct 327 ms 34080 KB Output is correct
71 Correct 297 ms 34084 KB Output is correct
72 Correct 584 ms 45124 KB Output is correct
73 Correct 570 ms 46692 KB Output is correct
74 Correct 1232 ms 68576 KB Output is correct
75 Correct 822 ms 41844 KB Output is correct
76 Correct 998 ms 53884 KB Output is correct
77 Correct 1000 ms 54144 KB Output is correct
78 Correct 316 ms 37720 KB Output is correct
79 Correct 541 ms 42136 KB Output is correct
80 Correct 275 ms 36612 KB Output is correct
81 Correct 496 ms 40164 KB Output is correct
82 Correct 1032 ms 54628 KB Output is correct
83 Correct 1050 ms 54584 KB Output is correct
84 Correct 791 ms 53344 KB Output is correct
85 Correct 786 ms 53320 KB Output is correct
86 Correct 429 ms 66768 KB Output is correct
87 Correct 466 ms 69000 KB Output is correct
88 Correct 598 ms 45988 KB Output is correct
89 Correct 940 ms 51880 KB Output is correct