답안 #1026586

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1026586 2024-07-18T08:17:40 Z Gray 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
576 ms 68436 KB
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
#include <algorithm>
#include <climits>
#include <ios>
#include<iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define ff first
#define ss second
#define ull unsigned ll
#define ln "\n"
#define pll pair<ll, ll>
#define INF 2e18
using namespace std;

struct DSU{
	vector<set<ll>> h_in, h_out, lead;
	vector<ll> par, sz;
	ll n, ecnt;
	DSU(ll N){
		n=N;
		ecnt=0;
		par.resize(n+1, -1);
		h_in=h_out=lead = vector<set<ll>>(n+1);
		for (ll i=1; i<=n; i++) lead[i].insert(i);
		sz.resize(n+1, 1);
	}
	void debug(){
		return;
		for (ll i=1; i<=n; i++){
			cout << i << "::\n";
			cout << "lead: ";
			for (auto ch:lead[i]) cout << ch << ' ';
			cout << "\nin: ";
			for (auto ch:h_in[i]) cout << ch << ' ';
			cout << "\nout: ";
			for (auto ch:h_out[i]) cout << ch << ' ';
			cout << ln << ln;
		}
	}
	queue<pair<ll, ll>> unity;
	ll get(ll u){
		return par[u]==-1?u:par[u]=get(par[u]);
	}

	void bind(ll pu, ll pv){
		h_in[pv].insert(pu);
		h_out[pu].insert(pv);
		if (h_out[pv].count(pu)){
			unity.push({pu, pv});
		}
	}
	void link(ll u, ll v){
		ll pu = get(u), pv=get(v);
		if (pu==pv or lead[pv].count(u)){
			return;
		}else{
			lead[pv].insert(u);
			ecnt+=sz[pv];
			bind(pu, pv);
			proc_unities();
		}
		// debug();
	}
	void unite(ll u, ll v){
		u=get(u); v=get(v);
		if (u==v) return;
		if (h_in[u].size()+h_out[u].size()+lead[u].size()<h_in[v].size()+h_out[v].size()+lead[v].size()) swap(u, v);
		par[v]=u;
		// cout << "PDEB: " << ecnt << ":" << sz[u] << ' ' << sz[v] << ln;
		ecnt+=sz[u]*lead[v].size()+sz[v]*lead[u].size();
		sz[u]+=sz[v];
		// cout << "Unite: " << v << "->" << u << ln;
		// cout << "PDEB: " << ecnt << ln;
		for (auto mem:lead[v]){
			ecnt-=sz[u]*lead[u].count(mem);
			lead[u].insert(mem);
		}
		lead[v].clear();
		h_in[v].erase(u); h_out[u].erase(v);
		h_in[u].erase(v); h_out[v].erase(u);
		for (auto mem:h_in[v]){
			h_out[mem].erase(v);
			bind(mem, u);
		}
		h_in[v].clear();
		for (auto mem:h_out[v]){
			h_in[mem].erase(v);
			bind(u, mem);
		}
		h_out[v].clear();
	}
	void proc_unities(){
		while (!unity.empty()){
			auto cur = unity.front();
			unity.pop();
			unite(cur.ff, cur.ss);
		}
	}
};

void solve(){
	ll n, m; 
	cin >> n >> m;
	DSU tr(n);
	for (ll i=0; i<m; i++){
		ll u, v; cin >> u >> v;
		tr.link(u, v);
		cout << tr.ecnt << ln;
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(nullptr);
	ll t=1;
	// cin >> t;
	while (t--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 456 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 456 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 47 ms 6228 KB Output is correct
36 Correct 76 ms 9080 KB Output is correct
37 Correct 72 ms 9300 KB Output is correct
38 Correct 73 ms 9004 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 3 ms 860 KB Output is correct
41 Correct 3 ms 860 KB Output is correct
42 Correct 4 ms 724 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 3 ms 1048 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 3 ms 860 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 860 KB Output is correct
49 Correct 8 ms 1628 KB Output is correct
50 Correct 72 ms 9272 KB Output is correct
51 Correct 5 ms 1116 KB Output is correct
52 Correct 57 ms 7628 KB Output is correct
53 Correct 7 ms 1628 KB Output is correct
54 Correct 61 ms 8348 KB Output is correct
55 Correct 5 ms 1372 KB Output is correct
56 Correct 5 ms 1416 KB Output is correct
57 Correct 4 ms 1372 KB Output is correct
58 Correct 4 ms 1480 KB Output is correct
59 Correct 3 ms 860 KB Output is correct
60 Correct 50 ms 5732 KB Output is correct
61 Correct 4 ms 1116 KB Output is correct
62 Correct 65 ms 8748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 456 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 47 ms 6228 KB Output is correct
36 Correct 76 ms 9080 KB Output is correct
37 Correct 72 ms 9300 KB Output is correct
38 Correct 73 ms 9004 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 3 ms 860 KB Output is correct
41 Correct 3 ms 860 KB Output is correct
42 Correct 4 ms 724 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 3 ms 1048 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 3 ms 860 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 860 KB Output is correct
49 Correct 8 ms 1628 KB Output is correct
50 Correct 72 ms 9272 KB Output is correct
51 Correct 5 ms 1116 KB Output is correct
52 Correct 57 ms 7628 KB Output is correct
53 Correct 7 ms 1628 KB Output is correct
54 Correct 61 ms 8348 KB Output is correct
55 Correct 5 ms 1372 KB Output is correct
56 Correct 5 ms 1416 KB Output is correct
57 Correct 4 ms 1372 KB Output is correct
58 Correct 4 ms 1480 KB Output is correct
59 Correct 3 ms 860 KB Output is correct
60 Correct 50 ms 5732 KB Output is correct
61 Correct 4 ms 1116 KB Output is correct
62 Correct 65 ms 8748 KB Output is correct
63 Correct 332 ms 68436 KB Output is correct
64 Correct 351 ms 68372 KB Output is correct
65 Correct 341 ms 68332 KB Output is correct
66 Correct 123 ms 25176 KB Output is correct
67 Correct 224 ms 31312 KB Output is correct
68 Correct 144 ms 25020 KB Output is correct
69 Correct 245 ms 32084 KB Output is correct
70 Correct 151 ms 25136 KB Output is correct
71 Correct 135 ms 25172 KB Output is correct
72 Correct 217 ms 31576 KB Output is correct
73 Correct 241 ms 31524 KB Output is correct
74 Correct 572 ms 43092 KB Output is correct
75 Correct 333 ms 37972 KB Output is correct
76 Correct 420 ms 42212 KB Output is correct
77 Correct 423 ms 42324 KB Output is correct
78 Correct 138 ms 31004 KB Output is correct
79 Correct 256 ms 34132 KB Output is correct
80 Correct 131 ms 31060 KB Output is correct
81 Correct 211 ms 34132 KB Output is correct
82 Correct 514 ms 52564 KB Output is correct
83 Correct 576 ms 52568 KB Output is correct
84 Correct 485 ms 52460 KB Output is correct
85 Correct 415 ms 52440 KB Output is correct
86 Correct 224 ms 25940 KB Output is correct
87 Correct 195 ms 28224 KB Output is correct
88 Correct 234 ms 31648 KB Output is correct
89 Correct 385 ms 42128 KB Output is correct