답안 #856874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856874 2023-10-04T18:26:29 Z serifefedartar 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
890 ms 69084 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 18; 
const ll INF = 1e15;
const ll MAXN = 1e5 + 5;
 
vector<set<int>> graph, rev;
queue<pair<int,int>> q;
set<int> in[MAXN];
int par[MAXN];
ll sz[MAXN];
ll ans = 0;
int find(int node) {
	if (par[node] == node)
		return node;
	return par[node] = find(par[node]);
}
 
void conn(int from, int to) {
	graph[from].insert(to);
	rev[to].insert(from);
	if (graph[to].count(from))
		q.push({from, to});	
}
 
void merge(int x, int y) {
	if (x == y)
		return ;
	if (graph[x].size() + rev[x].size() + in[x].size() < graph[y].size() + rev[y].size() + in[y].size())
 		swap(x, y);
 
	ans += in[y].size() * sz[x] + in[x].size() * sz[y]; 
	sz[x] += sz[y];
	par[y] = x;
 		
	for (auto u : in[y]) {
		if (in[x].count(u) == 0)
			in[x].insert(u);
		else
			ans -= sz[x];
	}
 
	graph[x].erase(y);
	graph[y].erase(x);
	rev[x].erase(y); 
	rev[y].erase(x);
	for (auto u : graph[y]) {
		rev[u].erase(y);
		conn(x, u); 
	}
	for (auto u : rev[y]) {
		graph[u].erase(y);
		conn(u, x);
	}
}
 
