Submission #134704

# Submission time Handle Problem Language Result Execution time Memory
134704 2019-07-23T07:36:16 Z dvdg6566 Simurgh (IOI17_simurgh) C++14
70 / 100
589 ms 29692 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define pb emplace_back
#define mp make_pair
#define f first
#define s second
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define MAXN 510

int N,a,b;
vpi V[MAXN];
vpi edgeList;
vi res,cur;
vpi tmp;
int p[MAXN];
int lowlink[MAXN], depth[MAXN];
int par(int x){return (x==p[x])?x:p[x] = par(p[x]);}
vi bridges;
unordered_map<int,int> M,safe;
	
int gpar,gnode;
vi voided;
vi currentquery;
vi done;

void addsafe(int a, int b){
	safe[a*N+b] = safe[b*N+a] = 1;
}

int find_ind(int a, int b){
	return M[a*N+b];
}
int calls;
int query(int s, int e){
	++calls;
	assert(calls<=12000);
	if (e<s)return 0;
	currentquery.clear();
	int offset = 0;
	for (auto i : voided)if(i!=gpar){
		currentquery.pb(find_ind(gpar, i));
		if (safe[gpar*N+i])++offset;
	}
	for (int i=0;i<SZ(cur);++i){
		// cout<<i<<' '<<cur[i]<<'\n';
		if (i>=s&&i<=e){
			currentquery.pb(find_ind(gnode,cur[i]));
		}else{
			currentquery.pb(find_ind(gpar,cur[i]));
			if (safe[gpar*N+cur[i]])++offset;
		}
	}
	if (gnode != 2)return count_common_roads(currentquery)-offset;
	// for (auto i : currentquery){
	// 	cout<<edgeList[i].f<<' '<<edgeList[i].s<<'\n';
	// }
	return count_common_roads(currentquery)-offset;
}

void dnc(int s, int e, int t){
	if(t==0)return;
	// if (gnode == 2)cout<<"DNC "<<s<<' '<<e<<' '<<t<<'\n';
	if (s == e){
		if (t == 1){
			done.pb(cur[s]);
		}
		return;
	}
	int m = (s+e)/2;
	int l = query(s,m-1);
	int r = query(m+1,e);
	// if (gnode == 2)cout<<m<<' '<<l<<' '<<r<<'\n';
	if (l + r < t){
		done.pb(cur[m]);
	}
	if (s < m)dnc(s,m-1,l);
	if (m < e)dnc(m+1,e,r);
}


void dfs(int x,int p){
	// cout<<"DFS "<<x<<' '<<p<<'\n';
	lowlink[x] = depth[x];
	for (auto v : V[x])if(v.f!=p){
		if (depth[v.f] != -1){
			lowlink[x] = min(lowlink[x], lowlink[v.f]);
			continue;
		}
		depth[v.f] = depth[x]+1;
		dfs(v.f,x);
		lowlink[x] = min(lowlink[x], lowlink[v.f]);
	}
}
 
