# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
72632 | bnahmad15 | Mousetrap (CEOI17_mousetrap) | C++17 | 0 ms | 0 KiB |
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>
#define forn(i,j,n) for(int i = (int)j;i<=(int)n;i++)
#define nfor(i,j,n) for(int i = (int)j;i>=(int)n;i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 2000001,LOG = 17;
int n,t,m,dep[N],d[N],src;
vector<pair<int,pii> > g[N];
int dfs1(int u,int prnt,int depth){
dep[u] = depth;
d[u] = depth;
int ret = 0;
forn(i,0,g[u].size() - 1){
int v = g[u][i].second.second;
if(v == prnt)
continue;
int tmp = dfs1(v,u,depth+1);
ret = max(ret,tmp);
dep[u] = max(dep[u],dep[v]);
g[u][i].first = tmp;
g[u][i].second.first = -dep[v];
}
if(u == t)
return 1;
return ret;
}
void dfs(int node,int prnt,int &one,int &two,int in){
if(node == t)
return;
int id = 0;
while(one <= two && id < g[node].size() && g[node][id].first != 1){
g[node][id].first = -1;
if(g[node][id].second.second != prnt && g[node][id].first != -1)
one++;
id++;
}
if(in == 1)
src = node;
while(id < g[node].size() && g[node][id].first != 1){
if(g[node][id].second.second != prnt && g[nodde][id].first != -1){
++two;
dfs(g[node][id].second.second,node,one,two,2);
}
id++;
}
if(in != 1){
++one;
++two;
return;
}
while(id < g[node].size()){
if(g[node][id].second.second != prnt && g[node][id].first == 1){
++two;
dfs(g[node][id].second.second,node,one,two,1);
break;
}
id++;
}
}
int main(){
scanf("%d%d%d",&n,&t,&m);
forn(i,1,n-1){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(make_pair(0,make_pair(0,v)));
g[v].push_back(make_pair(0,make_pair(0,u)));
}
dfs1(m,0,0);
forn(i,1,n){
sort(g[i].begin(),g[i].end());
}
int one = 0,two = 0;
dfs(m,0,one,two,1);
cout<<one;
return 0;
}