#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
/*int query(vector < int > islands){
for(auto u : islands)
cout << u << ' ';
cout << endl;
int ret;
cin >> ret;
return ret;
}*/
vector<int> g[600];
int n, curn;
int ok[600];
int sz[600];
void calcsz(int v, int prt){
sz[v] = 1;
for(auto u : g[v]){
if(u == prt || ok[u] == 0) continue;
calcsz(u, v);
sz[v] += sz[u];
}
}
vector<int> curv, othv;
void get(int v, int prt, vector<int> &curv){
curv.pb(v);
for(auto u : g[v]){
if(u == prt || ok[u] == 0) continue;
get(u, v, curv);
}
}
int dfs(int v, int prt){
vector<pii> child;
child.pb({-1, -1});
for(auto u : g[v])
if(ok[u])
child.pb({sz[u], u});
vector<bitset<600>> bs(child.size());
bs[0][0] = 1;
for(int i = 1; i < child.size(); ++i)
bs[i] = (bs[i-1]|(bs[i-1]<<child[i].ff));
int cur = -1;
if(bs[child.size()-1][(curn+1)/2-1])
cur = (curn+1)/2-1;
else{
for(auto u : g[v]){
if(u == prt || ok[u] == 0) continue;
int bef = sz[u];
sz[u] = sz[v];
sz[v] -= bef;
if(dfs(u, v))
return 1;
sz[v] += bef;
sz[u] = bef;
}
return 0;
}
curv.clear(); othv.clear();
curv.pb(v);
othv.pb(v);
for(int i = child.size()-1; i >= 1; --i){
if(cur-child[i].ff >= 0 && bs[i-1][cur-child[i].ff]){
get(child[i].ss, v, curv);
cur -= child[i].ff;
}
else
get(child[i].ss, v, othv);
}
if(query(curv) == 0)
swap(curv, othv);
return 1;
}
int calc(vector<int> v){
if(v.size() == 1) return v[0];
if(v.size() == 2){
if(query({v[0]}))
return v[0];
return v[1];
}
curn = v.size();
for(int i = 1; i <= n; ++i)
ok[i] = 0;
for(auto u : v)
ok[u] = 1;
int root = v[0];
calcsz(root, 0);
dfs(root, 0);
return calc(curv);
}
int findEgg(int N, vector<pair<int, int>> bridges){
n = N;
for(auto u : bridges){
g[u.ff].pb(u.ss);
g[u.ss].pb(u.ff);
}
vector<int> v;
for(int i = 1; i <= n; ++i)
v.pb(i);
return calc(v);
}
/*int main(){
cout << findEgg(5, {{1, 2}, {1, 3}, {2, 4}, {4, 5}}) << '\n';
}*/
Compilation message
eastereggs.cpp: In function 'int dfs(int, int)':
eastereggs.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 1; i < child.size(); ++i)
| ~~^~~~~~~~~~~~~~
eastereggs.cpp:81:8: error: 'query' was not declared in this scope
81 | if(query(curv) == 0)
| ^~~~~
eastereggs.cpp: In function 'int calc(std::vector<int>)':
eastereggs.cpp:88:12: error: 'query' was not declared in this scope
88 | if(query({v[0]}))
| ^~~~~