Submission #778114

# Submission time Handle Problem Language Result Execution time Memory
778114 2023-07-10T06:25:45 Z CSQ31 Simurgh (IOI17_simurgh) C++17
70 / 100
416 ms 12544 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 nn;
bool debug = 0;
int Q = 0;
int ask(){
	Q++;
	if(nn==500)assert(Q<=12000);
	vector<int>r;
	if(edges.empty())for(int x:edge)r.pb(x);
	else for(int x:edges)r.pb(x);
	if(debug)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;
void solve(int l,int r){
	if(l>r)return;
	init(nn);
	edge.clear();
	for(int i=l;i<=r;i++){
		if(unite(ed[e[i]].fi,ed[e[i]].se))edge.pb(e[i]);
	}
	int tot = 0;
	
	for(int i:solved){
		if(unite(ed[i].fi,ed[i].se)){
			edge.pb(i);
			tot+=good[i];
		}
		if(sz(edge)==nn-1)break;
		
	}
	int res = ask();
	edge.clear();
	if(res == tot){
		for(int i=l;i<=r;i++)good[e[i]] = 0;
		return;
	}else if(res == tot + r-l+1){
		for(int i=l;i<=r;i++)good[e[i]] = 1;
		return;
	}
	int mid = (l+r)/2;
	solve(l,mid);
	solve(mid+1,r);
	
	
}
mt19937 seed(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> find_roads(int N,vector<int> u,vector<int> v) {
	nn = N;
	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);
	debug = 0;
	for(int x:edges){
		if(good[x]==-1)good[x] = 1; //this is a bridge	
	}
	int found = 0;
	vector<int>left;
	for(int i=0;i<m;i++){
		//cout<<good[i]<<" ";
		if(good[i]>=0){
			found+=good[i];
			solved.pb(i);
		}else left.pb(i);
	}
	shuffle(left.begin(),left.end(),seed);
	shuffle(solved.begin(),solved.end(),seed);
	edges.clear();
	/*
	for(int i=0;i<m;i++){
		if(good[i]>=0)continue;
		if(found==n-1)break;
		init(n);
		edge.clear();
		edge.pb(i);
		unite(v[i],u[i]);
		int tot = 0;
		for(int x:solved){
			if(unite(v[x],u[x])){
				edge.pb(x);
				tot+=good[x];
			}
		}
		if(ask() == tot)good[i] = 0;
		else{
			good[i] = 1;
			found++;
		}
		solved.pb(i);
	}*/
	while(sz(left) && found != n-1){
		init(n);
		e.clear();
		for(int x:left){
			if(unite(v[x],u[x]))e.pb(x);
		}
		solve(0,sz(e)-1);
		vector<int>tmp;
		for(int x:left){
			if(good[x] == -1)tmp.pb(x);
			else found+=good[x];
		}
		swap(left,tmp);
	}
	
	
	
	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 3 ms 4948 KB correct
2 Correct 2 ms 4948 KB correct
3 Correct 3 ms 4948 KB correct
4 Correct 4 ms 4948 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 2 ms 4948 KB correct
7 Correct 3 ms 4964 KB correct
8 Correct 3 ms 4948 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 3 ms 4948 KB correct
12 Correct 2 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB correct
2 Correct 2 ms 4948 KB correct
3 Correct 3 ms 4948 KB correct
4 Correct 4 ms 4948 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 2 ms 4948 KB correct
7 Correct 3 ms 4964 KB correct
8 Correct 3 ms 4948 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 3 ms 4948 KB correct
12 Correct 2 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5076 KB correct
15 Correct 4 ms 5076 KB correct
16 Correct 5 ms 5112 KB correct
17 Correct 4 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 5 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5076 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 4 ms 5076 KB correct
25 Correct 2 ms 4948 KB correct
26 Correct 3 ms 5076 KB correct
27 Correct 3 ms 5004 KB correct
28 Correct 3 ms 5076 KB correct
29 Correct 2 ms 4948 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 3 ms 4948 KB correct
2 Correct 2 ms 4948 KB correct
3 Correct 3 ms 4948 KB correct
4 Correct 4 ms 4948 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 2 ms 4948 KB correct
7 Correct 3 ms 4964 KB correct
8 Correct 3 ms 4948 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 3 ms 4948 KB correct
12 Correct 2 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5076 KB correct
15 Correct 4 ms 5076 KB correct
16 Correct 5 ms 5112 KB correct
17 Correct 4 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 5 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5076 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 4 ms 5076 KB correct
25 Correct 2 ms 4948 KB correct
26 Correct 3 ms 5076 KB correct
27 Correct 3 ms 5004 KB correct
28 Correct 3 ms 5076 KB correct
29 Correct 2 ms 4948 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 67 ms 6852 KB correct
35 Correct 74 ms 6716 KB correct
36 Correct 44 ms 6480 KB correct
37 Correct 10 ms 5188 KB correct
38 Correct 57 ms 6768 KB correct
39 Correct 51 ms 6624 KB correct
40 Correct 47 ms 6472 KB correct
41 Correct 59 ms 6756 KB correct
42 Correct 59 ms 6728 KB correct
43 Correct 19 ms 6208 KB correct
44 Correct 17 ms 5844 KB correct
45 Correct 17 ms 5972 KB correct
46 Correct 14 ms 5784 KB correct
47 Correct 9 ms 5332 KB correct
48 Correct 3 ms 5076 KB correct
49 Correct 6 ms 5076 KB correct
50 Correct 9 ms 5460 KB correct
51 Correct 18 ms 5972 KB correct
52 Correct 15 ms 5832 KB correct
53 Correct 14 ms 5716 KB correct
54 Correct 19 ms 6120 KB correct
55 Correct 20 ms 5924 KB correct
56 Correct 23 ms 5996 KB correct
57 Correct 21 ms 5976 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB correct
2 Correct 2 ms 4948 KB correct
3 Correct 255 ms 10572 KB correct
4 Correct 387 ms 12436 KB correct
5 Correct 370 ms 12432 KB correct
6 Correct 391 ms 12420 KB correct
7 Correct 374 ms 12348 KB correct
8 Correct 416 ms 12336 KB correct
9 Correct 380 ms 12432 KB correct
10 Correct 385 ms 12444 KB correct
11 Correct 372 ms 12408 KB correct
12 Correct 404 ms 12460 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 390 ms 12388 KB correct
15 Correct 390 ms 12428 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB correct
2 Correct 2 ms 4948 KB correct
3 Correct 3 ms 4948 KB correct
4 Correct 4 ms 4948 KB correct
5 Correct 2 ms 4948 KB correct
6 Correct 2 ms 4948 KB correct
7 Correct 3 ms 4964 KB correct
8 Correct 3 ms 4948 KB correct
9 Correct 2 ms 4948 KB correct
10 Correct 2 ms 4948 KB correct
11 Correct 3 ms 4948 KB correct
12 Correct 2 ms 4948 KB correct
13 Correct 2 ms 4948 KB correct
14 Correct 4 ms 5076 KB correct
15 Correct 4 ms 5076 KB correct
16 Correct 5 ms 5112 KB correct
17 Correct 4 ms 5076 KB correct
18 Correct 3 ms 5076 KB correct
19 Correct 5 ms 5076 KB correct
20 Correct 3 ms 5076 KB correct
21 Correct 3 ms 5076 KB correct
22 Correct 3 ms 5076 KB correct
23 Correct 3 ms 5076 KB correct
24 Correct 4 ms 5076 KB correct
25 Correct 2 ms 4948 KB correct
26 Correct 3 ms 5076 KB correct
27 Correct 3 ms 5004 KB correct
28 Correct 3 ms 5076 KB correct
29 Correct 2 ms 4948 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 67 ms 6852 KB correct
35 Correct 74 ms 6716 KB correct
36 Correct 44 ms 6480 KB correct
37 Correct 10 ms 5188 KB correct
38 Correct 57 ms 6768 KB correct
39 Correct 51 ms 6624 KB correct
40 Correct 47 ms 6472 KB correct
41 Correct 59 ms 6756 KB correct
42 Correct 59 ms 6728 KB correct
43 Correct 19 ms 6208 KB correct
44 Correct 17 ms 5844 KB correct
45 Correct 17 ms 5972 KB correct
46 Correct 14 ms 5784 KB correct
47 Correct 9 ms 5332 KB correct
48 Correct 3 ms 5076 KB correct
49 Correct 6 ms 5076 KB correct
50 Correct 9 ms 5460 KB correct
51 Correct 18 ms 5972 KB correct
52 Correct 15 ms 5832 KB correct
53 Correct 14 ms 5716 KB correct
54 Correct 19 ms 6120 KB correct
55 Correct 20 ms 5924 KB correct
56 Correct 23 ms 5996 KB correct
57 Correct 21 ms 5976 KB correct
58 Correct 2 ms 4948 KB correct
59 Correct 2 ms 4948 KB correct
60 Correct 255 ms 10572 KB correct
61 Correct 387 ms 12436 KB correct
62 Correct 370 ms 12432 KB correct
63 Correct 391 ms 12420 KB correct
64 Correct 374 ms 12348 KB correct
65 Correct 416 ms 12336 KB correct
66 Correct 380 ms 12432 KB correct
67 Correct 385 ms 12444 KB correct
68 Correct 372 ms 12408 KB correct
69 Correct 404 ms 12460 KB correct
70 Correct 2 ms 4948 KB correct
71 Correct 390 ms 12388 KB correct
72 Correct 390 ms 12428 KB correct
73 Correct 2 ms 4960 KB correct
74 Incorrect 410 ms 12544 KB WA in grader: NO
75 Halted 0 ms 0 KB -