Submission #778380

# Submission time Handle Problem Language Result Execution time Memory
778380 2023-07-10T09:00:37 Z CSQ31 Simurgh (IOI17_simurgh) C++17
100 / 100
209 ms 11868 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN = 2e5;
int par[MAXN],szz[MAXN],good[MAXN],tree[MAXN];
vector<pii>adj[MAXN],ed;
int find(int x){
	if(x==par[x])return x;
	return par[x] = find(par[x]);
}
bool unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a==b)return 0;
	if(szz[a] > szz[b])swap(a,b);
	par[a] = b;
	szz[b]+=szz[a];
	return 1;
}
void init(int n){
	for(int i=0;i<n;i++){
		par[i] = i;
		szz[i] = 1;
	}
}
set<int>edges;
vector<int>edge;
int Q = 0;
int ask(){
	Q++;
	assert(Q<=9000);
	vector<int>r;
	if(edges.empty())for(int x:edge)r.pb(x);
	else for(int x:edges)r.pb(x);
	//assert(sz(r)==nn-1);
	return count_common_roads(r);
}
 
pii p[MAXN];
int dep[MAXN],vis[MAXN],glob;
void dfs(int v,int u){
	vis[v] = 1;
	for(pii z:adj[v]){
		int x = z.fi;
		int id = z.se;
		if(vis[x] || x==u)continue;
		tree[id] = 1;
		edges.insert(id);
		dep[x] = dep[v]+1;
		p[x] = {v,id};
		dfs(x,v);
	}
}
void dfs2(int v,int u){
	
	for(pii z:adj[v]){
		int x = z.fi;
		int id = z.se;
		if(tree[id]){
			if(x!=u)dfs2(x,v);
			continue;
		}
		if(dep[x] > dep[v])continue; //go downwards
		
		vector<int>path,res;
		int cur = v;
		while(cur != x){
			path.pb(p[cur].se);
			cur = p[cur].fi;
		}
		//for(int e:path)cout<<e<<" ";
		//cout<<'\n';
		int have = 0;
		for(int e:path)have += (good[e] == -1);
		if(!have)continue; //if all tree edge part of this cycle is known,skip
		edges.insert(id);
		if(have == sz(path)){ //everything is unknown
			//cout<<"case 1"<<'\n';
			for(int e:path){
				edges.erase(e);
				res.pb(ask());
				edges.insert(e);
			}
			path.pb(id);
			res.pb(glob);
			int s = sz(path);
			for(int i=0;i<s;i++){
				int j = (i+s-1) % s;
				if(res[i] == res[j])continue;
				good[path[i]] = 1;
				good[path[j]] = 0;
				if(res[i] > res[j])swap(good[path[i]],good[path[j]]);
			}
			for(int i=0;i<s;i++){
				int j = (i+s-1) % s;
				if(good[path[j]] >= 0 && res[i] == res[j])good[path[i]] = good[path[j]];
			}
			for(int i=s-1;i>=0;i--){
				int j = (i+1) % s;
				if(good[path[j]] >= 0 && res[i] == res[j])good[path[i]] = good[path[j]];
			}
			for(int x:path){
				if(good[x] == -1)good[x] = 0;
			}
		}else{
			//cout<<"case 2"<<'\n';
			if(good[id] == -1){
				for(int e:path){
					if(good[e] == -1)continue;
					edges.erase(e);
					int res2 = ask();
					edges.insert(e);
					if(res2 == glob)good[id] = good[e];
					else if(res2 > glob)good[id] = 1; 
					else good[id] = 0;
					break;
				}
			}
			//cout<<good[id]<<'\n';
			for(int e:path){
				if(good[e] != -1)continue;
				edges.erase(e);
				int res2 = ask();
				edges.insert(e);
				if(res2 == glob)good[e] = good[id];
				else if(res2 > glob)good[e] = 0;
				else good[e] = 1;
			}
		}
		edges.erase(id);
	}
}
vector<int>e,solved;
int make(){
	int cnt = 0;
	for(int x:solved){
		if(unite(ed[x].fi,ed[x].se)){
			edge.pb(x);
			cnt+=good[x];
		}
	}
	return cnt;
	
}
void solve(vector<int>e,int n,int num){
	if(sz(e)==1){
		good[e[0]] = 1;
		return;
		
	}
	int m = sz(e);
	vector<int>l,r;
	for(int i=0;i<m/2;i++)l.pb(e[i]);
	for(int i=m/2;i<m;i++)r.pb(e[i]);
	
	init(n);
	edge.clear();
	for(int x:l){
		edge.pb(x);
		unite(ed[x].fi,ed[x].se);
	}
	int tot = make();
	int res = ask();
	int x = res-tot;
	if(x)solve(l,n,x);
	if(num-x)solve(r,n,num-x);
	
	
}
mt19937 seed(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> find_roads(int N,vector<int> u,vector<int> v) {
	int n = N;
	int m = sz(u);
	for(int i=0;i<m;i++)ed.pb({v[i],u[i]});
	for(int i=0;i<m;i++){
		good[i] = -1;
		adj[u[i]].pb({v[i],i});
		adj[v[i]].pb({u[i],i});
	}
	dfs(0,-1);
	glob = ask();
	dfs2(0,-1);
	assert(Q<=1000);
 
	for(int x:edges){
		if(good[x]==-1)good[x] = 1; //this is a bridge	
	}
	for(int x:edges)solved.pb(x);
	edges.clear();
	for(int i=0;i<n;i++){
		e.clear();
		for(pii x:adj[i]){
			if(x.fi > i)e.pb(x.se);
		}
		if(e.empty())continue;
		init(n);
		edge.clear();
		for(int x:e){
			unite(ed[x].fi,ed[x].se);
			edge.pb(x);
		}
		int tot = make();
		int res = ask();
		if(res-tot)solve(e,n,res-tot);
	}
	vector<int>ans;
	for(int i=0;i<m;i++){
		if(good[i] == 1)ans.pb(i);
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 3 ms 4948 KB correct
3 Correct 2 ms 5012 KB correct
4 Correct 3 ms 5016 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 3 ms 4948 KB correct
7 Correct 3 ms 5012 KB correct
8 Correct 3 ms 5016 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 2 ms 4948 KB correct
12 Correct 3 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 3 ms 4948 KB correct
3 Correct 2 ms 5012 KB correct
4 Correct 3 ms 5016 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 3 ms 4948 KB correct
7 Correct 3 ms 5012 KB correct
8 Correct 3 ms 5016 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 2 ms 4948 KB correct
12 Correct 3 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5024 KB correct
15 Correct 4 ms 5020 KB correct
16 Correct 4 ms 5020 KB correct
17 Correct 3 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 4 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5020 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 3 ms 5092 KB correct
25 Correct 3 ms 5012 KB correct
26 Correct 3 ms 5012 KB correct
27 Correct 3 ms 5076 KB correct
28 Correct 3 ms 5012 KB correct
29 Correct 3 ms 5012 KB correct
30 Correct 3 ms 5076 KB correct
31 Correct 3 ms 5076 KB correct
32 Correct 3 ms 5076 KB correct
33 Correct 3 ms 5076 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 3 ms 4948 KB correct
3 Correct 2 ms 5012 KB correct
4 Correct 3 ms 5016 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 3 ms 4948 KB correct
7 Correct 3 ms 5012 KB correct
8 Correct 3 ms 5016 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 2 ms 4948 KB correct
12 Correct 3 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5024 KB correct
15 Correct 4 ms 5020 KB correct
16 Correct 4 ms 5020 KB correct
17 Correct 3 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 4 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5020 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 3 ms 5092 KB correct
25 Correct 3 ms 5012 KB correct
26 Correct 3 ms 5012 KB correct
27 Correct 3 ms 5076 KB correct
28 Correct 3 ms 5012 KB correct
29 Correct 3 ms 5012 KB correct
30 Correct 3 ms 5076 KB correct
31 Correct 3 ms 5076 KB correct
32 Correct 3 ms 5076 KB correct
33 Correct 3 ms 5076 KB correct
34 Correct 33 ms 6696 KB correct
35 Correct 33 ms 6572 KB correct
36 Correct 28 ms 6352 KB correct
37 Correct 8 ms 5180 KB correct
38 Correct 37 ms 6664 KB correct
39 Correct 29 ms 6456 KB correct
40 Correct 23 ms 6352 KB correct
41 Correct 32 ms 6608 KB correct
42 Correct 35 ms 6620 KB correct
43 Correct 18 ms 6004 KB correct
44 Correct 15 ms 5716 KB correct
45 Correct 17 ms 5864 KB correct
46 Correct 20 ms 5740 KB correct
47 Correct 11 ms 5332 KB correct
48 Correct 5 ms 5020 KB correct
49 Correct 7 ms 5148 KB correct
50 Correct 12 ms 5332 KB correct
51 Correct 20 ms 5792 KB correct
52 Correct 16 ms 5788 KB correct
53 Correct 15 ms 5716 KB correct
54 Correct 18 ms 6100 KB correct
55 Correct 20 ms 5912 KB correct
56 Correct 20 ms 5800 KB correct
57 Correct 28 ms 5908 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 3 ms 4948 KB correct
3 Correct 102 ms 9796 KB correct
4 Correct 171 ms 11868 KB correct
5 Correct 189 ms 11856 KB correct
6 Correct 175 ms 11856 KB correct
7 Correct 166 ms 11868 KB correct
8 Correct 158 ms 11848 KB correct
9 Correct 190 ms 11828 KB correct
10 Correct 209 ms 11856 KB correct
11 Correct 176 ms 11728 KB correct
12 Correct 188 ms 11848 KB correct
13 Correct 3 ms 4948 KB correct
14 Correct 173 ms 11828 KB correct
15 Correct 179 ms 11860 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 3 ms 4948 KB correct
3 Correct 2 ms 5012 KB correct
4 Correct 3 ms 5016 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 3 ms 4948 KB correct
7 Correct 3 ms 5012 KB correct
8 Correct 3 ms 5016 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 2 ms 4948 KB correct
12 Correct 3 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5024 KB correct
15 Correct 4 ms 5020 KB correct
16 Correct 4 ms 5020 KB correct
17 Correct 3 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 4 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5020 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 3 ms 5092 KB correct
25 Correct 3 ms 5012 KB correct
26 Correct 3 ms 5012 KB correct
27 Correct 3 ms 5076 KB correct
28 Correct 3 ms 5012 KB correct
29 Correct 3 ms 5012 KB correct
30 Correct 3 ms 5076 KB correct
31 Correct 3 ms 5076 KB correct
32 Correct 3 ms 5076 KB correct
33 Correct 3 ms 5076 KB correct
34 Correct 33 ms 6696 KB correct
35 Correct 33 ms 6572 KB correct
36 Correct 28 ms 6352 KB correct
37 Correct 8 ms 5180 KB correct
38 Correct 37 ms 6664 KB correct
39 Correct 29 ms 6456 KB correct
40 Correct 23 ms 6352 KB correct
41 Correct 32 ms 6608 KB correct
42 Correct 35 ms 6620 KB correct
43 Correct 18 ms 6004 KB correct
44 Correct 15 ms 5716 KB correct
45 Correct 17 ms 5864 KB correct
46 Correct 20 ms 5740 KB correct
47 Correct 11 ms 5332 KB correct
48 Correct 5 ms 5020 KB correct
49 Correct 7 ms 5148 KB correct
50 Correct 12 ms 5332 KB correct
51 Correct 20 ms 5792 KB correct
52 Correct 16 ms 5788 KB correct
53 Correct 15 ms 5716 KB correct
54 Correct 18 ms 6100 KB correct
55 Correct 20 ms 5912 KB correct
56 Correct 20 ms 5800 KB correct
57 Correct 28 ms 5908 KB correct
58 Correct 2 ms 4948 KB correct
59 Correct 3 ms 4948 KB correct
60 Correct 102 ms 9796 KB correct
61 Correct 171 ms 11868 KB correct
62 Correct 189 ms 11856 KB correct
63 Correct 175 ms 11856 KB correct
64 Correct 166 ms 11868 KB correct
65 Correct 158 ms 11848 KB correct
66 Correct 190 ms 11828 KB correct
67 Correct 209 ms 11856 KB correct
68 Correct 176 ms 11728 KB correct
69 Correct 188 ms 11848 KB correct
70 Correct 3 ms 4948 KB correct
71 Correct 173 ms 11828 KB correct
72 Correct 179 ms 11860 KB correct
73 Correct 3 ms 4948 KB correct
74 Correct 185 ms 11840 KB correct
75 Correct 179 ms 11664 KB correct
76 Correct 82 ms 7676 KB correct
77 Correct 188 ms 11824 KB correct
78 Correct 177 ms 11820 KB correct
79 Correct 198 ms 11844 KB correct
80 Correct 174 ms 11696 KB correct
81 Correct 135 ms 11060 KB correct
82 Correct 170 ms 11692 KB correct
83 Correct 112 ms 8832 KB correct
84 Correct 91 ms 9544 KB correct
85 Correct 92 ms 9340 KB correct
86 Correct 61 ms 7800 KB correct
87 Correct 49 ms 7248 KB correct
88 Correct 44 ms 6612 KB correct
89 Correct 41 ms 6560 KB correct
90 Correct 37 ms 6352 KB correct
91 Correct 21 ms 5288 KB correct
92 Correct 12 ms 5148 KB correct
93 Correct 91 ms 9344 KB correct
94 Correct 60 ms 7800 KB correct
95 Correct 62 ms 7756 KB correct
96 Correct 40 ms 6432 KB correct
97 Correct 43 ms 6568 KB correct
98 Correct 51 ms 7204 KB correct
99 Correct 46 ms 6608 KB correct
100 Correct 29 ms 5636 KB correct
101 Correct 13 ms 5228 KB correct
102 Correct 95 ms 8556 KB correct
103 Correct 115 ms 8532 KB correct
104 Correct 95 ms 8524 KB correct
105 Correct 94 ms 8472 KB correct