This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
vector<vector<int>> graph;
vector<int> mousepath;
vector<bool> inmousepath;
bool dfs1(int v, int t, int p=-1){
if(v==t)return true;
for(auto u:graph[v]){
if(u==p)continue;
if(dfs1(u,t,v)){
//cout<<"hi\n";
mousepath.push_back(v);
inmousepath[v]=true;
return true;
}
}
return false;
}
int dfs2(int v, int p=-1){
//if(inmousepath[v]&&p!=-1)return 0;
priority_queue<int> pq;
for(auto u:graph[v]){
if(u==p||inmousepath[u])continue;
int x=dfs2(u,v);
pq.push(x);
}
int sum=0;
int i=0;
while (!pq.empty()){
sum++;
if(i%2)sum+=pq.top();
i++;
pq.pop();
}
return sum;
}
void pv(vector<int> & v){
for(auto u:v){
cout<<u<<" ";
}
cout<<endl;
}
void pv(vector<bool> & v){
for(auto u:v){
cout<<u<<" ";
}
cout<<endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,t,m;
cin>>n>>t>>m;
graph.resize(n);
t--;
m--;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
inmousepath.resize(n);
inmousepath[t]=true;
dfs1(m,t);
//cout<<<<endl;
//pv(mousepath);
//pv(inmousepath);
int sol=0;
for(auto u:mousepath){
int aha=dfs2(u);
sol+= aha;
//cout<<aha<<" ";
}
//cout<<endl;
cout<<sol<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |