#include <bits/stdc++.h>
#include "grader.h"
using namespace std;
int n;
vector<int> con[520];
vector<int> children[520];
vector<int> forniv[520];
int level[520];
bool visited[520];
int parent[520];
int rez=-1;
int mxmniv=0;
int root;
int cntq;
map<vector<int>,int> done;
bool fa_query(vector<int> cv)
{
if(done[cv]==0)
{
cntq--;
bool bl = query(cv);
if(bl==0)
done[cv]=-1;
else
done[cv]=1;
}
if(done[cv]==-1)
return 0;
return 1;
}
void dfs(int nod)
{
visited[nod]=1;
mxmniv = max(mxmniv, level[nod]);
forniv[level[nod]].push_back(nod);
for(auto adj:con[nod])
{
if(!visited[adj])
{
level[adj] = level[nod]+1;
parent[adj]=nod;
dfs(adj);
for(int i=0;i<children[adj].size();i++)
children[nod].push_back(children[adj][i]);
}
}
children[nod].push_back(nod);
}
vector<int> aux;
void dfs2(int nod, int niv)
{
visited[nod]=1;
aux.push_back(nod);
for(auto adj:con[nod])
{
if(!visited[adj] && level[adj]<=niv)
{
dfs2(adj, niv);
}
}
}
bool verif_nivel(int niv)
{
for(int i=1;i<=n;i++)
visited[i]=0;
aux.clear();
dfs2(root,niv);
return fa_query(aux);
}
int find_nivel()
{
int st,dr,mij;
st=1;
dr=mxmniv;
while(st<=dr)
{
if(st==dr)
return st;
mij=(st+dr)/2;
if(verif_nivel(mij))
dr=mij;
else
st=mij+1;
}
return mij;
}
int fr[520];
bool verif_interval(int le, int ri, int niv)///returneaza 1 daca oul se afla in nodurile de pe forniv[niv][le..ri]
{
aux.clear();
for(int i=1;i<=n;i++)
fr[i]=0;
for(int i=le;i<=ri;i++)
{
int cur=forniv[niv][i];
while(cur!=-1)
{
fr[cur]++;
cur = parent[cur];
}
}
for(int i=1;i<=n;i++)
{
if(fr[i]>0)
aux.push_back(i);
}
return fa_query(aux);
}
int pls=1;
void dfs3(int nod, int dist)
{
pls=max(pls,dist);
visited[nod]=1;
for(auto adj:con[nod])
{
if(!visited[adj])
dfs3(adj,dist+1);
}
}
int find_root()
{
int mnm=n+2,care;
for(int i=1;i<=n;i++)
{
for(int i=1;i<=n;i++)
visited[i]=0;
pls=1;
dfs3(i,1);
if(pls<mnm)
{
mnm=pls;
care=i;
}
}
return care;
}
int findEgg (int N, vector < pair < int, int > > bridges)
{
mt19937 rng(time(nullptr));
n=N;
if(n<=16)
cntq = 4;
else if(n<=500)
cntq = 9;
else
cntq = 9;
done.clear();
for(int i=1;i<=N;i++)
{
visited[i]=0;
con[i].clear();
children[i].clear();
forniv[i].clear();
}
rez=-1;
mxmniv=0;
for(auto x:bridges)
{
con[x.first].push_back(x.second);
con[x.second].push_back(x.first);
}
root = find_root();
level[root] = 1;
parent[root] = -1;
for(int i=1;i<=n;i++)
visited[i]=0;
dfs(root);
int niv = find_nivel();
int st,dr,mij=0;
st=0;
dr=forniv[niv].size()-1;
while(st<=dr)
{
if(st==dr)
return forniv[niv][st];
if(dr-st+1<=cntq)
{
for(int i=st;i<=dr;i++)
{
if(query({forniv[niv][i]}))
return forniv[niv][i];
}
}
mij=(st+dr)/2;
if(verif_interval(1,mij,niv))
dr=mij;
else
st=mij+1;
}
return forniv[niv][mij];
}
Compilation message
eastereggs.cpp: In function 'void dfs(int)':
eastereggs.cpp:44:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<children[adj].size();i++)
| ~^~~~~~~~~~~~~~~~~~~~~
eastereggs.cpp: In function 'int find_root()':
eastereggs.cpp:140:12: warning: 'care' may be used uninitialized in this function [-Wmaybe-uninitialized]
140 | return care;
| ^~~~
eastereggs.cpp: In function 'int find_nivel()':
eastereggs.cpp:75:15: warning: 'mij' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | int st,dr,mij;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
480 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
504 KB |
Number of queries: 9 |
2 |
Runtime error |
9 ms |
660 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
7 ms |
1276 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |