#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=500050;
const int L=25;
int n,par[N][L],dep[N];
vector<int> E[N],l;
void DFS(int v,int pv){
par[v][0]=pv;
dep[v]=dep[pv]+1;
if(E[v].size()==1)l.pb(v);
for(int u:E[v])if(u!=pv)DFS(u,v);
}
int Lift(int v,int k){for(int i=L-1;~i;i>=0)if(k>>i&1)v=par[v][i];return v;}
int LCA(int v,int u){
if(dep[v]>dep[u])swap(u,v);
for(int i=L-1;~i;i--)if(dep[par[u][i]]>=dep[v])u=par[u][i];
for(int i=L-1;~i;i--)if(par[u][i]!=par[v][i])u=par[u][i],v=par[v][i];
return v==u?v:par[v][0];
}
int Dist(int u,int v){return dep[u]+dep[v]-2*dep[LCA(u,v)];}
bool was[N];
int Solve(int u,int v){
for(int i=1;i<=n;i++)was[i]=false;
int l=LCA(u,v);
deque<pair<int,int> > q;
q.pb({l,0});
was[l]=true;
for(int i=u;i!=l;i=par[i][0])q.pb({i,0}),was[i]=true;
for(int i=v;i!=l;i=par[i][0])q.pb({i,0}),was[i]=true;
int d=0;
while(!q.empty()){
pair<int,int> p=q.front();
q.pop_front();
int x=p.first;
d=max(d,p.second);
for(auto&y:E[x])if(!was[y])q.pb({y,p.second+1}),was[y]=true;
}
return d;
}
int main(){
scanf("%i",&n);
for(int i=1;i<n;i++){
int u,v;
scanf("%i%i",&u,&v);
E[u].pb(v);
E[v].pb(u);
}
if(n==2){
printf("0 1");
return 0;
}
int root=1;
for(int i=1;i<=n;i++)if(E[i].size()>1)root=i;
DFS(root,root);
for(int j=1;j<L;j++)for(int i=1;i<=n;i++)par[i][j]=par[par[i][j-1]][j-1];
int sz=(int)l.size();
if(sz==2){
printf("0 1");
return 0;
}
pair<long long,int> ans={-1,0};
for(int i=0;i<sz;i++)for(int j=(i+1)%sz;j<=(i+1)%sz;j++){
long long d=Solve(l[i],l[j])*1LL*Dist(l[i],l[j]);
if(ans.first<d)ans={d,1};
else if(ans.first==d)ans.second++;
}
printf("%lld %i",ans.first,ans.second);
return 0;
}
Compilation message
road.cpp: In function 'int Lift(int, int)':
road.cpp:14:41: warning: for increment expression has no effect [-Wunused-value]
14 | int Lift(int v,int k){for(int i=L-1;~i;i>=0)if(k>>i&1)v=par[v][i];return v;}
| ~^~~
road.cpp: In function 'int main()':
road.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
road.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%i%i",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11976 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11976 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11976 KB |
Output is correct |
7 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |