답안 #573376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
573376 2022-06-06T13:08:44 Z hollwo_pelw 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1345 ms 78352 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
 
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
 
void Hollwo_Pelw();
 
signed main(){
#ifndef hollwo_pelw_local
	if (fopen(".inp", "r"))
		assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
	using namespace chrono;
	auto start = steady_clock::now();
#endif
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = steady_clock::now();
	cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
 
const int N = 1e5 + 5;
 
int n, m, par[N];
long long ans, sz[N];
set<int> adj[N], rev[N], fol[N];
queue<pair<int, int>> to_merge;
 
void weakedge(int u, int v){
	adj[u].insert(v);
	rev[v].insert(u);
	// if now strong 
	if (adj[v].count(u))
		to_merge.push({u, v});
}
 
int find(int u){
	return par[u] = (par[u] == u ? u : find(par[u]));
}
 
int dsize(int u){
	return fol[u].size() + rev[u].size() + adj[u].size();
}
 
void merge(int u, int v){
	if ((u = find(u)) == (v = find(v))) return ;
	if (dsize(u) < dsize(v)) swap(u, v);
	ans += sz[u] * fol[v].size()
		 + sz[v] * fol[u].size();
	par[v] = u;
	sz[u] += sz[v];
	
	for (auto f : fol[v]){
		if (fol[u].count(f))
			ans -= sz[u];
		else 
			fol[u].insert(f);
	}
	
	adj[u].erase(v), rev[v].erase(u);
	adj[v].erase(u), rev[u].erase(v);
	
	for (auto f : adj[v]){
		rev[f].erase(v);
		weakedge(u, f);
	}
	for (auto f : rev[v]){
		adj[f].erase(u);
		weakedge(f, u);
	}
}
 
void Hollwo_Pelw(){
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		par[i] = i;
		sz[i] = 1;
		fol[i].insert(i);
	}
	for (int u, v; m --; ){
		cin >> u >> v;
		int pu = find(u), pv = find(v);
		if (pu != pv && !fol[pv].count(u)){
			fol[pv].insert(u);
			ans += sz[pv];
			weakedge(pu, pv);
			while(!to_merge.empty()){
				auto[uu, vv] = to_merge.front();
				to_merge.pop();
				merge(uu, vv);
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14388 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14432 KB Output is correct
9 Correct 9 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14424 KB Output is correct
14 Correct 8 ms 14372 KB Output is correct
15 Correct 8 ms 14388 KB Output is correct
16 Correct 9 ms 14392 KB Output is correct
17 Correct 10 ms 14428 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 8 ms 14564 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 10 ms 14416 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 9 ms 14424 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 8 ms 14444 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14420 KB Output is correct
31 Correct 8 ms 14428 KB Output is correct
32 Correct 8 ms 14524 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14388 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14432 KB Output is correct
9 Correct 9 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14424 KB Output is correct
14 Correct 8 ms 14372 KB Output is correct
15 Correct 8 ms 14388 KB Output is correct
16 Correct 9 ms 14392 KB Output is correct
17 Correct 10 ms 14428 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 8 ms 14564 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 10 ms 14416 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 9 ms 14424 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 8 ms 14444 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14420 KB Output is correct
31 Correct 8 ms 14428 KB Output is correct
32 Correct 8 ms 14524 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 84 ms 20236 KB Output is correct
36 Correct 103 ms 23200 KB Output is correct
37 Correct 104 ms 23304 KB Output is correct
38 Correct 110 ms 22996 KB Output is correct
39 Correct 9 ms 14760 KB Output is correct
40 Correct 14 ms 15204 KB Output is correct
41 Correct 10 ms 15160 KB Output is correct
42 Correct 9 ms 14884 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 10 ms 14816 KB Output is correct
45 Correct 10 ms 14688 KB Output is correct
46 Correct 10 ms 14760 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 10 ms 14932 KB Output is correct
49 Correct 17 ms 15844 KB Output is correct
50 Correct 107 ms 23468 KB Output is correct
51 Correct 13 ms 15060 KB Output is correct
52 Correct 87 ms 21488 KB Output is correct
53 Correct 21 ms 15716 KB Output is correct
54 Correct 93 ms 22392 KB Output is correct
55 Correct 12 ms 15224 KB Output is correct
56 Correct 14 ms 15316 KB Output is correct
57 Correct 13 ms 15188 KB Output is correct
58 Correct 12 ms 15200 KB Output is correct
59 Correct 11 ms 15152 KB Output is correct
60 Correct 70 ms 20120 KB Output is correct
61 Correct 12 ms 15060 KB Output is correct
62 Correct 102 ms 22668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14388 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14432 KB Output is correct
9 Correct 9 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 8 ms 14424 KB Output is correct
14 Correct 8 ms 14372 KB Output is correct
15 Correct 8 ms 14388 KB Output is correct
16 Correct 9 ms 14392 KB Output is correct
17 Correct 10 ms 14428 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 8 ms 14564 KB Output is correct
22 Correct 7 ms 14424 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 10 ms 14416 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 9 ms 14424 KB Output is correct
27 Correct 7 ms 14416 KB Output is correct
28 Correct 8 ms 14444 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14420 KB Output is correct
31 Correct 8 ms 14428 KB Output is correct
32 Correct 8 ms 14524 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 84 ms 20236 KB Output is correct
36 Correct 103 ms 23200 KB Output is correct
37 Correct 104 ms 23304 KB Output is correct
38 Correct 110 ms 22996 KB Output is correct
39 Correct 9 ms 14760 KB Output is correct
40 Correct 14 ms 15204 KB Output is correct
41 Correct 10 ms 15160 KB Output is correct
42 Correct 9 ms 14884 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 10 ms 14816 KB Output is correct
45 Correct 10 ms 14688 KB Output is correct
46 Correct 10 ms 14760 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 10 ms 14932 KB Output is correct
49 Correct 17 ms 15844 KB Output is correct
50 Correct 107 ms 23468 KB Output is correct
51 Correct 13 ms 15060 KB Output is correct
52 Correct 87 ms 21488 KB Output is correct
53 Correct 21 ms 15716 KB Output is correct
54 Correct 93 ms 22392 KB Output is correct
55 Correct 12 ms 15224 KB Output is correct
56 Correct 14 ms 15316 KB Output is correct
57 Correct 13 ms 15188 KB Output is correct
58 Correct 12 ms 15200 KB Output is correct
59 Correct 11 ms 15152 KB Output is correct
60 Correct 70 ms 20120 KB Output is correct
61 Correct 12 ms 15060 KB Output is correct
62 Correct 102 ms 22668 KB Output is correct
63 Correct 520 ms 67916 KB Output is correct
64 Correct 533 ms 67988 KB Output is correct
65 Correct 539 ms 67964 KB Output is correct
66 Correct 209 ms 33968 KB Output is correct
67 Correct 514 ms 69040 KB Output is correct
68 Correct 199 ms 33996 KB Output is correct
69 Correct 371 ms 35120 KB Output is correct
70 Correct 246 ms 34020 KB Output is correct
71 Correct 237 ms 34004 KB Output is correct
72 Correct 532 ms 47288 KB Output is correct
73 Correct 555 ms 49216 KB Output is correct
74 Correct 1345 ms 78352 KB Output is correct
75 Correct 607 ms 43252 KB Output is correct
76 Correct 914 ms 57620 KB Output is correct
77 Correct 924 ms 57924 KB Output is correct
78 Correct 255 ms 40004 KB Output is correct
79 Correct 446 ms 44984 KB Output is correct
80 Correct 252 ms 36496 KB Output is correct
81 Correct 368 ms 40204 KB Output is correct
82 Correct 1002 ms 57156 KB Output is correct
83 Correct 990 ms 56964 KB Output is correct
84 Correct 772 ms 56128 KB Output is correct
85 Correct 798 ms 56224 KB Output is correct
86 Correct 466 ms 66848 KB Output is correct
87 Correct 443 ms 69144 KB Output is correct
88 Correct 584 ms 48400 KB Output is correct
89 Correct 876 ms 55892 KB Output is correct