답안 #1055786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055786 2024-08-13T05:27:01 Z ReLice Simurgh (IOI17_simurgh) C++17
100 / 100
120 ms 9040 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define ll int
#define ins insert
#define pb push_back
#define pf push_front
#define pob pop_badk()
#define pof pop_front()
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;

template<class S,class T>
bool chmin(S &a,const T &b) {
	return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
	return a<b?(a=b)==b:false;
}

const ll N = 5e2 + 7;
const ll M = N * N;

vector<pll> g(N);
vll u, v;
ll n, m;
ll p[N], d[N];
ll vis[M];
ll royal[M];
ll ct;
pair<ll,ll> mx[N];

ll pr(ll a, ll ed){
	if(a == v[ed]) return u[ed];
	if(a == u[ed]) return v[ed];
	return -1;
}

int qry(vector <int> x){
	// check
	vll p(n), siz(n);
	for(int i=0;i<n;i++){p[i]=i,siz[i]=1;}
	
	function<int(int)> anc = [&] (ll a){
		return (a==p[a] ? a : p[a] = anc(p[a]));
	};
	
	auto uni = [&] (ll a, ll b){
		a = anc(a);
		b = anc(b);
		
		if(a == b) return false;
		if(siz[a] > siz[b]) swap(a,b);
		
		siz[b] += siz[a];
		p[b] = a;
		return true;
	};
	
	for(auto i : x){
		assert(uni(u[i], v[i]));
	}
	return count_common_roads(x);
}

ll pr(ll a){return pr(a, p[a]); }

void build_tree(ll v){
	mx[v] = {d[v], -1};
	//~ cout<<v<<endl;
	for(auto [to, r] : g[v]){
		if(r == p[v])continue;
		if(d[to] != d[n]) {
			mx[v] = min(mx[v], {d[to], r});
			continue;
		}
		
		
		d[to] = d[v] + 1;
		p[to] = r;
		
		build_tree(to);
		
		mx[v] = min(mx[v], mx[to]);
	}
}

ll check(ll v, ll top){
	while(pr(v) != top){
		v = pr(v);
	}
	if(vis[p[v]]) return 2;
	return 1;
}

vll get_path(ll v, ll top){
	vll path;
	while(v != top){
		path.pb(v);
		v = pr(v);
	}
	reverse(all(path));
	return path;
}

vll get_init(){
	vll v(n - 1);
	for(ll i=1;i<n;i++){
		v[i - 1] = p[i];
	}
	return v;
}

void tp1(ll u, ll ed){// if don't know any edges
	ll i;
	ll top = pr(u, ed);
	
	vll v = get_init();
	vll path = get_path(u, top);
	
	vll res;
	ll mn = ct, mx = ct;
	for(auto i : path){
		v[i - 1] = ed;
		
		ll x = qry(v);
		res.pb(x);
		mx = max(mx, x);
		mn = min(mn, x);
		
		v[i - 1] = p[i];
	}
	
	for(i=0;i<(ll)res.size();i++){
		ll a = path[i];
		
		vis[p[a]] = 1;

		if(res[i] == mx){
			royal[p[a]] = 0;
		}
		else royal[p[a]] = 1;
	}
	
	vis[ed] = 1;
	if (ct == mx){
		royal[ed] = 0;
	}else royal[ed] = 1;
}

void tp2(ll u, ll ed){// if we know some edges
	if(vis[p[u]]) return;
	
	ll top = pr(u, ed);
	
	vll v = get_init();
	vll path = get_path(u, top);
	
	top = path[0];
	
	v[top - 1] = ed;
	ll x = qry(v);
	v[top - 1] = p[top];
	
	vis[ed] = 1;
	royal[ed] = x - ( ct - royal[p[top]] );
	
	for(auto i : path){
		if(vis[p[i]]) continue;
		vis[p[i]] = 1;
		
		v[i - 1] = ed;
		ll x = qry(v);
		v[i - 1] = p[i];
		
		// x = ct - royal[i] + royal[ed]
		// royal[i] = ct - x + royal[ed] 
		vis[p[i]] = 1;
		royal[p[i]] = ct - x + royal[ed];
	}
}

