Submission #1052969

# Submission time Handle Problem Language Result Execution time Memory
1052969 2024-08-11T06:50:57 Z Alihan_8 Simurgh (IOI17_simurgh) C++17
100 / 100
616 ms 22352 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);
	
	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});
		}
		
		int it = 0, k = p.size();
		
		while ( it < k ){
			int l = it, r = k;
			
			auto ok = [&](int m){
				int tmp = C;
				
				for ( int j = it; j <= m; j++ ){
					et.insert(p[j][1]);
					et.erase(p[j][0]);
					
					tmp -= f[p[j][0]];
				}
				
				int q = ask(et);
				
				for ( int j = it; j <= m; j++ ){
					et.erase(p[j][1]);
					et.insert(p[j][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 1 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 0 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 1 ms 344 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 1 ms 344 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 3 ms 600 KB correct
16 Correct 3 ms 604 KB correct
17 Correct 2 ms 520 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 3 ms 604 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 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 2 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 1 ms 344 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 3 ms 600 KB correct
16 Correct 3 ms 604 KB correct
17 Correct 2 ms 520 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 3 ms 604 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 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 2 ms 348 KB correct
34 Correct 91 ms 4948 KB correct
35 Correct 88 ms 5012 KB correct
36 Correct 67 ms 4436 KB correct
37 Correct 16 ms 1624 KB correct
38 Correct 93 ms 4944 KB correct
39 Correct 83 ms 4696 KB correct
40 Correct 68 ms 4436 KB correct
41 Correct 91 ms 4948 KB correct
42 Correct 90 ms 4956 KB correct
43 Correct 51 ms 3408 KB correct
44 Correct 48 ms 3016 KB correct
45 Correct 49 ms 3412 KB correct
46 Correct 40 ms 3160 KB correct
47 Correct 25 ms 2396 KB correct
48 Correct 4 ms 604 KB correct
49 Correct 13 ms 1348 KB correct
50 Correct 25 ms 2384 KB correct
51 Correct 48 ms 3412 KB correct
52 Correct 49 ms 3156 KB correct
53 Correct 40 ms 3148 KB correct
54 Correct 53 ms 3624 KB correct
55 Correct 46 ms 3104 KB correct
56 Correct 47 ms 3152 KB correct
57 Correct 47 ms 3152 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 327 ms 14124 KB correct
4 Correct 605 ms 21840 KB correct
5 Correct 580 ms 21588 KB correct
6 Correct 580 ms 21332 KB correct
7 Correct 616 ms 22352 KB correct
8 Correct 595 ms 22100 KB correct
9 Correct 613 ms 21584 KB correct
10 Correct 588 ms 21844 KB correct
11 Correct 586 ms 21584 KB correct
12 Correct 582 ms 21844 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 580 ms 21792 KB correct
15 Correct 583 ms 21656 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 1 ms 344 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 3 ms 600 KB correct
16 Correct 3 ms 604 KB correct
17 Correct 2 ms 520 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 3 ms 604 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 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 2 ms 348 KB correct
34 Correct 91 ms 4948 KB correct
35 Correct 88 ms 5012 KB correct
36 Correct 67 ms 4436 KB correct
37 Correct 16 ms 1624 KB correct
38 Correct 93 ms 4944 KB correct
39 Correct 83 ms 4696 KB correct
40 Correct 68 ms 4436 KB correct
41 Correct 91 ms 4948 KB correct
42 Correct 90 ms 4956 KB correct
43 Correct 51 ms 3408 KB correct
44 Correct 48 ms 3016 KB correct
45 Correct 49 ms 3412 KB correct
46 Correct 40 ms 3160 KB correct
47 Correct 25 ms 2396 KB correct
48 Correct 4 ms 604 KB correct
49 Correct 13 ms 1348 KB correct
50 Correct 25 ms 2384 KB correct
51 Correct 48 ms 3412 KB correct
52 Correct 49 ms 3156 KB correct
53 Correct 40 ms 3148 KB correct
54 Correct 53 ms 3624 KB correct
55 Correct 46 ms 3104 KB correct
56 Correct 47 ms 3152 KB correct
57 Correct 47 ms 3152 KB correct
58 Correct 1 ms 344 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 327 ms 14124 KB correct
61 Correct 605 ms 21840 KB correct
62 Correct 580 ms 21588 KB correct
63 Correct 580 ms 21332 KB correct
64 Correct 616 ms 22352 KB correct
65 Correct 595 ms 22100 KB correct
66 Correct 613 ms 21584 KB correct
67 Correct 588 ms 21844 KB correct
68 Correct 586 ms 21584 KB correct
69 Correct 582 ms 21844 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 580 ms 21792 KB correct
72 Correct 583 ms 21656 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 603 ms 21580 KB correct
75 Correct 568 ms 21588 KB correct
76 Correct 216 ms 11860 KB correct
77 Correct 599 ms 21736 KB correct
78 Correct 573 ms 21620 KB correct
79 Correct 590 ms 21844 KB correct
80 Correct 578 ms 21584 KB correct
81 Correct 508 ms 21072 KB correct
82 Correct 603 ms 21584 KB correct
83 Correct 348 ms 17492 KB correct
84 Correct 351 ms 15700 KB correct
85 Correct 336 ms 15436 KB correct
86 Correct 247 ms 13032 KB correct
87 Correct 201 ms 12116 KB correct
88 Correct 179 ms 10836 KB correct
89 Correct 169 ms 10816 KB correct
90 Correct 153 ms 10072 KB correct
91 Correct 51 ms 4088 KB correct
92 Correct 19 ms 1624 KB correct
93 Correct 314 ms 14932 KB correct
94 Correct 244 ms 13140 KB correct
95 Correct 243 ms 13364 KB correct
96 Correct 162 ms 10668 KB correct
97 Correct 174 ms 11092 KB correct
98 Correct 199 ms 11740 KB correct
99 Correct 178 ms 10872 KB correct
100 Correct 86 ms 6956 KB correct
101 Correct 23 ms 2140 KB correct
102 Correct 314 ms 13424 KB correct
103 Correct 304 ms 13596 KB correct
104 Correct 296 ms 13396 KB correct
105 Correct 294 ms 13392 KB correct