# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
328194 | poom2904 | Torrent (COI16_torrent) | C++11 | 140 ms | 25452 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>
using namespace std;
int n,a,b;
int x,y;
vector<int> edge[300001];
stack<int> stk;
vector<int> instk(300001,false);
vector<int> dis(300001,0);
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>n>>a>>b;
for(int i=1;i<n;i++)
{
cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
queue<int> q;
q.push(a);
q.push(b);
stk.push(a);
stk.push(b);
instk[a]=instk[b]=true;
while(!q.empty())
{
int u = q.front();
q.pop();
for(auto v :edge[u])if(!instk[v])
{
stk.push(v);
instk[v]=true;
q.push(v);
}
}
while(!stk.empty())
{
int u = stk.top();
stk.pop();
instk[u]=false;
vector<int> vec;
for(auto v : edge[u])if(!instk[v] && v!=a && v!=b)vec.push_back(dis[v]);
sort(vec.begin(),vec.end(),greater<int>());
int max_t=0;
for(int i=0;i<vec.size();i++)max_t=max(max_t,vec[i]+i+1);
dis[u]=max_t;
}
cout<<max(dis[a],dis[b]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |