#include <bits/stdc++.h>
#include "grader.h"
using namespace std;
vector<int>st_size;
vector<vector<int>>graph;
vector<bool>active;
int st(int node, int parent){
st_size[node]=1;
for(int &child:graph[node]){
if(child==parent || !active[child])continue;
st_size[node]+=st(child,node);
}
return st_size[node];
}
int find_centroid(int node, int parent, int size){
for(int child:graph[node]){
if(child==parent || !active[child])continue;
if(st_size[child]>=size/2)return(find_centroid(child,node,size));
}
return node;
}
vector<int>t;
void build_the_tree(int node, int parent){
t.push_back(node);
for(int child:graph[node]){
if(child==parent || !active[child])continue;
build_the_tree(child,node);
}
}
int findEgg (int N, vector < pair < int, int > > bridges)
{
st_size.resize(N);
graph.resize(N);
active.resize(N,true);
for(int i=0;i<N-1;i++){
graph[bridges[i].first].push_back(bridges[i].second);
graph[bridges[i].second].push_back(bridges[i].first);
}
int total=N;
int parent=0;
while(total>1){
t.clear();
int centroid=find_centroid(parent,-1,st(parent,-1));
active[centroid]=0;
build_the_tree(parent,-1);
if(query(t)){
active.resize(N,0);
for(int i:t)active[i]=true;
total=st_size[parent]-st_size[centroid];
}
else {
for(int i:t)active[i]=false;
active[centroid]=true;
total=st_size[centroid];
parent=centroid;
}
}
return parent;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |