#include <bits/stdc++.h>
using namespace std;
vector <pair <int,int>> v[300005];
pair <int,int > tata[300005];
int mar[300005];
void dfs(int x,int dad)
{
mar[x]=1;
for (int i=0;i<v[x].size();i++)
{
int nod = v[x][i].first;
if (nod!=dad)
{
tata[nod]={x,v[x][i].second};
dfs(nod,x);
mar[x]+=mar[nod];
}
}
}
int din[300005];
void dfssol(int x,int muchie_interzisa,int dad)
{
int i,nr=1;
for (int i=0;i<v[x].size();i++)
{
int nod = v[x][i].first;
if (nod!=dad&&v[x][i].second!=muchie_interzisa)
{
dfssol(nod,muchie_interzisa,x);
din[x]=max(din[x],din[nod]+nr);
nr++;
}
}
}
bool compare2 (pair <int,int> a,pair <int,int> b )
{
return mar[a.first]>mar[b.first];
}
int n,a,b,i,j,k;
vector <int> muchii;
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n>>a>>b;
for (i=1;i<=n-1;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back({y,i});
v[y].push_back({x,i});
}
dfs(a,0);
for (i=1;i<=n;i++)
{
sort(v[i].begin(),v[i].end(),compare2);
}
int cop = b;
while (cop!=a)
{
muchii.push_back(tata[cop].second);
cop=tata[cop].first;
}
int minim = 1e9;
for (j=0;j<muchii.size();j++)
{
for (k=1;k<=n;k++)
{
din[k]=0;
}
dfssol(a,muchii[j],0);
dfssol(b,muchii[j],0);
minim=min(minim,max(din[a],din[b]));
}
cout<<minim;
return 0;
}
Compilation message
torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:10:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
torrent.cpp: In function 'void dfssol(int, int, int)':
torrent.cpp:25:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
torrent.cpp:24:9: warning: unused variable 'i' [-Wunused-variable]
24 | int i,nr=1;
| ^
torrent.cpp: In function 'int main()':
torrent.cpp:70:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (j=0;j<muchii.size();j++)
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7376 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2045 ms |
29004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |