Submission #831739

# Submission time Handle Problem Language Result Execution time Memory
831739 2023-08-20T12:59:47 Z Antekb Simurgh (IOI17_simurgh) C++17
100 / 100
132 ms 6692 KB
#include<bits/stdc++.h>
#include "simurgh.h"
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pp pop_back
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pii>;
using ll = long long;

void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);

const int N=505;
int r[N], vis[N], par[N], paredg[N], dep[N];
int dobry[N];
int deg[N];
vii E[N];
vii edg;
int n;
int find(int v){
	if(r[v]==v)return v;
	return r[v]=find(r[v]);
}
void Union(int u, int v){
	r[find(u)]=find(v);
}
void dfs(int v){
	vis[v]=1;
	for(auto ee:E[v]){
		int u=ee.st, e=ee.nd;
		//deb(v, u);
		if(!vis[u]){
			paredg[u]=e;
			par[u]=v;
			dep[u]=dep[v]+1;
			dfs(u);
		}
	}
}
vii drz;
int queries=0;
int licz(vi V){
	for(int i=0; i<n; i++){
		r[i]=i;
	}
	vi V2;
	int sum=0;
	for(int i:V){
		V2.pb(i);
		Union(edg[i].st, edg[i].nd);
	}
	for(pii i:drz){
		if(find(edg[i.st].st)!=find(edg[i.st].nd)){
			sum+=i.nd;
			V2.pb(i.st);
			Union(edg[i.st].st, edg[i.st].nd);
		}
	}
	assert(queries++<25000);
	int ile=count_common_roads(V2);
	return ile-sum;
}
std::vector<int> find_roads(int _n, std::vector<int> _u, std::vector<int> _v) {
	n=_n;
	for(int i=0; i<_u.size(); i++)
	{
		E[_u[i]].eb(_v[i], i);
		E[_v[i]].eb(_u[i], i);
		edg.eb(_u[i], _v[i]);
	}
	par[0]=paredg[0]=-1;
	dfs(0);
	vi V;
	for(int i=1; i<n; i++){
		r[i]=i;
		deb(i, par[i], paredg[i], dep[i]);
		V.pb(paredg[i]);
	}
	int ile=count_common_roads(V);
	for(int v=0; v<n; v++){
		for(pii ee:E[v]){
			int u=ee.st, e=ee.nd;
			if(dep[u]>dep[v] && v!=par[u]){
				vi todo;
				//deb(v, u);
				while(true){
					int uu=find(u);
					if(uu==find(v)){
						break;
					}
					else{
						int e2=paredg[uu];
						for(int &i:V){
							if(i==e2)i=e;
						}
	assert(queries++<25000);
						int ile2=count_common_roads(V);
						if(ile2==ile){
							//deb(e2);
							todo.pb(e2);
						}
						else if(ile2>ile){
							//kraw=e;
						}
						else{
							dobry[uu]=1;
							//kraw=e2;
						}
						r[uu]=par[uu];
						for(int &i:V){
							if(i==e)i=e2;
						}
					}
				}
				if(todo.empty())continue;
				for(int uu=u; uu!=v; uu=par[uu]){
					bool czy=0;
					for(int i:todo){
						if(i==paredg[uu]){
							czy=1;
							break;
						}
					}
					if(czy)continue;
					//deb("a");
					int e2=paredg[uu];
					for(int &i:V){
						if(i==e2)i=e;
					}
	assert(queries++<25000);
					int ile2=count_common_roads(V);
					if(ile2+dobry[uu]>ile){
						//deb("b");
						for(int i:todo){
							if(i==paredg[edg[i].st])dobry[edg[i].st]=1;
							else dobry[edg[i].nd]=1;
						}
					}
					for(int &i:V){
						if(i==e)i=e2;
					}
					break;
				}
			}
		}
	}
	for(int i=1; i<n; i++){
		if(find(i)==i){
			dobry[i]=1;
		}
		deb(i, find(i), paredg[i], dobry[i]);
		drz.eb(paredg[i], dobry[i]);
	}
	for(int i=0; i<n; i++){
		vis[i]=0;
		vi V2;
		for(auto e:E[i]){
			V2.pb(e.nd);
		}
		deg[i]=licz(V2);
		deb(i, deg[i]);
	}
	vi ans;
	for(int ii=1; ii<n; ii++){
		int v=-1;
		for(int i=0; i<n; i++){
			if(!vis[i] && deg[i]==1){
				v=i;
			}
		}
		vi V2;
		assert(v!=-1);
		for(auto e:E[v]){
			if(!vis[e.st])V2.pb(e.nd);
		}
		int l=0, r=V2.size()-1;
		while(l<r){
			int m=(l+r)>>1;
			vi V3=V2;
			V3.resize(m+1);
			if(licz(V3)==0)l=m+1;
			else r=m;
		}
		ans.pb(V2[l]);
		int u=edg[ans.back()].st^edg[ans.back()].nd^v;
		deg[u]--;
		deg[v]--;
		vis[v]=1;
	}
	return ans;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:72:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for(int i=0; i<_u.size(); i++)
      |               ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 3 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 340 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 3 ms 396 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 3 ms 280 KB correct
23 Correct 2 ms 340 KB correct
24 Correct 3 ms 340 KB correct
25 Correct 2 ms 340 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 2 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 3 ms 340 KB correct
33 Correct 2 ms 340 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 3 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 340 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 3 ms 396 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 3 ms 280 KB correct
23 Correct 2 ms 340 KB correct
24 Correct 3 ms 340 KB correct
25 Correct 2 ms 340 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 2 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 3 ms 340 KB correct
33 Correct 2 ms 340 KB correct
34 Correct 29 ms 1616 KB correct
35 Correct 29 ms 1792 KB correct
36 Correct 27 ms 1492 KB correct
37 Correct 12 ms 340 KB correct
38 Correct 29 ms 1820 KB correct
39 Correct 28 ms 1608 KB correct
40 Correct 27 ms 1496 KB correct
41 Correct 30 ms 1820 KB correct
42 Correct 29 ms 1748 KB correct
43 Correct 21 ms 1236 KB correct
44 Correct 19 ms 1108 KB correct
45 Correct 21 ms 1096 KB correct
46 Correct 18 ms 980 KB correct
47 Correct 14 ms 676 KB correct
48 Correct 9 ms 320 KB correct
49 Correct 11 ms 340 KB correct
50 Correct 14 ms 596 KB correct
51 Correct 20 ms 1100 KB correct
52 Correct 19 ms 1072 KB correct
53 Correct 18 ms 980 KB correct
54 Correct 21 ms 1320 KB correct
55 Correct 22 ms 1100 KB correct
56 Correct 22 ms 1100 KB correct
57 Correct 22 ms 1144 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 80 ms 4400 KB correct
4 Correct 126 ms 6572 KB correct
5 Correct 129 ms 6548 KB correct
6 Correct 124 ms 6548 KB correct
7 Correct 128 ms 6532 KB correct
8 Correct 130 ms 6600 KB correct
9 Correct 126 ms 6600 KB correct
10 Correct 123 ms 6552 KB correct
11 Correct 126 ms 6464 KB correct
12 Correct 132 ms 6612 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 127 ms 6568 KB correct
15 Correct 121 ms 6572 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 3 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 340 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 3 ms 396 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 3 ms 280 KB correct
23 Correct 2 ms 340 KB correct
24 Correct 3 ms 340 KB correct
25 Correct 2 ms 340 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 2 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 3 ms 340 KB correct
33 Correct 2 ms 340 KB correct
34 Correct 29 ms 1616 KB correct
35 Correct 29 ms 1792 KB correct
36 Correct 27 ms 1492 KB correct
37 Correct 12 ms 340 KB correct
38 Correct 29 ms 1820 KB correct
39 Correct 28 ms 1608 KB correct
40 Correct 27 ms 1496 KB correct
41 Correct 30 ms 1820 KB correct
42 Correct 29 ms 1748 KB correct
43 Correct 21 ms 1236 KB correct
44 Correct 19 ms 1108 KB correct
45 Correct 21 ms 1096 KB correct
46 Correct 18 ms 980 KB correct
47 Correct 14 ms 676 KB correct
48 Correct 9 ms 320 KB correct
49 Correct 11 ms 340 KB correct
50 Correct 14 ms 596 KB correct
51 Correct 20 ms 1100 KB correct
52 Correct 19 ms 1072 KB correct
53 Correct 18 ms 980 KB correct
54 Correct 21 ms 1320 KB correct
55 Correct 22 ms 1100 KB correct
56 Correct 22 ms 1100 KB correct
57 Correct 22 ms 1144 KB correct
58 Correct 0 ms 212 KB correct
59 Correct 1 ms 212 KB correct
60 Correct 80 ms 4400 KB correct
61 Correct 126 ms 6572 KB correct
62 Correct 129 ms 6548 KB correct
63 Correct 124 ms 6548 KB correct
64 Correct 128 ms 6532 KB correct
65 Correct 130 ms 6600 KB correct
66 Correct 126 ms 6600 KB correct
67 Correct 123 ms 6552 KB correct
68 Correct 126 ms 6464 KB correct
69 Correct 132 ms 6612 KB correct
70 Correct 1 ms 212 KB correct
71 Correct 127 ms 6568 KB correct
72 Correct 121 ms 6572 KB correct
73 Correct 0 ms 212 KB correct
74 Correct 131 ms 6692 KB correct
75 Correct 126 ms 6432 KB correct
76 Correct 83 ms 2768 KB correct
77 Correct 130 ms 6516 KB correct
78 Correct 121 ms 6508 KB correct
79 Correct 125 ms 6556 KB correct
80 Correct 124 ms 6368 KB correct
81 Correct 121 ms 5844 KB correct
82 Correct 127 ms 6420 KB correct
83 Correct 104 ms 3788 KB correct
84 Correct 84 ms 4812 KB correct
85 Correct 80 ms 4292 KB correct
86 Correct 72 ms 2896 KB correct
87 Correct 60 ms 2408 KB correct
88 Correct 54 ms 1752 KB correct
89 Correct 53 ms 1688 KB correct
90 Correct 50 ms 1492 KB correct
91 Correct 30 ms 560 KB correct
92 Correct 23 ms 460 KB correct
93 Correct 86 ms 4272 KB correct
94 Correct 68 ms 2940 KB correct
95 Correct 66 ms 2832 KB correct
96 Correct 52 ms 1652 KB correct
97 Correct 55 ms 1752 KB correct
98 Correct 59 ms 2384 KB correct
99 Correct 68 ms 1780 KB correct
100 Correct 37 ms 760 KB correct
101 Correct 23 ms 468 KB correct
102 Correct 92 ms 3496 KB correct
103 Correct 90 ms 3540 KB correct
104 Correct 86 ms 3480 KB correct
105 Correct 108 ms 3476 KB correct