답안 #269303

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
269303 2020-08-17T06:21:37 Z 송준혁(#5099) 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1089 ms 45564 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, M;
int P[101010];
LL ans;
set<int> S[101010], T[101010];
vector<int> U, V;

int rt(int u){
	if (P[u] < 0) return u;
	return P[u] = rt(P[u]);
}

void Union(int u, int v){
	if (rt(u) == rt(v)) return;
	u=rt(u), v=rt(v);
	if (S[u].size() < S[v].size()) swap(u, v);
	ans -= P[u]*(LL)S[v].size() + P[v]*(LL)S[u].size();
	for (int t : S[v]){
		if (T[u].find(rt(t)) != T[u].end()) U.push_back(u), V.push_back(t);

		if (S[u].find(t) != S[u].end()) ans += P[u] + P[v];
		else S[u].insert(t);

		T[rt(t)].erase(v);
		T[rt(t)].insert(u);
	}
	for (int t : T[v]){
		if (T[rt(t)].find(u) != T[rt(t)].end()) U.push_back(u), V.push_back(t);
		T[u].insert(t);
	}
	S[v].clear(), T[v].clear();
	P[u] += P[v], P[v]=u;
}

int main(){
	scanf("%d %d", &N, &M);
	for (int i=1; i<=N; i++) P[i]=-1, S[i].insert(i);
	for (int i=1; i<=M; i++){
		int x, y;
		scanf("%d %d", &x, &y);
		int u=rt(x), v=rt(y);
		if (S[v].find(x) != S[v].end()){
			printf("%lld\n", ans);
			continue;
		}
		if (T[v].find(u) != T[v].end()){
			U.push_back(u), V.push_back(v);
			while (U.size()){
				u=U.back(), v=V.back();
				U.pop_back(), V.pop_back();
				Union(u, v);
			}
		}
		else{
			ans -= P[v];
			T[u].insert(v);
			S[v].insert(x);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |  scanf("%d %d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 8 ms 9856 KB Output is correct
3 Correct 9 ms 9856 KB Output is correct
4 Correct 8 ms 9856 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 7 ms 9856 KB Output is correct
12 Correct 7 ms 9856 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 7 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9856 KB Output is correct
22 Correct 7 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 7 ms 9856 KB Output is correct
26 Correct 7 ms 9856 KB Output is correct
27 Correct 8 ms 9856 KB Output is correct
28 Correct 7 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 7 ms 9856 KB Output is correct
31 Correct 8 ms 9856 KB Output is correct
32 Correct 9 ms 9856 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 8 ms 9856 KB Output is correct
3 Correct 9 ms 9856 KB Output is correct
4 Correct 8 ms 9856 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 7 ms 9856 KB Output is correct
12 Correct 7 ms 9856 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 7 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9856 KB Output is correct
22 Correct 7 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 7 ms 9856 KB Output is correct
26 Correct 7 ms 9856 KB Output is correct
27 Correct 8 ms 9856 KB Output is correct
28 Correct 7 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 7 ms 9856 KB Output is correct
31 Correct 8 ms 9856 KB Output is correct
32 Correct 9 ms 9856 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
34 Correct 13 ms 9984 KB Output is correct
35 Correct 165 ms 13276 KB Output is correct
36 Correct 190 ms 15112 KB Output is correct
37 Correct 194 ms 15064 KB Output is correct
38 Correct 182 ms 14840 KB Output is correct
39 Correct 10 ms 9984 KB Output is correct
40 Correct 13 ms 10112 KB Output is correct
41 Correct 12 ms 10112 KB Output is correct
42 Correct 12 ms 9984 KB Output is correct
43 Correct 13 ms 10112 KB Output is correct
44 Correct 11 ms 10112 KB Output is correct
45 Correct 12 ms 9984 KB Output is correct
46 Correct 13 ms 9984 KB Output is correct
47 Correct 14 ms 10112 KB Output is correct
48 Correct 12 ms 10112 KB Output is correct
49 Correct 18 ms 10624 KB Output is correct
50 Correct 184 ms 15224 KB Output is correct
51 Correct 15 ms 10240 KB Output is correct
52 Correct 172 ms 13944 KB Output is correct
53 Correct 23 ms 10616 KB Output is correct
54 Correct 182 ms 14556 KB Output is correct
55 Correct 13 ms 10368 KB Output is correct
56 Correct 15 ms 10368 KB Output is correct
57 Correct 16 ms 10368 KB Output is correct
58 Correct 15 ms 10368 KB Output is correct
59 Correct 12 ms 9984 KB Output is correct
60 Correct 158 ms 12792 KB Output is correct
61 Correct 15 ms 10112 KB Output is correct
62 Correct 194 ms 14824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 8 ms 9856 KB Output is correct
3 Correct 9 ms 9856 KB Output is correct
4 Correct 8 ms 9856 KB Output is correct
5 Correct 8 ms 9856 KB Output is correct
6 Correct 9 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 7 ms 9856 KB Output is correct
12 Correct 7 ms 9856 KB Output is correct
13 Correct 7 ms 9856 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9856 KB Output is correct
16 Correct 7 ms 9856 KB Output is correct
17 Correct 7 ms 9856 KB Output is correct
18 Correct 8 ms 9856 KB Output is correct
19 Correct 7 ms 9856 KB Output is correct
20 Correct 7 ms 9856 KB Output is correct
21 Correct 9 ms 9856 KB Output is correct
22 Correct 7 ms 9856 KB Output is correct
23 Correct 7 ms 9856 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 7 ms 9856 KB Output is correct
26 Correct 7 ms 9856 KB Output is correct
27 Correct 8 ms 9856 KB Output is correct
28 Correct 7 ms 9856 KB Output is correct
29 Correct 7 ms 9856 KB Output is correct
30 Correct 7 ms 9856 KB Output is correct
31 Correct 8 ms 9856 KB Output is correct
32 Correct 9 ms 9856 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
34 Correct 13 ms 9984 KB Output is correct
35 Correct 165 ms 13276 KB Output is correct
36 Correct 190 ms 15112 KB Output is correct
37 Correct 194 ms 15064 KB Output is correct
38 Correct 182 ms 14840 KB Output is correct
39 Correct 10 ms 9984 KB Output is correct
40 Correct 13 ms 10112 KB Output is correct
41 Correct 12 ms 10112 KB Output is correct
42 Correct 12 ms 9984 KB Output is correct
43 Correct 13 ms 10112 KB Output is correct
44 Correct 11 ms 10112 KB Output is correct
45 Correct 12 ms 9984 KB Output is correct
46 Correct 13 ms 9984 KB Output is correct
47 Correct 14 ms 10112 KB Output is correct
48 Correct 12 ms 10112 KB Output is correct
49 Correct 18 ms 10624 KB Output is correct
50 Correct 184 ms 15224 KB Output is correct
51 Correct 15 ms 10240 KB Output is correct
52 Correct 172 ms 13944 KB Output is correct
53 Correct 23 ms 10616 KB Output is correct
54 Correct 182 ms 14556 KB Output is correct
55 Correct 13 ms 10368 KB Output is correct
56 Correct 15 ms 10368 KB Output is correct
57 Correct 16 ms 10368 KB Output is correct
58 Correct 15 ms 10368 KB Output is correct
59 Correct 12 ms 9984 KB Output is correct
60 Correct 158 ms 12792 KB Output is correct
61 Correct 15 ms 10112 KB Output is correct
62 Correct 194 ms 14824 KB Output is correct
63 Correct 420 ms 45564 KB Output is correct
64 Correct 429 ms 45304 KB Output is correct
65 Correct 447 ms 45432 KB Output is correct
66 Correct 266 ms 17276 KB Output is correct
67 Correct 590 ms 22776 KB Output is correct
68 Correct 280 ms 17272 KB Output is correct
69 Correct 413 ms 22136 KB Output is correct
70 Correct 282 ms 17400 KB Output is correct
71 Correct 326 ms 17272 KB Output is correct
72 Correct 501 ms 22224 KB Output is correct
73 Correct 516 ms 22288 KB Output is correct
74 Correct 1089 ms 30692 KB Output is correct
75 Correct 637 ms 25452 KB Output is correct
76 Correct 850 ms 29180 KB Output is correct
77 Correct 803 ms 29476 KB Output is correct
78 Correct 250 ms 21240 KB Output is correct
79 Correct 607 ms 23024 KB Output is correct
80 Correct 208 ms 21368 KB Output is correct
81 Correct 382 ms 22904 KB Output is correct
82 Correct 708 ms 35504 KB Output is correct
83 Correct 789 ms 35372 KB Output is correct
84 Correct 726 ms 36620 KB Output is correct
85 Correct 681 ms 37104 KB Output is correct
86 Correct 604 ms 19404 KB Output is correct
87 Correct 676 ms 19936 KB Output is correct
88 Correct 637 ms 22136 KB Output is correct
89 Correct 774 ms 28668 KB Output is correct