void calc(ll v){
	if(mx[v].sc == -1){
		if(p[v] >= 0){
			vis[p[v]] = 1;
			royal[p[v]] = 1;
		}
	} 
	else {
		ll top = pr(v, mx[v].sc);
		if(top >= 0 && d[top] < d[v]){
			//~ cout<<v<<' '<<top<<endl;
			if(check(v, top) == 1) tp1(v, mx[v].sc);
			else tp2(v, mx[v].sc);
		}
	}
	
	
	for(auto [to, r] : g[v]){
		if(pr(to) != v) continue;
		
		calc(to);
	}
}

void calc_tree(){
	memset(d, 0x3f, sizeof(d));
	p[0] = -1;
	d[0] = 0;
	build_tree(0);
	
	ct = qry(get_init());
	
	calc(0);
}

ll count(deque<ll> &ed, vll &v, ll m, ll u){
	ll cur = ct;
	for(ll i=0;i<m;i++){
		ll a = pr(u, ed[i]);
		
		cur -= royal[p[a]];
		
		v[a - 1] = ed[i];
	}
	ll x = qry(v);
	for(ll i=0;i<m;i++){
		ll a = pr(u, ed[i]);
		
		v[a - 1] = p[a];
	}
	return x - cur;
}

void find_others(ll u){
	vll v = get_init();
	deque<ll> ed;
	
	for(auto [to, r] : g[u]){
		if(vis[r]) continue;
		vis[r] = 1;
		ed.pb(r);
	}
	
	ll x = count(ed, v, (ll)ed.size(), u);
	
	//~ cout<<u<<'*'<<x<<endl;
	//~ for(auto i : ed) cout<<u<<' '<<pr(u, i)<<endl;
	//~ cout<<endl;
	for(ll i=1;i<=x;i++){
		ll l = 0, r = (ll)ed.size();
		while(l + 1 < r){
			ll m = (l + r) / 2;
			
			if(count(ed, v, m, u) == 0) l = m;
			else r = m;
		}
		
		royal[ed[r-1]] = 1;
		while(r--) ed.pof;
	}
	
	for(auto [to, r] : g[u]){
		if(pr(to) != u) continue;
		
		find_others(to);
	}
}

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	ll i;
	m = U.sz;
	n = N, u = U, v = V;
	
	for(i=0;i<m;i++){
		g[u[i]].pb({v[i],i});
		g[v[i]].pb({u[i],i});
	}
	
	calc_tree();
	
	//~ cout<<endl;
	//~ for(ll i=0;i<m;i++)if(vis[i])cout<<i<<' '<<royal[i]<<endl;
	
	find_others(0);
	
	//~ for(i=1;i<n;i++)cout<<u[p[i]]<<' '<<v[p[i]]<<endl;
	
	vll v;
	
	for(i=0;i<m;i++){
		//~ if(vis[i])cout<<i<<' '<<royal[i]<<endl;
		if(royal[i])v.pb(i);
	}
	
	return v;
}



# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 604 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 604 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 344 KB correct
15 Correct 1 ms 344 KB correct
16 Correct 1 ms 460 KB correct
17 Correct 1 ms 344 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 0 ms 348 KB correct
23 Correct 1 ms 452 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 344 KB correct
28 Correct 1 ms 600 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 604 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 344 KB correct
15 Correct 1 ms 344 KB correct
16 Correct 1 ms 460 KB correct
17 Correct 1 ms 344 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 0 ms 348 KB correct
23 Correct 1 ms 452 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 344 KB correct
28 Correct 1 ms 600 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 22 ms 2400 KB correct
35 Correct 20 ms 2368 KB correct
36 Correct 17 ms 2112 KB correct
37 Correct 6 ms 860 KB correct
38 Correct 27 ms 2396 KB correct
39 Correct 23 ms 2188 KB correct
40 Correct 16 ms 2140 KB correct
41 Correct 19 ms 2392 KB correct
42 Correct 17 ms 2384 KB correct
43 Correct 13 ms 1628 KB correct
44 Correct 8 ms 1372 KB correct
45 Correct 8 ms 1452 KB correct
46 Correct 8 ms 1348 KB correct
47 Correct 5 ms 860 KB correct
48 Correct 2 ms 348 KB correct
49 Correct 4 ms 604 KB correct
50 Correct 6 ms 860 KB correct
51 Correct 9 ms 1496 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 8 ms 1448 KB correct
54 Correct 9 ms 1704 KB correct
55 Correct 8 ms 1432 KB correct
56 Correct 9 ms 1628 KB correct
57 Correct 8 ms 1628 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 66 ms 6268 KB correct
4 Correct 105 ms 8784 KB correct
5 Correct 120 ms 8864 KB correct
6 Correct 79 ms 9040 KB correct
7 Correct 91 ms 8788 KB correct
8 Correct 89 ms 8788 KB correct
9 Correct 100 ms 8784 KB correct
10 Correct 101 ms 8784 KB correct
11 Correct 108 ms 8788 KB correct
12 Correct 103 ms 8760 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 90 ms 8928 KB correct
15 Correct 101 ms 8784 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 604 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 344 KB correct
15 Correct 1 ms 344 KB correct
16 Correct 1 ms 460 KB correct
17 Correct 1 ms 344 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 0 ms 348 KB correct
23 Correct 1 ms 452 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 344 KB correct
28 Correct 1 ms 600 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 22 ms 2400 KB correct
35 Correct 20 ms 2368 KB correct
36 Correct 17 ms 2112 KB correct
37 Correct 6 ms 860 KB correct
38 Correct 27 ms 2396 KB correct
39 Correct 23 ms 2188 KB correct
40 Correct 16 ms 2140 KB correct
41 Correct 19 ms 2392 KB correct
42 Correct 17 ms 2384 KB correct
43 Correct 13 ms 1628 KB correct
44 Correct 8 ms 1372 KB correct
45 Correct 8 ms 1452 KB correct
46 Correct 8 ms 1348 KB correct
47 Correct 5 ms 860 KB correct
48 Correct 2 ms 348 KB correct
49 Correct 4 ms 604 KB correct
50 Correct 6 ms 860 KB correct
51 Correct 9 ms 1496 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 8 ms 1448 KB correct
54 Correct 9 ms 1704 KB correct
55 Correct 8 ms 1432 KB correct
56 Correct 9 ms 1628 KB correct
57 Correct 8 ms 1628 KB correct
58 Correct 1 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 66 ms 6268 KB correct
61 Correct 105 ms 8784 KB correct
62 Correct 120 ms 8864 KB correct
63 Correct 79 ms 9040 KB correct
64 Correct 91 ms 8788 KB correct
65 Correct 89 ms 8788 KB correct
66 Correct 100 ms 8784 KB correct
67 Correct 101 ms 8784 KB correct
68 Correct 108 ms 8788 KB correct
69 Correct 103 ms 8760 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 90 ms 8928 KB correct
72 Correct 101 ms 8784 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 100 ms 8904 KB correct
75 Correct 99 ms 8788 KB correct
76 Correct 50 ms 3924 KB correct
77 Correct 106 ms 8928 KB correct
78 Correct 101 ms 8792 KB correct
79 Correct 100 ms 8784 KB correct
80 Correct 101 ms 8556 KB correct
81 Correct 80 ms 8020 KB correct
82 Correct 99 ms 8528 KB correct
83 Correct 79 ms 5540 KB correct
84 Correct 43 ms 6224 KB correct
85 Correct 42 ms 5716 KB correct
86 Correct 38 ms 4008 KB correct
87 Correct 31 ms 3412 KB correct
88 Correct 27 ms 2652 KB correct
89 Correct 27 ms 2648 KB correct
90 Correct 24 ms 2136 KB correct
91 Correct 14 ms 860 KB correct
92 Correct 10 ms 604 KB correct
93 Correct 40 ms 5744 KB correct
94 Correct 34 ms 4180 KB correct
95 Correct 35 ms 4180 KB correct
96 Correct 27 ms 2548 KB correct
97 Correct 27 ms 2644 KB correct
98 Correct 29 ms 3152 KB correct
99 Correct 26 ms 2652 KB correct
100 Correct 17 ms 1372 KB correct
101 Correct 10 ms 1544 KB correct
102 Correct 38 ms 5212 KB correct
103 Correct 40 ms 5204 KB correct
104 Correct 39 ms 5200 KB correct
105 Correct 38 ms 5212 KB correct