bool isBridge(int x, int y){
	if (lowlink[x]<=depth[y]&&depth[x]>depth[y])return 0;
	if (lowlink[y]<=depth[x]&&depth[y]>depth[x])return 0;
	// cout<<x<<' '<<y<<'\n';
	return 1;
}

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
	N=n;
	for (int i=0;i<SZ(u);++i){
		a=u[i];b=v[i];
		V[a].pb(b,i);V[b].pb(a,i);
		edgeList.pb(a,b);
		M[a*N+b]=i;
		M[b*N+a]=i;
	}
	if (N <= 240){
		a=1;
		memset(depth,-1,sizeof(depth));
		depth[0]=0;
		dfs(0,0);
		for (int i=0;i<SZ(u);++i){
			int a=edgeList[i].f;
			int b=edgeList[i].s;
			if (isBridge(a,b))bridges.pb(i);
		}
 
		// for (auto i : bridges)cout<<i<<'\n';
		if (SZ(bridges) == N-1)return bridges;
 
		for (int i=0;i<N;++i){
			// We want to find all edges with index to a bigger node
			for (int j=0;j<N;++j)p[j]=j;
			cur.clear();
			tmp.clear();
			for (auto it : bridges){
				cur.pb(it);
				p[par(edgeList[it].f)] = par(edgeList[it].s);
			}
			for (int j=0;j<SZ(edgeList);++j){
				pi it = edgeList[j];
				if (it.f == i || it.s == i)continue;
				if (par(it.f) == par(it.s))continue;
				p[par(it.f)] = par(it.s);
				cur.pb(j);
			}
 
			for (auto x : V[i]){
				if (isBridge(i,x.f))continue;
				if (i>x.f)continue;
				cur.pb(x.s);
				// for (auto i : cur)cout<<i<<' ' ;cout<<'\n';
				int t = count_common_roads(cur);
				tmp.pb(x.s,t);
				cur.pop_back();
			}
 
			// cout<<"Current "<<i<<'\n';
			// for (auto x : tmp)cout<<x.f<<' '<<x.s<<'\n';
			int hh = 0;
			// cout<<"V:";for (auto v:res)cout<<v<<' ';cout<<'\n';
			bool gay=1;
 
			for (auto t : res){
				pi it = edgeList[t];
				if (it.f == i || it.s == i){
					cur.pb(t);
					hh = count_common_roads(cur);
					gay=0;
					break;
				}
			}
 
			if (gay){
				int ll = 0;
				for (auto x:tmp)ll = max(ll,x.s);
				for (auto x :tmp)if(x.s==ll)res.pb(x.f);
				continue;
			}
 
			for (auto x : tmp){
				if (x.s == hh)res.pb(x.f);
			}
			
			// for (auto x :tmp)cout<<x.f<<' '<<x.s<<'\n';
			// for (auto x:res)cout<<x<<' ';cout<<'\n';
		}
		for (auto i : bridges)res.pb(i);
		sort(ALL(res));
		res.resize(unique(ALL(res)) - res.begin());
		// assert(SZ(res) == N-1);
		return res;
	}
	// Must be complete graph
	for (int j=0;j<N;++j)p[j]=j;
	for (int i=0;i<SZ(edgeList);++i){
		pi v=edgeList[i];
		if (v.f==0||v.s==0)continue;
		if (par(v.f)==par(v.s))continue;
		p[par(v.f)]=par(v.s);
		cur.pb(i);
	}
	for (auto i :V[0]){
		cur.pb(i.s);
		tmp.pb(i.s,count_common_roads(cur));
		cur.pop_back();
	}
	// for (auto v : tmp)cout<<v.f<<' '<<v.s<<'\n';
	int ll = 0;
	for (auto x:tmp)ll = max(ll,x.s);
	for (auto x :tmp)if(x.s==ll)res.pb(x.f);

	set<int> S;
	queue<pi> q;
	for (int i=1;i<N;++i)S.insert(i);
	voided.pb(0);

	for (auto i : res){
		int x = edgeList[i].f+edgeList[i].s;
		q.push(mp(x,0));
		addsafe(x,0);
		S.erase(x);
		voided.pb(x);
	}

	while(SZ(q)){
		int t = q.front().f;
		int p = q.front().s;
		gpar = p;
		gnode = t;
		// cout<<"Resolve "<<gnode<<' '<<gpar<<'\n';

		q.pop();
		cur.clear();

		for (auto i : S)cur.pb(i);
		// for (auto i :cur)cout<<i<<' ';cout<<'\n';
		// cout<<query(0,SZ(cur))<<'\n';
		dnc(0,SZ(cur),query(0,SZ(cur)));
		// if (gnode == 2)for (auto i : cur)cout<<i <<' ';cout<<'\n';
		for (auto i : done){
			// cout<<"Done "<<i<<'\n';
			res.pb(find_ind(gnode,i));
			// cout<<"Add Edge "<<i<<' '<<gnode<<'\n';
			addsafe(t,i);
			S.erase(i);
			voided.pb(i);
			q.push(mp(i,gnode));
		}
		done.clear();
		// cout<<"Leftover: ";for (auto v : S)cout<<v<<' ';cout<<'\n';
	}
	sort(ALL(res));
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 7 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 380 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 7 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 380 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 504 KB correct
16 Correct 5 ms 504 KB correct
17 Correct 4 ms 504 KB correct
18 Correct 3 ms 348 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 508 KB correct
21 Correct 4 ms 504 KB correct
22 Correct 4 ms 372 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 7 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 380 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 504 KB correct
16 Correct 5 ms 504 KB correct
17 Correct 4 ms 504 KB correct
18 Correct 3 ms 348 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 508 KB correct
21 Correct 4 ms 504 KB correct
22 Correct 4 ms 372 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 254 ms 4644 KB correct
35 Correct 249 ms 4640 KB correct
36 Correct 171 ms 3100 KB correct
37 Correct 17 ms 500 KB correct
38 Correct 247 ms 4644 KB correct
39 Correct 226 ms 3584 KB correct
40 Correct 169 ms 2984 KB correct
41 Correct 256 ms 4640 KB correct
42 Correct 262 ms 4640 KB correct
43 Correct 131 ms 2724 KB correct
44 Correct 118 ms 1912 KB correct
45 Correct 129 ms 2552 KB correct
46 Correct 100 ms 1844 KB correct
47 Correct 44 ms 1032 KB correct
48 Correct 8 ms 504 KB correct
49 Correct 15 ms 552 KB correct
50 Correct 45 ms 1016 KB correct
51 Correct 130 ms 2424 KB correct
52 Correct 110 ms 2040 KB correct
53 Correct 101 ms 1920 KB correct
54 Correct 134 ms 2648 KB correct
55 Correct 118 ms 2424 KB correct
56 Correct 117 ms 2424 KB correct
57 Correct 116 ms 2552 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 220 ms 13656 KB correct
4 Correct 388 ms 22620 KB correct
5 Correct 589 ms 28672 KB correct
6 Correct 163 ms 17300 KB correct
7 Correct 160 ms 17348 KB correct
8 Correct 159 ms 17280 KB correct
9 Correct 345 ms 19592 KB correct
10 Correct 468 ms 28792 KB correct
11 Correct 439 ms 24864 KB correct
12 Correct 487 ms 28728 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 232 ms 17328 KB correct
15 Correct 472 ms 28744 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 7 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 380 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 504 KB correct
16 Correct 5 ms 504 KB correct
17 Correct 4 ms 504 KB correct
18 Correct 3 ms 348 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 508 KB correct
21 Correct 4 ms 504 KB correct
22 Correct 4 ms 372 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 254 ms 4644 KB correct
35 Correct 249 ms 4640 KB correct
36 Correct 171 ms 3100 KB correct
37 Correct 17 ms 500 KB correct
38 Correct 247 ms 4644 KB correct
39 Correct 226 ms 3584 KB correct
40 Correct 169 ms 2984 KB correct
41 Correct 256 ms 4640 KB correct
42 Correct 262 ms 4640 KB correct
43 Correct 131 ms 2724 KB correct
44 Correct 118 ms 1912 KB correct
45 Correct 129 ms 2552 KB correct
46 Correct 100 ms 1844 KB correct
47 Correct 44 ms 1032 KB correct
48 Correct 8 ms 504 KB correct
49 Correct 15 ms 552 KB correct
50 Correct 45 ms 1016 KB correct
51 Correct 130 ms 2424 KB correct
52 Correct 110 ms 2040 KB correct
53 Correct 101 ms 1920 KB correct
54 Correct 134 ms 2648 KB correct
55 Correct 118 ms 2424 KB correct
56 Correct 117 ms 2424 KB correct
57 Correct 116 ms 2552 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 220 ms 13656 KB correct
61 Correct 388 ms 22620 KB correct
62 Correct 589 ms 28672 KB correct
63 Correct 163 ms 17300 KB correct
64 Correct 160 ms 17348 KB correct
65 Correct 159 ms 17280 KB correct
66 Correct 345 ms 19592 KB correct
67 Correct 468 ms 28792 KB correct
68 Correct 439 ms 24864 KB correct
69 Correct 487 ms 28728 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 232 ms 17328 KB correct
72 Correct 472 ms 28744 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 509 ms 29692 KB correct
75 Incorrect 147 ms 18028 KB WA in grader: NO
76 Halted 0 ms 0 KB -