답안 #388091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
388091 2021-04-10T05:22:18 Z pure_mem 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1510 ms 69108 KB
#include <bits/stdc++.h> 
 
#define X first
#define Y second
#define MP make_pair
#define ll long long
 
using namespace std;
 
const int MAXN = 1e5 + 12;
            
int n, m, dsu[MAXN];
set< int > children[MAXN], graph[MAXN], rgraph[MAXN];
queue< pair<int, int> > to_merge;
ll answer, sz[MAXN];

void merge(int a, int b){
	graph[a].insert(b);
	rgraph[b].insert(a);
	if(graph[b].count(a))
		to_merge.push(MP(a, b));
}

int get_pr(int v){
	if(dsu[v] == v)
		return v;
	return dsu[v] = get_pr(dsu[v]);
}

int dsu_size(int a){
	return children[a].size() + graph[a].size() + rgraph[a].size();
}

void dsu_merge(int a, int b){
	if(a == b)
		return;
	if(dsu_size(a) < dsu_size(b))
		swap(a, b);
	answer += sz[b] * children[a].size() + sz[a] * children[b].size();
	dsu[b] = a, sz[a] += sz[b];
	for(int i: children[b]){
		if(children[a].count(i)) 
			answer -= sz[a];
		else
			children[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);
		merge(a, i);
	}
	for(int i: rgraph[b]){
		graph[i].erase(b);
		merge(i, a);
	}
}

