답안 #211837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211837 2020-03-21T13:48:20 Z theStaticMind 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1048 ms 68100 KB
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;

int ans = 0;

vector<int> no(100005);
vector<int> S(100005, 1);
unordered_set<int> data[100005], indeg[100005], outdeg[100005]; 
queue<ii> Q;

void upans(int x, int sign){
	ans += sign * ((sz(data[x]) - S[x]) * S[x] + S[x] * (S[x] - 1)); 
}

int find(int x){
	
	if(no[x] == x) return x;
	return no[x] = find(no[x]);

}

bool merge(int x, int y){
	
	x = find(x);
	y = find(y);

	if(x == y) return false;

	upans(x, -1);
	upans(y, -1);

	if(indeg[x].count(y)) indeg[x].erase(y);
	if(indeg[y].count(x)) indeg[y].erase(x);
	if(outdeg[x].count(y)) outdeg[x].erase(y);
	if(outdeg[y].count(x)) outdeg[y].erase(x);
	
	if(sz(indeg[x]) + sz(outdeg[x]) < sz(indeg[y]) + sz(outdeg[y])) swap(x, y);
	
	for(auto p : outdeg[y]){
		if(indeg[x].count(p)) Q.push({x, p});
		indeg[p].erase(y);
		indeg[p].insert(x);
	}
	for(auto p : indeg[y]){
		if(outdeg[x].count(p)) Q.push({x, p});
		outdeg[p].erase(y);
		outdeg[p].insert(x);
	}

	if(outdeg[x].size() < outdeg[y].size()) swap(outdeg[x], outdeg[y]);
	if(indeg[x].size() < indeg[y].size()) swap(indeg[x], indeg[y]);
	if(data[x].size() < data[y].size()) swap(data[x], data[y]);
	
	for(auto p : data[y]) data[x].insert(p); data[y].clear();
	for(auto p : indeg[y]) indeg[x].insert(p); indeg[y].clear();
	for(auto p : outdeg[y]) outdeg[x].insert(p); outdeg[y].clear();
	
	no[y] = x;
	S[x] += S[y];

	upans(x, 1);

	return true;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	for(int i = 0; i < 100005; i++) no[i] = i, data[i].insert(i);
	
	int n, q;
	cin >> n >> q;
	
	while(q--){
		int x, y;
		cin >> x >> y;

		if(outdeg[find(y)].count(find(x))){
			Q.push({x, y});
			while(!Q.empty()){
				merge(Q.front().first, Q.front().second);
				Q.pop();
			}
			
		}
		else {
			y = find(y);
			upans(y, -1);
			data[y].insert(x);
			upans(y, 1);
			outdeg[find(x)].insert(y);
			indeg[y].insert(find(x));
		}
		cout << ans << '\n';
	}
}

Compilation message

joitter2.cpp: In function 'bool merge(long long int, long long int)':
joitter2.cpp:62:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for(auto p : data[y]) data[x].insert(p); data[y].clear();
  ^~~
joitter2.cpp:62:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for(auto p : data[y]) data[x].insert(p); data[y].clear();
                                           ^~~~
joitter2.cpp:63:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for(auto p : indeg[y]) indeg[x].insert(p); indeg[y].clear();
  ^~~
joitter2.cpp:63:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for(auto p : indeg[y]) indeg[x].insert(p); indeg[y].clear();
                                             ^~~~~
joitter2.cpp:64:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for(auto p : outdeg[y]) outdeg[x].insert(p); outdeg[y].clear();
  ^~~
joitter2.cpp:64:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for(auto p : outdeg[y]) outdeg[x].insert(p); outdeg[y].clear();
                                               ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 24568 KB Output is correct
2 Correct 31 ms 24552 KB Output is correct
3 Correct 33 ms 24576 KB Output is correct
4 Correct 31 ms 24568 KB Output is correct
5 Correct 31 ms 24568 KB Output is correct
6 Correct 34 ms 24576 KB Output is correct
7 Correct 31 ms 24696 KB Output is correct
8 Correct 36 ms 24572 KB Output is correct
9 Correct 31 ms 24704 KB Output is correct
10 Correct 32 ms 24576 KB Output is correct
11 Correct 31 ms 24572 KB Output is correct
12 Correct 31 ms 24576 KB Output is correct
13 Correct 30 ms 24704 KB Output is correct
14 Correct 31 ms 24568 KB Output is correct
15 Correct 32 ms 24576 KB Output is correct
16 Correct 31 ms 24568 KB Output is correct
17 Correct 32 ms 24704 KB Output is correct
18 Correct 31 ms 24576 KB Output is correct
19 Correct 31 ms 24568 KB Output is correct
20 Correct 30 ms 24576 KB Output is correct
21 Correct 33 ms 24704 KB Output is correct
22 Correct 31 ms 24704 KB Output is correct
23 Correct 31 ms 24704 KB Output is correct
24 Correct 30 ms 24704 KB Output is correct
25 Correct 32 ms 24696 KB Output is correct
26 Correct 30 ms 24576 KB Output is correct
27 Correct 30 ms 24568 KB Output is correct
28 Correct 30 ms 24568 KB Output is correct
29 Correct 32 ms 24704 KB Output is correct
30 Correct 33 ms 24576 KB Output is correct
31 Correct 32 ms 24576 KB Output is correct
32 Correct 32 ms 24568 KB Output is correct
33 Correct 34 ms 24704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 24568 KB Output is correct
2 Correct 31 ms 24552 KB Output is correct
3 Correct 33 ms 24576 KB Output is correct
4 Correct 31 ms 24568 KB Output is correct
5 Correct 31 ms 24568 KB Output is correct
6 Correct 34 ms 24576 KB Output is correct
7 Correct 31 ms 24696 KB Output is correct
8 Correct 36 ms 24572 KB Output is correct
9 Correct 31 ms 24704 KB Output is correct
10 Correct 32 ms 24576 KB Output is correct
11 Correct 31 ms 24572 KB Output is correct
12 Correct 31 ms 24576 KB Output is correct
13 Correct 30 ms 24704 KB Output is correct
14 Correct 31 ms 24568 KB Output is correct
15 Correct 32 ms 24576 KB Output is correct
16 Correct 31 ms 24568 KB Output is correct
17 Correct 32 ms 24704 KB Output is correct
18 Correct 31 ms 24576 KB Output is correct
19 Correct 31 ms 24568 KB Output is correct
20 Correct 30 ms 24576 KB Output is correct
21 Correct 33 ms 24704 KB Output is correct
22 Correct 31 ms 24704 KB Output is correct
23 Correct 31 ms 24704 KB Output is correct
24 Correct 30 ms 24704 KB Output is correct
25 Correct 32 ms 24696 KB Output is correct
26 Correct 30 ms 24576 KB Output is correct
27 Correct 30 ms 24568 KB Output is correct
28 Correct 30 ms 24568 KB Output is correct
29 Correct 32 ms 24704 KB Output is correct
30 Correct 33 ms 24576 KB Output is correct
31 Correct 32 ms 24576 KB Output is correct
32 Correct 32 ms 24568 KB Output is correct
33 Correct 34 ms 24704 KB Output is correct
34 Correct 36 ms 24704 KB Output is correct
35 Correct 127 ms 28024 KB Output is correct
36 Correct 142 ms 30328 KB Output is correct
37 Correct 149 ms 30456 KB Output is correct
38 Correct 168 ms 30260 KB Output is correct
39 Correct 33 ms 24824 KB Output is correct
40 Correct 38 ms 24960 KB Output is correct
41 Correct 36 ms 24952 KB Output is correct
42 Correct 34 ms 24832 KB Output is correct
43 Correct 35 ms 24832 KB Output is correct
44 Correct 35 ms 24832 KB Output is correct
45 Correct 37 ms 24832 KB Output is correct
46 Correct 33 ms 24832 KB Output is correct
47 Correct 36 ms 24952 KB Output is correct
48 Correct 36 ms 24952 KB Output is correct
49 Correct 43 ms 25600 KB Output is correct
50 Correct 142 ms 30456 KB Output is correct
51 Correct 41 ms 25088 KB Output is correct
52 Correct 133 ms 28920 KB Output is correct
53 Correct 43 ms 25464 KB Output is correct
54 Correct 145 ms 29820 KB Output is correct
55 Correct 38 ms 25344 KB Output is correct
56 Correct 38 ms 25212 KB Output is correct
57 Correct 38 ms 25344 KB Output is correct
58 Correct 38 ms 25216 KB Output is correct
59 Correct 37 ms 24960 KB Output is correct
60 Correct 124 ms 27256 KB Output is correct
61 Correct 37 ms 24952 KB Output is correct
62 Correct 141 ms 29944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 24568 KB Output is correct
2 Correct 31 ms 24552 KB Output is correct
3 Correct 33 ms 24576 KB Output is correct
4 Correct 31 ms 24568 KB Output is correct
5 Correct 31 ms 24568 KB Output is correct
6 Correct 34 ms 24576 KB Output is correct
7 Correct 31 ms 24696 KB Output is correct
8 Correct 36 ms 24572 KB Output is correct
9 Correct 31 ms 24704 KB Output is correct
10 Correct 32 ms 24576 KB Output is correct
11 Correct 31 ms 24572 KB Output is correct
12 Correct 31 ms 24576 KB Output is correct
13 Correct 30 ms 24704 KB Output is correct
14 Correct 31 ms 24568 KB Output is correct
15 Correct 32 ms 24576 KB Output is correct
16 Correct 31 ms 24568 KB Output is correct
17 Correct 32 ms 24704 KB Output is correct
18 Correct 31 ms 24576 KB Output is correct
19 Correct 31 ms 24568 KB Output is correct
20 Correct 30 ms 24576 KB Output is correct
21 Correct 33 ms 24704 KB Output is correct
22 Correct 31 ms 24704 KB Output is correct
23 Correct 31 ms 24704 KB Output is correct
24 Correct 30 ms 24704 KB Output is correct
25 Correct 32 ms 24696 KB Output is correct
26 Correct 30 ms 24576 KB Output is correct
27 Correct 30 ms 24568 KB Output is correct
28 Correct 30 ms 24568 KB Output is correct
29 Correct 32 ms 24704 KB Output is correct
30 Correct 33 ms 24576 KB Output is correct
31 Correct 32 ms 24576 KB Output is correct
32 Correct 32 ms 24568 KB Output is correct
33 Correct 34 ms 24704 KB Output is correct
34 Correct 36 ms 24704 KB Output is correct
35 Correct 127 ms 28024 KB Output is correct
36 Correct 142 ms 30328 KB Output is correct
37 Correct 149 ms 30456 KB Output is correct
38 Correct 168 ms 30260 KB Output is correct
39 Correct 33 ms 24824 KB Output is correct
40 Correct 38 ms 24960 KB Output is correct
41 Correct 36 ms 24952 KB Output is correct
42 Correct 34 ms 24832 KB Output is correct
43 Correct 35 ms 24832 KB Output is correct
44 Correct 35 ms 24832 KB Output is correct
45 Correct 37 ms 24832 KB Output is correct
46 Correct 33 ms 24832 KB Output is correct
47 Correct 36 ms 24952 KB Output is correct
48 Correct 36 ms 24952 KB Output is correct
49 Correct 43 ms 25600 KB Output is correct
50 Correct 142 ms 30456 KB Output is correct
51 Correct 41 ms 25088 KB Output is correct
52 Correct 133 ms 28920 KB Output is correct
53 Correct 43 ms 25464 KB Output is correct
54 Correct 145 ms 29820 KB Output is correct
55 Correct 38 ms 25344 KB Output is correct
56 Correct 38 ms 25212 KB Output is correct
57 Correct 38 ms 25344 KB Output is correct
58 Correct 38 ms 25216 KB Output is correct
59 Correct 37 ms 24960 KB Output is correct
60 Correct 124 ms 27256 KB Output is correct
61 Correct 37 ms 24952 KB Output is correct
62 Correct 141 ms 29944 KB Output is correct
63 Correct 656 ms 68088 KB Output is correct
64 Correct 667 ms 68088 KB Output is correct
65 Correct 647 ms 68100 KB Output is correct
66 Correct 248 ms 33928 KB Output is correct
67 Correct 614 ms 43656 KB Output is correct
68 Correct 229 ms 34056 KB Output is correct
69 Correct 394 ms 35080 KB Output is correct
70 Correct 279 ms 34016 KB Output is correct
71 Correct 267 ms 33928 KB Output is correct
72 Correct 536 ms 38440 KB Output is correct
73 Correct 541 ms 38800 KB Output is correct
74 Correct 1048 ms 53512 KB Output is correct
75 Correct 516 ms 40456 KB Output is correct
76 Correct 768 ms 47972 KB Output is correct
77 Correct 788 ms 47880 KB Output is correct
78 Correct 220 ms 36392 KB Output is correct
79 Correct 415 ms 39424 KB Output is correct
80 Correct 202 ms 35996 KB Output is correct
81 Correct 334 ms 37688 KB Output is correct
82 Correct 804 ms 56852 KB Output is correct
83 Correct 795 ms 56600 KB Output is correct
84 Correct 593 ms 56336 KB Output is correct
85 Correct 601 ms 56528 KB Output is correct
86 Correct 482 ms 45184 KB Output is correct
87 Correct 493 ms 45764 KB Output is correct
88 Correct 533 ms 38696 KB Output is correct
89 Correct 755 ms 47152 KB Output is correct