int main() {
	fast
	int N, M, a, b;
	cin >> N >> M;
 
	for (int i = 1; i <= N; i++) {
		par[i] = i, sz[i] = 1;
		in[i].insert(i);
	}
	graph = vector<set<int>>(N+1, set<int>());
	rev = vector<set<int>>(N+1, set<int>());
 
	for (int i = 1; i <= M; i++) {
		cin >> a >> b;
		int par_a = find(a);
		int par_b = find(b);
 
		if (par_b == par_a) {
			cout << ans << "\n";
			continue;
		}
		if (in[par_b].count(a) == 0) {
			in[par_b].insert(a);
			ans += sz[par_b];
 
			conn(par_a, par_b);
			while (!q.empty()) {
				int from = q.front().f;
				int to = q.front().s;
				q.pop();
				merge(find(from), find(to));
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 5976 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 1 ms 6128 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 6196 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 6108 KB Output is correct
26 Correct 2 ms 5976 KB Output is correct
27 Correct 2 ms 5976 KB Output is correct
28 Correct 2 ms 5992 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 1 ms 5980 KB Output is correct
31 Correct 2 ms 5980 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 5976 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 1 ms 6128 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 6196 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 6108 KB Output is correct
26 Correct 2 ms 5976 KB Output is correct
27 Correct 2 ms 5976 KB Output is correct
28 Correct 2 ms 5992 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 1 ms 5980 KB Output is correct
31 Correct 2 ms 5980 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 4 ms 6232 KB Output is correct
35 Correct 58 ms 12092 KB Output is correct
36 Correct 78 ms 14928 KB Output is correct
37 Correct 81 ms 14932 KB Output is correct
38 Correct 75 ms 14508 KB Output is correct
39 Correct 3 ms 6492 KB Output is correct
40 Correct 5 ms 7004 KB Output is correct
41 Correct 5 ms 7004 KB Output is correct
42 Correct 3 ms 6492 KB Output is correct
43 Correct 4 ms 6492 KB Output is correct
44 Correct 4 ms 6488 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 4 ms 6488 KB Output is correct
47 Correct 5 ms 6688 KB Output is correct
48 Correct 5 ms 6748 KB Output is correct
49 Correct 10 ms 7516 KB Output is correct
50 Correct 80 ms 15100 KB Output is correct
51 Correct 7 ms 7004 KB Output is correct
52 Correct 66 ms 13212 KB Output is correct
53 Correct 10 ms 7516 KB Output is correct
54 Correct 71 ms 14232 KB Output is correct
55 Correct 6 ms 7004 KB Output is correct
56 Correct 6 ms 7004 KB Output is correct
57 Correct 7 ms 7028 KB Output is correct
58 Correct 6 ms 6936 KB Output is correct
59 Correct 5 ms 7000 KB Output is correct
60 Correct 56 ms 11912 KB Output is correct
61 Correct 5 ms 6748 KB Output is correct
62 Correct 75 ms 14544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 5976 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 1 ms 6128 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 2 ms 6196 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 6108 KB Output is correct
26 Correct 2 ms 5976 KB Output is correct
27 Correct 2 ms 5976 KB Output is correct
28 Correct 2 ms 5992 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 1 ms 5980 KB Output is correct
31 Correct 2 ms 5980 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 4 ms 6232 KB Output is correct
35 Correct 58 ms 12092 KB Output is correct
36 Correct 78 ms 14928 KB Output is correct
37 Correct 81 ms 14932 KB Output is correct
38 Correct 75 ms 14508 KB Output is correct
39 Correct 3 ms 6492 KB Output is correct
40 Correct 5 ms 7004 KB Output is correct
41 Correct 5 ms 7004 KB Output is correct
42 Correct 3 ms 6492 KB Output is correct
43 Correct 4 ms 6492 KB Output is correct
44 Correct 4 ms 6488 KB Output is correct
45 Correct 3 ms 6492 KB Output is correct
46 Correct 4 ms 6488 KB Output is correct
47 Correct 5 ms 6688 KB Output is correct
48 Correct 5 ms 6748 KB Output is correct
49 Correct 10 ms 7516 KB Output is correct
50 Correct 80 ms 15100 KB Output is correct
51 Correct 7 ms 7004 KB Output is correct
52 Correct 66 ms 13212 KB Output is correct
53 Correct 10 ms 7516 KB Output is correct
54 Correct 71 ms 14232 KB Output is correct
55 Correct 6 ms 7004 KB Output is correct
56 Correct 6 ms 7004 KB Output is correct
57 Correct 7 ms 7028 KB Output is correct
58 Correct 6 ms 6936 KB Output is correct
59 Correct 5 ms 7000 KB Output is correct
60 Correct 56 ms 11912 KB Output is correct
61 Correct 5 ms 6748 KB Output is correct
62 Correct 75 ms 14544 KB Output is correct
63 Correct 449 ms 67916 KB Output is correct
64 Correct 468 ms 68212 KB Output is correct
65 Correct 452 ms 67920 KB Output is correct
66 Correct 160 ms 34128 KB Output is correct
67 Correct 332 ms 60920 KB Output is correct
68 Correct 149 ms 34076 KB Output is correct
69 Correct 265 ms 35344 KB Output is correct
70 Correct 172 ms 33996 KB Output is correct
71 Correct 174 ms 34240 KB Output is correct
72 Correct 307 ms 45136 KB Output is correct
73 Correct 342 ms 46672 KB Output is correct
74 Correct 890 ms 68792 KB Output is correct
75 Correct 413 ms 42092 KB Output is correct
76 Correct 572 ms 54100 KB Output is correct
77 Correct 568 ms 54452 KB Output is correct
78 Correct 173 ms 37652 KB Output is correct
79 Correct 290 ms 42180 KB Output is correct
80 Correct 163 ms 36564 KB Output is correct
81 Correct 267 ms 40312 KB Output is correct
82 Correct 605 ms 54612 KB Output is correct
83 Correct 618 ms 54780 KB Output is correct
84 Correct 495 ms 53728 KB Output is correct
85 Correct 475 ms 53712 KB Output is correct
86 Correct 252 ms 66900 KB Output is correct
87 Correct 274 ms 69084 KB Output is correct
88 Correct 321 ms 45904 KB Output is correct
89 Correct 543 ms 52256 KB Output is correct