int main () {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i++)
		sz[i] = 1, dsu[i] = i, children[i].insert(i);
	for(int a, b;m--;){
		cin >> a >> b;
		b = get_pr(b);
		if(get_pr(a) != b && !children[b].count(a)){
			children[b].insert(a);
			answer += sz[b];
			a = get_pr(a);
			merge(a, b);
			while(!to_merge.empty()){
				tie(a, b) = to_merge.front();
				to_merge.pop();
				dsu_merge(get_pr(a), get_pr(b));
			}	
		}
		cout << answer << "\n";	
	}
}               
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14344 KB Output is correct
4 Correct 8 ms 14320 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 9 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 9 ms 14340 KB Output is correct
13 Correct 9 ms 14544 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 8 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 10 ms 14432 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14400 KB Output is correct
23 Correct 9 ms 14412 KB Output is correct
24 Correct 9 ms 14452 KB Output is correct
25 Correct 9 ms 14356 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 9 ms 14404 KB Output is correct
29 Correct 9 ms 14436 KB Output is correct
30 Correct 9 ms 14412 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 9 ms 14412 KB Output is correct
33 Correct 9 ms 14396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14344 KB Output is correct
4 Correct 8 ms 14320 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 9 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 9 ms 14340 KB Output is correct
13 Correct 9 ms 14544 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 8 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 10 ms 14432 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14400 KB Output is correct
23 Correct 9 ms 14412 KB Output is correct
24 Correct 9 ms 14452 KB Output is correct
25 Correct 9 ms 14356 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 9 ms 14404 KB Output is correct
29 Correct 9 ms 14436 KB Output is correct
30 Correct 9 ms 14412 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 9 ms 14412 KB Output is correct
33 Correct 9 ms 14396 KB Output is correct
34 Correct 11 ms 14568 KB Output is correct
35 Correct 104 ms 20280 KB Output is correct
36 Correct 130 ms 22980 KB Output is correct
37 Correct 133 ms 23072 KB Output is correct
38 Correct 127 ms 22720 KB Output is correct
39 Correct 12 ms 14796 KB Output is correct
40 Correct 17 ms 15080 KB Output is correct
41 Correct 16 ms 15052 KB Output is correct
42 Correct 12 ms 14760 KB Output is correct
43 Correct 13 ms 14820 KB Output is correct
44 Correct 13 ms 14812 KB Output is correct
45 Correct 12 ms 14800 KB Output is correct
46 Correct 12 ms 14680 KB Output is correct
47 Correct 13 ms 14952 KB Output is correct
48 Correct 13 ms 14976 KB Output is correct
49 Correct 22 ms 15644 KB Output is correct
50 Correct 141 ms 23236 KB Output is correct
51 Correct 17 ms 15068 KB Output is correct
52 Correct 115 ms 21436 KB Output is correct
53 Correct 20 ms 15528 KB Output is correct
54 Correct 124 ms 22408 KB Output is correct
55 Correct 15 ms 15200 KB Output is correct
56 Correct 15 ms 15180 KB Output is correct
57 Correct 19 ms 15180 KB Output is correct
58 Correct 15 ms 15180 KB Output is correct
59 Correct 13 ms 15180 KB Output is correct
60 Correct 98 ms 20164 KB Output is correct
61 Correct 14 ms 14924 KB Output is correct
62 Correct 127 ms 22620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14344 KB Output is correct
4 Correct 8 ms 14320 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14412 KB Output is correct
8 Correct 9 ms 14436 KB Output is correct
9 Correct 9 ms 14392 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 8 ms 14404 KB Output is correct
12 Correct 9 ms 14340 KB Output is correct
13 Correct 9 ms 14544 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 8 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 9 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 10 ms 14432 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 9 ms 14400 KB Output is correct
23 Correct 9 ms 14412 KB Output is correct
24 Correct 9 ms 14452 KB Output is correct
25 Correct 9 ms 14356 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 9 ms 14436 KB Output is correct
28 Correct 9 ms 14404 KB Output is correct
29 Correct 9 ms 14436 KB Output is correct
30 Correct 9 ms 14412 KB Output is correct
31 Correct 9 ms 14412 KB Output is correct
32 Correct 9 ms 14412 KB Output is correct
33 Correct 9 ms 14396 KB Output is correct
34 Correct 11 ms 14568 KB Output is correct
35 Correct 104 ms 20280 KB Output is correct
36 Correct 130 ms 22980 KB Output is correct
37 Correct 133 ms 23072 KB Output is correct
38 Correct 127 ms 22720 KB Output is correct
39 Correct 12 ms 14796 KB Output is correct
40 Correct 17 ms 15080 KB Output is correct
41 Correct 16 ms 15052 KB Output is correct
42 Correct 12 ms 14760 KB Output is correct
43 Correct 13 ms 14820 KB Output is correct
44 Correct 13 ms 14812 KB Output is correct
45 Correct 12 ms 14800 KB Output is correct
46 Correct 12 ms 14680 KB Output is correct
47 Correct 13 ms 14952 KB Output is correct
48 Correct 13 ms 14976 KB Output is correct
49 Correct 22 ms 15644 KB Output is correct
50 Correct 141 ms 23236 KB Output is correct
51 Correct 17 ms 15068 KB Output is correct
52 Correct 115 ms 21436 KB Output is correct
53 Correct 20 ms 15528 KB Output is correct
54 Correct 124 ms 22408 KB Output is correct
55 Correct 15 ms 15200 KB Output is correct
56 Correct 15 ms 15180 KB Output is correct
57 Correct 19 ms 15180 KB Output is correct
58 Correct 15 ms 15180 KB Output is correct
59 Correct 13 ms 15180 KB Output is correct
60 Correct 98 ms 20164 KB Output is correct
61 Correct 14 ms 14924 KB Output is correct
62 Correct 127 ms 22620 KB Output is correct
63 Correct 805 ms 67992 KB Output is correct
64 Correct 804 ms 67992 KB Output is correct
65 Correct 834 ms 67996 KB Output is correct
66 Correct 341 ms 33988 KB Output is correct
67 Correct 639 ms 60996 KB Output is correct
68 Correct 321 ms 34060 KB Output is correct
69 Correct 602 ms 35156 KB Output is correct
70 Correct 326 ms 34036 KB Output is correct
71 Correct 346 ms 33972 KB Output is correct
72 Correct 662 ms 45124 KB Output is correct
73 Correct 694 ms 46632 KB Output is correct
74 Correct 1510 ms 68552 KB Output is correct
75 Correct 857 ms 41924 KB Output is correct
76 Correct 1122 ms 54100 KB Output is correct
77 Correct 1126 ms 54212 KB Output is correct
78 Correct 358 ms 37572 KB Output is correct
79 Correct 627 ms 42116 KB Output is correct
80 Correct 339 ms 36416 KB Output is correct
81 Correct 538 ms 40132 KB Output is correct
82 Correct 1243 ms 54724 KB Output is correct
83 Correct 1288 ms 54608 KB Output is correct
84 Correct 938 ms 53316 KB Output is correct
85 Correct 937 ms 53348 KB Output is correct
86 Correct 491 ms 66836 KB Output is correct
87 Correct 540 ms 69108 KB Output is correct
88 Correct 673 ms 45888 KB Output is correct
89 Correct 1088 ms 51880 KB Output is correct