Submission #1052974

# Submission time Handle Problem Language Result Execution time Memory
1052974 2024-08-11T06:58:26 Z Alihan_8 Simurgh (IOI17_simurgh) C++17
100 / 100
349 ms 22668 KB
#include "simurgh.h"

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define pb push_back
#define ar array
#define ln '\n'

template <class F, class S>
bool chmin(F &u, const S &v){
	bool flag = false;
	
	if ( u > v ){
		u = v; flag |= true;
	}
	
	return flag;
}

map <vector<int>,int> dp;

int qry(vector <int> x){
	sort(all(x));
	
	if ( !dp.count(x) ){
		dp[x] = count_common_roads(x);
	}
	
	return dp[x];
}

int ask(set <int> s){
	vector <int> x;
	
	for ( auto &u: s ) x.pb(u);
	
	return qry(x);
}

vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V){
	int m = U.size();
	
	vector <vector<ar<int,2>>> adj(n);
	
	for ( int i = 0; i < m; i++ ){
		adj[U[i]].pb({V[i], i});
		adj[V[i]].pb({U[i], i});
	}
	
	vector <int> f(m, -1), d(n, -1), is(m);
	vector <ar<int,2>> fa(n, {-1, -1}), b(n);
	
	auto dfs = [&](auto dfs, int u, int p) -> void{
		ar <int,2> back = {n + 1, -1};
	
		for ( auto &[v, j]: adj[u] ){
			if ( v != p ){
				if ( d[v] == -1 ){
					d[v] = d[u] + 1;
					fa[v] = {u, j};
					is[j] = 1;
					
					dfs(dfs, v, u);
				} else if ( d[v] < d[u] ){ 
					if ( back[1] == -1 || d[back[0]] > d[v] ){
						back = {v, j};
					}
				}
			}
		}
		
		b[u] = back;
	};
	
	d[0] = 0;
	
	dfs(dfs, 0, -1);
	
	set <int> et;
	
	for ( int i = 0; i < m; i++ ){
		if ( is[i] ) et.insert(i);
	}
	
	int C = ask(et);
	
	auto dfs2 = [&](auto dfs2, int u, int p) -> void{
		for ( auto &[v, j]: adj[u] ){
			if ( v != p && is[j] ){
				dfs2(dfs2, v, u);
			}
		}
		
		int j = fa[u][1];
		
		if ( j == -1 ) return;
		
		auto &bb = b[u];
		
		if ( bb[1] == -1 ){
			if ( f[j] == -1 ) f[j] = 1;
		} else{			
			vector <int> p;
			
			int v = u;
			
			while ( v != bb[0] ){
				p.pb(fa[v][1]);
				v = fa[v][0];
			}
			
			sort(all(p), [&](int &u, int &v){
				return f[u] > f[v];
			});
			
			vector <int> R;
			
			int tmp = -1;
			
			et.insert(bb[1]);
			
			for ( auto &j: p ){
				et.erase(j);
				
				if ( f[j] != -1 ){
					if ( tmp == -1 ){
						tmp = ask(et);
					
						if ( tmp == C ){
							f[bb[1]] = f[j];
						} else{
							f[bb[1]] = (tmp > C);
							assert(f[bb[1]] != f[j]);
						}
					}
				} else{
					int q = ask(et);
					
					if ( q == C ){
						R.pb(j); // f[j] == f[bb[1]]
					} else{
						f[bb[1]] = (q > C);
						f[j] = f[bb[1]] ^ 1;
					}
				}
				
				et.insert(j);
			}
			
			et.erase(bb[1]);
			
			if ( f[bb[1]] == -1 ){
				f[bb[1]] = 0;
			}
			
			for ( auto &x: R ) f[x] = f[bb[1]];
		}
	};
	
	dfs2(dfs2, 0, -1);
	
	vector <int> T;
	
	for ( auto &x: et ) T.pb(x);
	
	for ( int root = 0; root < n; root++ ){
		vector <int> fa(n);
		
		auto dfs = [&](auto dfs, int u, int p) -> void{
			for ( auto &[v, j]: adj[u] ){
				if ( v != p && is[j] ){
					fa[v] = j;
					
					dfs(dfs, v, u);
				}
			}
		};
		
		dfs(dfs, root, -1);
		
		
		vector <ar<int,2>> p;
		
		for ( auto &[v, j]: adj[root] ){
			if ( f[j] == -1 ) p.pb({fa[v], j});
		}
		
		vector <int> skip(m);
		
		int it = 0, k = p.size();
		
		while ( it < k ){
			int l = it, r = k;
			
			auto ok = [&](int m){
				int tmp = C;
				
				vector <int> a;
				
				for ( int j = it; j <= m; j++ ){
					skip[p[j][0]] = 1;
					a.pb(p[j][1]);
					
					tmp -= f[p[j][0]];
				}
				
				for ( auto &x: T ){
					if ( !skip[x] ) a.pb(x);
				}
				
				int q = qry(a);
				
				for ( int j = it; j <= m; j++ ){
					skip[p[j][0]] = 0;
				}
				
				return q != tmp;
			};
			
			while ( l < r ){
				int m = (l + r) / 2;
				
				if ( ok(m) ){
					r = m;
				} else l = m + 1;
			}
			
			for ( int j = it; j < l; j++ ){
				f[p[j][1]] = 0;
			}
			
			if ( l < k ) f[p[l][1]] = 1;
			
			it = l + 1;
		}
	}

	for ( auto &x: f ){
		assert(x != -1);
	}
	
	vector <int> ans;
	
	for ( int i = 0; i < m; i++ ){
		if ( f[i] ) ans.pb(i);
	}
	
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 1 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 1 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 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 2 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 1 ms 576 KB correct
17 Correct 2 ms 632 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 2 ms 604 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 484 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 1 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 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 2 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 1 ms 576 KB correct
17 Correct 2 ms 632 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 2 ms 604 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 484 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 46 ms 5084 KB correct
35 Correct 48 ms 5156 KB correct
36 Correct 38 ms 4444 KB correct
37 Correct 13 ms 1628 KB correct
38 Correct 51 ms 5256 KB correct
39 Correct 43 ms 4952 KB correct
40 Correct 39 ms 4688 KB correct
41 Correct 44 ms 4948 KB correct
42 Correct 47 ms 4948 KB correct
43 Correct 30 ms 3664 KB correct
44 Correct 26 ms 3416 KB correct
45 Correct 28 ms 3420 KB correct
46 Correct 25 ms 3164 KB correct
47 Correct 17 ms 2648 KB correct
48 Correct 3 ms 604 KB correct
49 Correct 11 ms 1372 KB correct
50 Correct 18 ms 2384 KB correct
51 Correct 30 ms 3412 KB correct
52 Correct 26 ms 3156 KB correct
53 Correct 25 ms 3272 KB correct
54 Correct 29 ms 3564 KB correct
55 Correct 28 ms 3204 KB correct
56 Correct 28 ms 3420 KB correct
57 Correct 28 ms 3248 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 179 ms 14492 KB correct
4 Correct 333 ms 22344 KB correct
5 Correct 321 ms 22116 KB correct
6 Correct 342 ms 22028 KB correct
7 Correct 327 ms 22668 KB correct
8 Correct 341 ms 22632 KB correct
9 Correct 331 ms 22124 KB correct
10 Correct 333 ms 22372 KB correct
11 Correct 326 ms 22120 KB correct
12 Correct 336 ms 22240 KB correct
13 Correct 1 ms 344 KB correct
14 Correct 323 ms 22276 KB correct
15 Correct 332 ms 22112 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 1 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 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 2 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 1 ms 576 KB correct
17 Correct 2 ms 632 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 2 ms 604 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 484 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 46 ms 5084 KB correct
35 Correct 48 ms 5156 KB correct
36 Correct 38 ms 4444 KB correct
37 Correct 13 ms 1628 KB correct
38 Correct 51 ms 5256 KB correct
39 Correct 43 ms 4952 KB correct
40 Correct 39 ms 4688 KB correct
41 Correct 44 ms 4948 KB correct
42 Correct 47 ms 4948 KB correct
43 Correct 30 ms 3664 KB correct
44 Correct 26 ms 3416 KB correct
45 Correct 28 ms 3420 KB correct
46 Correct 25 ms 3164 KB correct
47 Correct 17 ms 2648 KB correct
48 Correct 3 ms 604 KB correct
49 Correct 11 ms 1372 KB correct
50 Correct 18 ms 2384 KB correct
51 Correct 30 ms 3412 KB correct
52 Correct 26 ms 3156 KB correct
53 Correct 25 ms 3272 KB correct
54 Correct 29 ms 3564 KB correct
55 Correct 28 ms 3204 KB correct
56 Correct 28 ms 3420 KB correct
57 Correct 28 ms 3248 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 179 ms 14492 KB correct
61 Correct 333 ms 22344 KB correct
62 Correct 321 ms 22116 KB correct
63 Correct 342 ms 22028 KB correct
64 Correct 327 ms 22668 KB correct
65 Correct 341 ms 22632 KB correct
66 Correct 331 ms 22124 KB correct
67 Correct 333 ms 22372 KB correct
68 Correct 326 ms 22120 KB correct
69 Correct 336 ms 22240 KB correct
70 Correct 1 ms 344 KB correct
71 Correct 323 ms 22276 KB correct
72 Correct 332 ms 22112 KB correct
73 Correct 0 ms 344 KB correct
74 Correct 349 ms 22172 KB correct
75 Correct 318 ms 21880 KB correct
76 Correct 128 ms 11972 KB correct
77 Correct 333 ms 22380 KB correct
78 Correct 329 ms 22124 KB correct
79 Correct 329 ms 22124 KB correct
80 Correct 327 ms 21884 KB correct
81 Correct 289 ms 21444 KB correct
82 Correct 317 ms 22136 KB correct
83 Correct 206 ms 17612 KB correct
84 Correct 208 ms 15880 KB correct
85 Correct 200 ms 15640 KB correct
86 Correct 145 ms 13168 KB correct
87 Correct 122 ms 12372 KB correct
88 Correct 111 ms 10828 KB correct
89 Correct 110 ms 10976 KB correct
90 Correct 100 ms 10320 KB correct
91 Correct 41 ms 4152 KB correct
92 Correct 15 ms 1628 KB correct
93 Correct 185 ms 15184 KB correct
94 Correct 143 ms 13192 KB correct
95 Correct 142 ms 13484 KB correct
96 Correct 107 ms 10576 KB correct
97 Correct 112 ms 11092 KB correct
98 Correct 121 ms 11856 KB correct
99 Correct 113 ms 10836 KB correct
100 Correct 70 ms 6992 KB correct
101 Correct 20 ms 2136 KB correct
102 Correct 176 ms 13852 KB correct
103 Correct 186 ms 13860 KB correct
104 Correct 180 ms 13688 KB correct
105 Correct 183 ms 13664 KB correct