Submission #834325

# Submission time Handle Problem Language Result Execution time Memory
834325 2023-08-22T13:03:29 Z Lobo Simurgh (IOI17_simurgh) C++17
100 / 100
102 ms 6376 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
 
const int maxn = 550;
const int maxm = maxn*maxn/2;
 
int n, m, h[maxn], mark[maxn], U[maxm], V[maxm];
pair<int,int> low[maxn];
vector<int> g[maxn], span, ins, outs;
int par[maxn], pari[maxn], is_span[maxm];
int in[maxm], out[maxm];
int queryspan;
int ds[maxn], dsz[maxn];


int cnt = 0;
int QUERY(vector<int> edges) {
	assert(edges.size() == n-1);
	cnt++;
	if(cnt > 8000) assert(false);
	return count_common_roads(edges);
}
 
int find(int v) {
	if(v == ds[v]) return v;
	return ds[v] = find(ds[v]);
}
 
void join(int u, int v) {
	if(dsz[u] < dsz[v]) swap(u,v);
	dsz[u]+= dsz[v];
	ds[v] = u;
}
 
vector<int> construct(vector<int> edges) {
	vector<int> finaledges;
 
	for(int i = 0; i < n; i++) {
		ds[i] = i;
		dsz[i] = 1;
	}
 
	for(auto i : edges) {
		int u = find(U[i]);
		int v = find(V[i]);
		if(u != v) {
			join(u,v);
			finaledges.pb(i);
		}
	}
	assert((int) finaledges.size()==n-1);
 	return finaledges;
}
 
void dfslow(int u, int ant) {
	mark[u] = 1;
	low[u] = mp(h[u],-1);
	for(auto i : g[u]) {
		int v = U[i]+V[i]-u;
		if(v == ant) continue;
 
		if(!mark[v]) {
			h[v] = h[u]+1;
			par[v] = u;
			pari[v] = i;
			span.pb(i);
			is_span[i] = 1;
			dfslow(v,u);
			low[u] = min(low[u],low[v]);
		}
		else {
			low[u] = min(low[u],mp(h[v],i));
		}
	}
}
 
void dfs(int u, int ant) {
	for(auto i : g[u]) {
		int v = U[i]+V[i]-u;
		if(v == ant || is_span[i] == 0) continue;
		dfs(v,u);
	}
 
	if(u == 0 || in[pari[u]] || out[pari[u]]) return;
	
	if(low[u] == mp(h[u],-1)) {
		in[pari[u]] = 1;
		ins.pb(pari[u]);
	}
	else {
		int ilow = low[u].sc;
		int ulow = U[ilow];
		int vlow = V[ilow];
		if(h[ulow] > h[vlow]) swap(ulow,vlow);
 
		int v = vlow;
		vector<int> edges;
		while(v != ulow) {
			edges.pb(pari[v]);
			v = par[v];
		}
 
		int know = -1;
		for(auto i : edges) {
			if(in[i] || out[i]) {
				know = i;
			}
		}
 
		// cout << u << " " << ilow << " - " << QUERY(construct(span)) << endl;
 
		if(know == -1) {
			vector<pair<int,int>> cons;
			cons.pb(mp(queryspan,ilow));
			for(auto i : edges) {
				vector<int> doquery;
				for(auto j : span) {
					if(j != i) doquery.pb(j);
				}
				doquery.pb(ilow);
				cons.pb(mp(QUERY(construct(doquery)),i));
			}
 
			sort(all(cons));
			if(cons[0].fr == cons.back().fr) {
				// todo mundo out / 0
				out[ilow] = 1; outs.pb(ilow);
				for(auto i : edges) {
					out[i] = 1; outs.pb(i);
				}
			}
			else {
				for(auto X : cons) {
					int i = X.sc;
					if(X.fr < cons.back().fr) {
						// in / 1
						in[i] = 1; ins.pb(i);
					}
					else {
						// out / 0
						out[i] = 1; outs.pb(i);
					}
				}
			}
		}
		else {
			vector<pair<int,int>> cons;
			cons.pb(mp(queryspan,ilow));
 
			vector<int> doquery;
			for(auto i : span) {
				if(i != pari[u]) {
					doquery.pb(i);
				}
			}
			doquery.pb(ilow);
			cons.pb(mp(QUERY(construct(doquery)),pari[u]));
			doquery.clear();
 
			for(auto i : span) {
				if(i != know) {
					doquery.pb(i);
				}
			}
			doquery.pb(ilow);
			cons.pb(mp(QUERY(construct(doquery)),know));
			doquery.clear();
 
			if(in[know]) {
				if(cons[0].fr == cons[2].fr) {
					in[ilow] = 1; ins.pb(ilow);
				}
				else {
					out[ilow] = 1; outs.pb(ilow);
				}
 
				if(cons[1].fr == cons[2].fr) {
					in[pari[u]] = 1; ins.pb(pari[u]);
				}
				else {
					out[pari[u]] = 1; outs.pb(pari[u]);
				}
			}
			else {
				if(cons[0].fr == cons[2].fr) {
					out[ilow] = 1; outs.pb(ilow);
				}
				else {
					in[ilow] = 1; ins.pb(ilow);
				}
 
				if(cons[1].fr == cons[2].fr) {
					out[pari[u]] = 1; outs.pb(pari[u]);
				}
				else {
					in[pari[u]] = 1; ins.pb(pari[u]);
				}
			}
		}
	}
}
 
