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;
#define int long long
#define ff first
#define ss second
#define pb push_back
const int N=3e3+5;
bool vis[N];
vector<int>v[N];
vector<pair<int,int>>rts;
vector<int>vec;
void dfs(int a, pair<int,int>p ){
int x=p.ff;
int y=p.ss;
vec.pb(a);
vis[a]=1;
for(auto b:v[a]){
if((a==x and b==y) or (a==y and b==x)){
continue;
}
if(vis[b]==0)dfs(b,p);
}
}
int dist[N][N];
signed main(){
int n;
cin>>n;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
dist[a][b]=-100000;
dist[b][a]=-100000;
v[a].pb(b);
v[b].pb(a);
rts.pb({a,b});
}
int z=0;
for(auto p:rts){
int x=p.ff,y=p.ss;
memset(vis,0,sizeof vis);
vec.clear();
dfs(x,p);
vector<int>vc=vec;
vec.clear();
dfs(y,p);
for(auto w:vec){
for(auto e:vc){
dist[w][e]++;
dist[e][w]++;
z=max(z,dist[w][e]);
}
}
}
int k=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(dist[i][j]==z){
k++;
}
}
}
cout<<k<<endl;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(dist[i][j]==z){
k++;
cout<<i<<" "<<j<<endl;
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |