#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+6;
ll par[MAXN],n,depth[MAXN];
vector<ll>g[MAXN];
vector<ll>cycle;
bool vis[MAXN];
bool found;
ll x1,x2;
ll cycle_size;
void find_cycle(ll u,ll p)
{
par[u]=p;
for(auto xd:g[u])
{
if(par[xd]==0)
{
depth[xd]=depth[u]+1;
find_cycle(xd,u);
}
else
{
x1=u;
x2=xd;
}
}
}
void make_cycle()
{
if(depth[x1]<depth[x2])swap(x1,x2);
cycle.push_back(x1);
for(;;)
{
x1=par[x1];
cycle.push_back(x1);
if(x1==x2)break;
}
}
pair<ll,ll> maxd[MAXN];
pair<ll,ll> calc(ll u)
{
vis[u]=1;
pair<ll,ll> ret={0,1};
for(auto xd:g[u])
{
if(vis[xd])continue;
pair<ll,ll> f=calc(xd);
f.first++;
if(f.first==ret.first)ret.second+=f.second;
else if(f.first>ret.first)ret=f;
}
return maxd[u]=ret;
}
pair<ll,ll>maxdist;
ll ans;
ll val1[MAXN],val2[MAXN];
map<ll,ll> mp1,mp2;
int main()
{
maxdist={0,0};
cin>>n;
for(ll i=1;i<=n;i++)
{
ll x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
find_cycle(1,-1);
make_cycle();
cycle_size=cycle.size();
for(ll i=0;i<cycle_size;i++)
{
cycle.push_back(cycle[i]);
}
ll t=cycle_size/2;
for(ll i=1;i<=cycle_size;i++)
{
vis[cycle[i]]=1;
}
for(ll i=1;i<=cycle_size;i++)
{
calc(cycle[i]);
}
set<ll>s1,s2;
for(ll i=0;i<cycle_size*2;i++)
{
val1[i]=-i+maxd[cycle[i]].first;
val2[i]=i+maxd[cycle[i]].first;
}
for(ll i=0;i<t;i++)
{
s1.insert(val1[i]);
mp1[val1[i]]+=maxd[cycle[i]].second;
//s2.insert(val2[t+1+i]);
//mp2[val2[t+1+i]]+=maxd[cycle[t+1+i]].second;
}
//cout<<t<<" "<<cycle[t]<<":\n";
ll max1=(*s1.rbegin());
//ll max2=(*s2.rbegin());
ll cnt,dist;
cnt=mp1[max1]*maxd[cycle[t]].second;
dist=max1+t+maxd[cycle[t]].first;
//cout<<dist<<" "<<cnt<<endl;
if(dist>maxdist.first)
{
maxdist.first=dist;
maxdist.second=0;
}
if(dist==maxdist.first)
{
maxdist.second+=cnt;
}
/*cnt=mp2[max2]*maxd[cycle[t]].second;
dist=max2-t+maxd[cycle[t]].first;
cout<<dist<<" "<<cnt<<endl;
if(dist>maxdist.first)
{
maxdist.first=dist;
maxdist.second=0;
}
if(dist==maxdist.first)
{
maxdist.second+=cnt;
}*/
for(ll mid=t+1;mid<=cycle_size+t;mid++)
{
//cout<<mid<<" "<<cycle[mid]<<":\n";
s1.erase(val1[mid-t-1]);
mp1[val1[mid-t-1]]-=maxd[cycle[mid-t-1]].second;
//s2.erase(val2[mid]);
//mp2[val2[mid]]-=maxd[cycle[mid]].second;
s1.insert(val1[mid-1]);
mp1[val1[mid-1]]+=maxd[cycle[mid-1]].second;
//s2.insert(val2[mid+t]);
//mp2[val2[mid+t]]+=maxd[cycle[mid+t]].second;
max1=(*s1.rbegin());
//max2=(*s2.rbegin());
cnt=mp1[max1]*maxd[cycle[mid]].second;
dist=max1+mid+maxd[cycle[mid]].first;
//cout<<dist<<" "<<cnt<<endl;
if(dist>maxdist.first)
{
maxdist.first=dist;
maxdist.second=0;
}
if(dist==maxdist.first)
{
maxdist.second+=cnt;
}
/*cnt=mp2[max2]*maxd[cycle[mid]].second;
dist=max2-mid+maxd[cycle[mid]].first;
cout<<dist<<" "<<cnt<<endl;
if(dist>maxdist.first)
{
maxdist.first=dist;
maxdist.second=0;
}
if(dist==maxdist.first)
{
maxdist.second+=cnt;
}
*/
}
//cout<<maxdist.first<<" "<<maxdist.second<<endl;
ans=maxdist.second;
if(cycle_size%2==0)
{
for(ll i=0;i<t;i++)
{
ll j=i+t;
ll dist=maxd[cycle[i]].first+t+maxd[cycle[j]].first;
if(dist==maxdist.first)
{
ans+=maxd[cycle[i]].second*maxd[cycle[j]].second;
}
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
12120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |