답안 #220455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
220455 2020-04-08T00:51:14 Z super_j6 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
652 ms 44612 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define f first
#define s second

const int maxn = 100000;
int n, m;
int par[maxn];
long long sz[maxn];
set<int> ei[maxn];
set<pi> eo[maxn];
long long ret;

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

void solve(int u, int v){
	int x = find(u), y = find(v);
	if(x == y) return;
	auto it = eo[y].lower_bound({x, 0});
	if(it != eo[y].end() && (*it).f == x){
		if(ei[x].size() + eo[x].size() < ei[y].size() + eo[y].size()) swap(x, y);
		vector<pi> vo;
		for(pi i : eo[y]){
			ret -= sz[i.f];
			ei[i.f].erase(i.s);
			if(i.f != x) vo.push_back(i);
		}
		eo[y].clear();
		ret += sz[y] * (2 * sz[x] + ei[x].size() - ei[y].size());
		vector<int> vi;
		for(int i : ei[y]){
			int j = find(i);
			eo[j].erase({y, i});
			if(j != x) vi.push_back(i);
		}
		ei[y].clear();
		par[y] = x, sz[x] += sz[y];
		for(int i : vi) solve(i, x);
		for(pi i : vo) solve(i.s, i.f);
	}else if(!ei[y].count(u)){
		ret += sz[y];
		ei[y].insert(u);
		eo[x].insert({y, u});
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m;
	
	for(int i = 0; i < n; i++) par[i] = i, sz[i] = 1;
	for(int i = 0; i < m; i++){
		int u, v;
		cin >> u >> v;
		u--, v--;
		solve(u, v);
		cout << ret << endl;
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9704 KB Output is correct
12 Correct 11 ms 9676 KB Output is correct
13 Correct 11 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9856 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9704 KB Output is correct
12 Correct 11 ms 9676 KB Output is correct
13 Correct 11 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9856 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
34 Correct 14 ms 9856 KB Output is correct
35 Correct 91 ms 15096 KB Output is correct
36 Correct 111 ms 17400 KB Output is correct
37 Correct 116 ms 17400 KB Output is correct
38 Correct 104 ms 17144 KB Output is correct
39 Correct 11 ms 9856 KB Output is correct
40 Correct 12 ms 9900 KB Output is correct
41 Correct 12 ms 9984 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 12 ms 9984 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 11 ms 9856 KB Output is correct
47 Correct 12 ms 9984 KB Output is correct
48 Correct 13 ms 9984 KB Output is correct
49 Correct 19 ms 10496 KB Output is correct
50 Correct 108 ms 17400 KB Output is correct
51 Correct 16 ms 10112 KB Output is correct
52 Correct 109 ms 16248 KB Output is correct
53 Correct 17 ms 10496 KB Output is correct
54 Correct 105 ms 16864 KB Output is correct
55 Correct 14 ms 10240 KB Output is correct
56 Correct 14 ms 10240 KB Output is correct
57 Correct 15 ms 10240 KB Output is correct
58 Correct 14 ms 10240 KB Output is correct
59 Correct 11 ms 9856 KB Output is correct
60 Correct 90 ms 14712 KB Output is correct
61 Correct 13 ms 9984 KB Output is correct
62 Correct 105 ms 16888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9704 KB Output is correct
12 Correct 11 ms 9676 KB Output is correct
13 Correct 11 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9856 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
34 Correct 14 ms 9856 KB Output is correct
35 Correct 91 ms 15096 KB Output is correct
36 Correct 111 ms 17400 KB Output is correct
37 Correct 116 ms 17400 KB Output is correct
38 Correct 104 ms 17144 KB Output is correct
39 Correct 11 ms 9856 KB Output is correct
40 Correct 12 ms 9900 KB Output is correct
41 Correct 12 ms 9984 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 12 ms 9984 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 11 ms 9856 KB Output is correct
47 Correct 12 ms 9984 KB Output is correct
48 Correct 13 ms 9984 KB Output is correct
49 Correct 19 ms 10496 KB Output is correct
50 Correct 108 ms 17400 KB Output is correct
51 Correct 16 ms 10112 KB Output is correct
52 Correct 109 ms 16248 KB Output is correct
53 Correct 17 ms 10496 KB Output is correct
54 Correct 105 ms 16864 KB Output is correct
55 Correct 14 ms 10240 KB Output is correct
56 Correct 14 ms 10240 KB Output is correct
57 Correct 15 ms 10240 KB Output is correct
58 Correct 14 ms 10240 KB Output is correct
59 Correct 11 ms 9856 KB Output is correct
60 Correct 90 ms 14712 KB Output is correct
61 Correct 13 ms 9984 KB Output is correct
62 Correct 105 ms 16888 KB Output is correct
63 Correct 407 ms 44536 KB Output is correct
64 Correct 406 ms 44612 KB Output is correct
65 Correct 400 ms 44592 KB Output is correct
66 Correct 108 ms 15380 KB Output is correct
67 Correct 175 ms 19192 KB Output is correct
68 Correct 106 ms 15352 KB Output is correct
69 Correct 236 ms 20092 KB Output is correct
70 Correct 113 ms 15352 KB Output is correct
71 Correct 112 ms 15352 KB Output is correct
72 Correct 205 ms 19576 KB Output is correct
73 Correct 213 ms 19320 KB Output is correct
74 Correct 652 ms 28536 KB Output is correct
75 Correct 378 ms 24744 KB Output is correct
76 Correct 499 ms 27724 KB Output is correct
77 Correct 510 ms 27640 KB Output is correct
78 Correct 146 ms 18808 KB Output is correct
79 Correct 271 ms 22520 KB Output is correct
80 Correct 155 ms 18936 KB Output is correct
81 Correct 245 ms 22136 KB Output is correct
82 Correct 449 ms 33304 KB Output is correct
83 Correct 478 ms 33528 KB Output is correct
84 Correct 442 ms 33640 KB Output is correct
85 Correct 421 ms 33576 KB Output is correct
86 Correct 108 ms 14584 KB Output is correct
87 Correct 144 ms 16760 KB Output is correct
88 Correct 208 ms 19448 KB Output is correct
89 Correct 488 ms 27128 KB Output is correct