vector<int> find_roads(int N, std::vector<int> UU, std::vector<int> VV) {
	n = N;
	m = UU.size();
	for(int i = 0; i < m; i++) {
		U[i] = UU[i];
		V[i] = VV[i];
		g[U[i]].pb(i);
		g[V[i]].pb(i);
	}
 
	par[0] = -1;
	pari[0] = -1;
	dfslow(0,-1);
	queryspan = QUERY(construct(span));
	dfs(0,-1);
 
	for(int u = 0; u < n; u++) {
		int qtdhere = 0;

		{
			vector<int> doquery;
			for(int i = 0; i < g[u].size(); i++) doquery.pb(g[u][i]);
			for(auto i : span) doquery.pb(i);

			vector<int> used = construct(doquery);
			int should = 0;
			for(auto i : used) should+= in[i];
			qtdhere = QUERY(used)-should;
		}
		int L = 0;
		while(L < g[u].size() && qtdhere != 0) {
			int l = L;
			int r = (int) g[u].size()-1;
			int nx = -1;
			while(l <= r) {
				int mid = (l+r)>>1;
				vector<int> doquery;
 
				for(int i = 0; i <= mid; i++) doquery.pb(g[u][i]);
				for(auto i : span) doquery.pb(i);
 
				vector<int> used = construct(doquery);
				int should = 0;
				for(auto i : used) should+= in[i];
 
				if(QUERY(used) > should) {
					nx = mid;
					r = mid-1;
				}
				else {
					l = mid+1;
				}
			}
			if(nx == -1) break;
			in[g[u][nx]] = 1; ins.pb(g[u][nx]);
			L = nx+1;
			qtdhere--;
		}
	}
 	ins = construct(ins);
	assert(ins.size() == n-1);
	assert(QUERY(ins) == n-1);
	return ins;
 
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp: In function 'int QUERY(std::vector<int>)':
simurgh.cpp:24:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   24 |  assert(edges.size() == n-1);
      |         ~~~~~~~~~~~~~^~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:230:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  230 |    for(int i = 0; i < g[u].size(); i++) doquery.pb(g[u][i]);
      |                   ~~^~~~~~~~~~~~~
simurgh.cpp:239:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  239 |   while(L < g[u].size() && qtdhere != 0) {
      |         ~~^~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp:269:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  269 |  assert(ins.size() == n-1);
      |         ~~~~~~~~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB correct
2 Correct 0 ms 340 KB correct
3 Correct 0 ms 320 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 0 ms 340 KB correct
6 Correct 1 ms 328 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 0 ms 340 KB correct
9 Correct 0 ms 340 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 340 KB correct
13 Correct 1 ms 340 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB correct
2 Correct 0 ms 340 KB correct
3 Correct 0 ms 320 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 0 ms 340 KB correct
6 Correct 1 ms 328 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 0 ms 340 KB correct
9 Correct 0 ms 340 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 340 KB correct
13 Correct 1 ms 340 KB correct
14 Correct 1 ms 332 KB correct
15 Correct 1 ms 340 KB correct
16 Correct 1 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 336 KB correct
19 Correct 1 ms 328 KB correct
20 Correct 1 ms 340 KB correct
21 Correct 1 ms 332 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 340 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 340 KB correct
28 Correct 1 ms 328 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 328 KB correct
33 Correct 1 ms 328 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB correct
2 Correct 0 ms 340 KB correct
3 Correct 0 ms 320 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 0 ms 340 KB correct
6 Correct 1 ms 328 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 0 ms 340 KB correct
9 Correct 0 ms 340 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 340 KB correct
13 Correct 1 ms 340 KB correct
14 Correct 1 ms 332 KB correct
15 Correct 1 ms 340 KB correct
16 Correct 1 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 336 KB correct
19 Correct 1 ms 328 KB correct
20 Correct 1 ms 340 KB correct
21 Correct 1 ms 332 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 340 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 340 KB correct
28 Correct 1 ms 328 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 328 KB correct
33 Correct 1 ms 328 KB correct
34 Correct 20 ms 1748 KB correct
35 Correct 20 ms 1748 KB correct
36 Correct 18 ms 1340 KB correct
37 Correct 6 ms 508 KB correct
38 Correct 20 ms 1748 KB correct
39 Correct 19 ms 1576 KB correct
40 Correct 22 ms 1332 KB correct
41 Correct 21 ms 1748 KB correct
42 Correct 21 ms 1748 KB correct
43 Correct 9 ms 1192 KB correct
44 Correct 9 ms 940 KB correct
45 Correct 9 ms 980 KB correct
46 Correct 8 ms 852 KB correct
47 Correct 7 ms 668 KB correct
48 Correct 3 ms 328 KB correct
49 Correct 4 ms 460 KB correct
50 Correct 6 ms 676 KB correct
51 Correct 9 ms 1060 KB correct
52 Correct 9 ms 980 KB correct
53 Correct 8 ms 852 KB correct
54 Correct 11 ms 1236 KB correct
55 Correct 9 ms 980 KB correct
56 Correct 9 ms 980 KB correct
57 Correct 9 ms 980 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB correct
2 Correct 1 ms 340 KB correct
3 Correct 61 ms 4252 KB correct
4 Correct 96 ms 6088 KB correct
5 Correct 95 ms 6096 KB correct
6 Correct 92 ms 6112 KB correct
7 Correct 87 ms 6092 KB correct
8 Correct 85 ms 6196 KB correct
9 Correct 99 ms 6376 KB correct
10 Correct 98 ms 6080 KB correct
11 Correct 94 ms 6044 KB correct
12 Correct 94 ms 6076 KB correct
13 Correct 1 ms 340 KB correct
14 Correct 97 ms 6188 KB correct
15 Correct 96 ms 6144 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB correct
2 Correct 0 ms 340 KB correct
3 Correct 0 ms 320 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 0 ms 340 KB correct
6 Correct 1 ms 328 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 0 ms 340 KB correct
9 Correct 0 ms 340 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 340 KB correct
13 Correct 1 ms 340 KB correct
14 Correct 1 ms 332 KB correct
15 Correct 1 ms 340 KB correct
16 Correct 1 ms 340 KB correct
17 Correct 1 ms 340 KB correct
18 Correct 1 ms 336 KB correct
19 Correct 1 ms 328 KB correct
20 Correct 1 ms 340 KB correct
21 Correct 1 ms 332 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 340 KB correct
25 Correct 1 ms 340 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 340 KB correct
28 Correct 1 ms 328 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 1 ms 340 KB correct
31 Correct 1 ms 340 KB correct
32 Correct 1 ms 328 KB correct
33 Correct 1 ms 328 KB correct
34 Correct 20 ms 1748 KB correct
35 Correct 20 ms 1748 KB correct
36 Correct 18 ms 1340 KB correct
37 Correct 6 ms 508 KB correct
38 Correct 20 ms 1748 KB correct
39 Correct 19 ms 1576 KB correct
40 Correct 22 ms 1332 KB correct
41 Correct 21 ms 1748 KB correct
42 Correct 21 ms 1748 KB correct
43 Correct 9 ms 1192 KB correct
44 Correct 9 ms 940 KB correct
45 Correct 9 ms 980 KB correct
46 Correct 8 ms 852 KB correct
47 Correct 7 ms 668 KB correct
48 Correct 3 ms 328 KB correct
49 Correct 4 ms 460 KB correct
50 Correct 6 ms 676 KB correct
51 Correct 9 ms 1060 KB correct
52 Correct 9 ms 980 KB correct
53 Correct 8 ms 852 KB correct
54 Correct 11 ms 1236 KB correct
55 Correct 9 ms 980 KB correct
56 Correct 9 ms 980 KB correct
57 Correct 9 ms 980 KB correct
58 Correct 0 ms 340 KB correct
59 Correct 1 ms 340 KB correct
60 Correct 61 ms 4252 KB correct
61 Correct 96 ms 6088 KB correct
62 Correct 95 ms 6096 KB correct
63 Correct 92 ms 6112 KB correct
64 Correct 87 ms 6092 KB correct
65 Correct 85 ms 6196 KB correct
66 Correct 99 ms 6376 KB correct
67 Correct 98 ms 6080 KB correct
68 Correct 94 ms 6044 KB correct
69 Correct 94 ms 6076 KB correct
70 Correct 1 ms 340 KB correct
71 Correct 97 ms 6188 KB correct
72 Correct 96 ms 6144 KB correct
73 Correct 1 ms 340 KB correct
74 Correct 96 ms 6148 KB correct
75 Correct 95 ms 6012 KB correct
76 Correct 51 ms 2592 KB correct
77 Correct 102 ms 6112 KB correct
78 Correct 93 ms 6076 KB correct
79 Correct 94 ms 6120 KB correct
80 Correct 96 ms 5916 KB correct
81 Correct 82 ms 5372 KB correct
82 Correct 96 ms 5980 KB correct
83 Correct 75 ms 3532 KB correct
84 Correct 42 ms 3824 KB correct
85 Correct 40 ms 3512 KB correct
86 Correct 34 ms 2580 KB correct
87 Correct 30 ms 2000 KB correct
88 Correct 26 ms 1624 KB correct
89 Correct 33 ms 1720 KB correct
90 Correct 25 ms 1424 KB correct
91 Correct 13 ms 468 KB correct
92 Correct 9 ms 340 KB correct
93 Correct 41 ms 3624 KB correct
94 Correct 33 ms 2592 KB correct
95 Correct 33 ms 2564 KB correct
96 Correct 25 ms 1492 KB correct
97 Correct 26 ms 1620 KB correct
98 Correct 29 ms 2004 KB correct
99 Correct 26 ms 1620 KB correct
100 Correct 18 ms 800 KB correct
101 Correct 13 ms 532 KB correct
102 Correct 38 ms 3156 KB correct
103 Correct 39 ms 3156 KB correct
104 Correct 37 ms 3116 KB correct
105 Correct 37 ms 3076 KB correct