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 st first
#define nd second
#define ll int64_t
using namespace std;
const int SZ=3e5+7;
vector <int> node[SZ];
int dp[SZ],a,b;
short vit[SZ];
void dfs(int v,int p,int z) {
if(node[v].size()==1 && v!=a && v!=b) return ;
vector <int> went;
for(auto x:node[v]) {
if(x==a || x==b || x==p || vit[x]!=z) continue;
dfs(x,v,z);
went.emplace_back(dp[x]);
}
int mx(0);
sort(went.begin(),went.end(),greater<int>());
for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
dp[v]=mx;
return ;
}
int main(void) {
cin.tie(0)->ios::sync_with_stdio(false);
int n;
cin >> n >> a >> b;
for(int i=1,x,y;i<n;i++) {
cin >> x >> y;
node[x].emplace_back(y);
node[y].emplace_back(x);
}
queue <pair<int,int>> hx;
hx.push({a,1});
hx.push({b,2});
while(!hx.empty()) {
auto o=hx.front();
hx.pop();
vit[o.st]=o.nd;
for(auto x:node[o.st]) {
if(!vit[x]) {
vit[x]=o.nd;
hx.push({x,o.nd});
}
}
}
dfs(a,-1,1);
dfs(b,-1,2);
cout << max(dp[a],dp[b]);
}
Compilation message (stderr)
torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<went.size();i++) mx=max(mx,went[i]+i+1);
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |