#include <bits/stdc++.h>
#define N 200005
using namespace std;
vector<int> adj[N];
bool on_cycle[N];
int par[N];
int vis[N];
int dep[N];
pair<int,int> maxi[N];
vector<int> cycle;
pair<int,long long> merge(pair<int,long long> a,pair<int,long long> b){
pair<int,long long> ret = {0,0};
ret.first = max(a.first,b.first);
if(a.first == ret.first)ret.second += a.second;
if(b.first == ret.first)ret.second += b.second;
return ret;
}
pair<int,long long> merge2(pair<int,long long> a,pair<int,long long> b){
return {a.first + b.first,a.second*b.second};
}
struct SegTree{
vector<pair<int,long long>> t;
int n;
SegTree(int size){
n = size;
t.assign(4*n,{-1e9,0});
}
void upd(int v,int tl,int tr,int pos,pair<int,long long> val){
if(tl == tr){
t[v] = merge(t[v],val);
return;
}
int tm = (tl + tr)/2;
if(pos <= tm)
upd(v*2,tl,tm,pos,val);
else upd(v*2+1,tm+1,tr,pos,val);
t[v] = merge(t[v*2],t[v*2+1]);
}
pair<int,long long> get(int v,int tl,int tr,int l,int r){
if(tr < l || r < tl)
return {-1e9,0};
if(l <= tl && tr <= r)
return t[v];
int tm = (tl + tr)/2;
return merge(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void upd(int pos,pair<int,long long> val){
upd(1,0,n,pos,val);
}
pair<int,long long> get(int l,int r){
return get(1,0,n,l,r);
}
};
void dfs(int v,int pr){
vis[v] = 1;
par[v] = pr;
for(auto u:adj[v]){
if(u == pr)continue;
if(cycle.size())return;
if(vis[u] == 1){
while(1){
on_cycle[v] = 1;
cycle.push_back(v);
if(u == v)break;
v = par[v];
}
}
if(cycle.size())return;
dfs(u,v);
}
vis[v] = 2;
}
void dfs1(int v,int pr){
maxi[v] = {dep[v],1};
for(auto u:adj[v]){
if(u == pr || on_cycle[u])continue;
dep[u] = dep[v] + 1;
dfs1(u,v);
maxi[v] = merge(maxi[v],maxi[u]);
}
}
pair<int,long long> get_diameter_and_count(int v){
return {1,1};
}
void solve(){
int n;
cin >> n;
for(int i = 1;i<=n;i++){
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
assert(cycle.size() >= 3);
// for(auto u:cycle){
// cout << u << " ";
// }
// cout << endl;
pair<int,long long> ans = {0,0};
for(auto u:cycle){
ans = merge(ans,get_diameter_and_count(u));
dfs1(u,0);
}
if(cycle.size() % 2){
int half_sz = cycle.size()/2;
int sz = cycle.size();
SegTree t(2*sz);
SegTree t2(2*sz);
for(int i = 0;i<2*sz;i++){
t.upd(i,{maxi[cycle[i%sz]].first + i,maxi[cycle[i%sz]].second});
}
for(int i = 0;i<2*sz;i++){
t2.upd(i,{maxi[cycle[i%sz]].first - i,maxi[cycle[i%sz]].second});
}
for(int i = 0;i<sz;i++){
//cout << cycle[i] << " " << maxi[cycle[i]].first << " " << maxi[cycle[i]].second << endl;
pair<int,long long> nowl = t2.get(i + half_sz + 1,i + sz-1);
nowl.first += i + sz;
pair<int,long long> nowr = t.get(i+1,i+half_sz);
nowr.first -= i;
nowl = merge2(nowl,maxi[cycle[i]]);
nowr = merge2(nowr,maxi[cycle[i]]);
//cout << nowl.first << " " << nowl.second << endl;
//cout << nowr.first << " " << nowr.second << endl;
ans = merge(ans,nowl);
ans = merge(ans,nowr);
}
}
else{
int half_sz = cycle.size()/2 - 1;
int sz = cycle.size();
SegTree t(2*sz);
SegTree t2(2*sz);
for(int i = 0;i<2*sz;i++){
t.upd(i,{maxi[cycle[i%sz]].first + i,maxi[cycle[i%sz]].second});
}
for(int i = 0;i<2*sz;i++){
t2.upd(i,{maxi[cycle[i%sz]].first - i,maxi[cycle[i%sz]].second});
}
for(int i = 0;i<sz;i++){
//cout << cycle[i] << endl;
pair<int,long long> nowl = t2.get(i + half_sz + 2,i + sz-1);
nowl.first += i + sz;
pair<int,long long> nowr = t.get(i+1,i+half_sz);
nowr.first -= i;
pair<int,long long> middle = {half_sz + 1 + maxi[cycle[(i+half_sz+1)%sz]].first,2*maxi[cycle[(i+half_sz+1)%sz]].second};
//cout << nowr.first << " " << nowr.second << endl;
//cout << nowl.first << " " << nowl.second << endl;
//cout << middle.first << " " << middle.second << endl;
nowl = merge2(nowl,maxi[cycle[i]]);
nowr = merge2(nowr,maxi[cycle[i]]);
middle = merge2(middle,maxi[cycle[i]]);
ans = merge(ans,nowl);
ans = merge(ans,nowr);
ans = merge(ans,middle);
}
}
cout << ans.second / 2 << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |