#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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
63040 KB |
Output is correct |
2 |
Correct |
344 ms |
56856 KB |
Output is correct |
3 |
Incorrect |
845 ms |
61292 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |