#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef thuc
freopen("input.INP","r",stdin);
freopen("output.OUT","w",stdout);
#else
#endif
}
ll root=1;
const ll N=5e5+10,M=1e9+7;
ll par[N],in[N],cnt[N],n,out[N],cnt2[N];
pll in2[N];
vector<ll> gr[N];
void dfs(ll u)
{
cnt[u]=1;
in[u]=1;
for(auto v:gr[u])
{
if(v==par[u]) continue;
par[v]=u;
dfs(v);
if(maximize(in[u],in[v]+1)) cnt[u]=0;
if(in[u]==in[v]+1)
{
cnt[u]+=cnt[v];
}
}
}
vector<pll> ans;
void dp(ll u)
{
vector<ll> lst;
lst.push_back(out[u]);
for(auto v:gr[u])
{
if(v==par[u]) continue;
lst.push_back(in[v]);
}
sort(lst.begin(),lst.end(),greater<ll>());
vector<pll> vt;
for(auto v:gr[u])
{
if(v==par[u]) continue;
if(in[v]==lst[0]) out[v]=lst[1]+1;
else out[v]=lst[0]+1;
vt.emplace_back(in[v],cnt[v]);
dp(v);
}
sort(vt.begin(),vt.end(),greater<pll>());
if(vt.size()>1)
{
vector<ll> mi;
vector<ll> ha;
for(auto v:vt)
{
if(v.fi==vt[0].fi) mi.push_back(v.se);
}
if(mi.size()<vt.size())
for(auto v:vt)
{
if(v.fi==vt[mi.size()].fi) ha.push_back(v.se);
}
ll dis=out[u];
if(mi.size()>=3)
{
maximize(dis,vt[0].fi);
ll cnt=0;
ll res=0;
for(int i=0; i<mi.size(); i++)
{
res+=mi[i]*cnt;
cnt+=mi[i];
}
ans.emplace_back(dis*vt[0].fi*2,cnt);
return;
}
if(mi.size()==2)
{
if(dis>=vt[0].fi||vt.size()==2)
ans.emplace_back(dis*vt[0].fi*2,mi[0]*mi[1]);
else
{
ll res=0,ok=mi[0]+mi[1];
for(int j=0; j<ha.size(); j++)
{
res+=ok*ha[j];
}
ans.emplace_back(vt[0].fi*(vt[0].fi+vt[2].fi),res);
}
return ;
}
if(mi.size()==1)
{
if(dis>=mi[0])
{
ll res=0;
for(int j=0; j<ha.size(); j++)
{
res+=mi[0]*ha[j];
}
ans.emplace_back(dis*(vt[0].fi+vt[mi.size()].fi),res);
}
else
{
dis=vt[0].fi;
mi=ha;
if(mi.size()>=2)
{
ll cnt=0;
ll res=0;
for(auto v:mi)
{
res+=cnt*v;
cnt+=v;
}
ans.emplace_back(dis*vt[1].fi*2,res);
}
else
{
ha.clear();
if(vt.size()>mi.size()+1)
{
for(auto v:vt) if(v.fi==vt[mi.size()+1].fi) ha.push_back(v.se);
ll res=0;
for(auto v:ha)
{
res+=mi[0]*v;
}
ans.emplace_back(dis*(vt[1].fi+vt[2].fi),res);
}
else
{
ans.emplace_back(0,1);
}
}
}
return;
}
}
}
int main()
{
online();
cin>>n;
if(n==2)
{
cout<<0;
return 0;
}
for(int i=0; i<=n; i++) out[i]=0;
for(int i=1; i<n; i++)
{
ll u,v;
cin>>u>>v;
gr[u].push_back(v);
gr[v].push_back(u);
if(gr[u].size()>1) root=u;
if(gr[v].size()>1) root=v;
}
dfs(root);
dp(root);
sort(ans.begin(),ans.end(),greater<pll>());
cout<<ans[0].fi<<' ';
ll cnt=0;
for(int i=0; i<ans.size(); i++) if(ans[i].fi==ans[0].fi) cnt+=ans[i].se;
cout<<cnt;
}
Compilation message
road.cpp: In function 'void dp(long long int)':
road.cpp:104:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0; i<mi.size(); i++)
| ~^~~~~~~~~~
road.cpp:119:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int j=0; j<ha.size(); j++)
| ~^~~~~~~~~~
road.cpp:132:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int j=0; j<ha.size(); j++)
| ~^~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:201:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
201 | for(int i=0; i<ans.size(); i++) if(ans[i].fi==ans[0].fi) cnt+=ans[i].se;
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21492 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21492 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21080 KB |
Output is correct |
3 |
Correct |
4 ms |
21080 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21492 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17020 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |