#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+1);
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)
{
graph.clear();
st_size.clear();
active.clear();
st_size.resize(N);
graph.resize(N);
active.resize(N,true);
for(int i=0;i<N-1;i++){
graph[bridges[i].first-1].push_back(bridges[i].second-1);
graph[bridges[i].second-1].push_back(bridges[i].first-1);
}
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-1]=true;
total=st_size[parent]-st_size[centroid];
}
else {
for(int i:t)active[i-1]=false;
active[centroid]=true;
total=st_size[centroid];
parent=centroid;
}
}
return parent+1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Number of queries: 4 |
2 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
208 KB |
Number of queries: 8 |
2 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
336 KB |
Number of queries: 9 |
2 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |