#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%d",&a);
#define scan2(a,b) scanf("%d %d",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
#define eps 1e-12
const int N = 2e5+12,INF=1e9+7;
vector <int> g[N],cycle,v;
pii ver[N];
bool bosh[N],vis[N];
void dfs_cycle(int x,int p){
vis[x] = 1;
int i,j;
v.pb(x);
for (i=0;i<g[x].size();++i){
int to = g[x][i];
if (vis[to] == 1 && to != p){
bool f=0;
for (j=0;j<v.size();++j){
if (v[j] == to)f=1;
if (f){
cycle.pb(v[j]);
}
}
}
if (vis[to] == 0){
dfs_cycle(to,x);
}
}
v.pop_back();
}
int dlina[N],colichestvo[N];
int ans,cont;
void dfs_diametr(int x,int p){
int i;
vector <pii> tmp;
for (i=0;i<g[x].size();++i){
if (g[x][i] == p || bosh[g[x][i]])continue;
dfs_diametr(g[x][i],x);
tmp.pb({dlina[g[x][i]],colichestvo[g[x][i]]});
}
if (tmp.size() == 0){
colichestvo[x] = 1;
ret;
}
sort(allr(tmp));
int mx = tmp[0].fr,cnt = tmp[0].sc;
for (i=1;i<tmp.size();++i){
if (tmp[i].fr == mx){
cnt += tmp[i].sc;
}
}
dlina[x] = mx+1;
colichestvo[x] = cnt;
}
int calc(int x,int y,int m){
int dist = min(abs(x-y),m-abs(x-y));
dist += dlina[cycle[x]];
dist += dlina[cycle[y]];
if (ans < dist){
ans = dist;
cont=0;
}
if (dist == ans){
if (abs(x-y) == (m-abs(x-y))){
cont += max(colichestvo[cycle[x]],1)*max(1,colichestvo[cycle[y]])*2;
}
else
cont += colichestvo[cycle[x]]*colichestvo[cycle[y]];
}
}
main(){
int n,i,j;
cin>>n;
for (i=1;i<=n;++i){
int x,y;
cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
dfs_cycle(1,0);
int m = cycle.size();
for (i=0;i<m;++i)
bosh[cycle[i]] = 1;
for (i=0;i<m;++i){
dfs_diametr(cycle[i],0);
// cout <<cycle[i]<<"-"<<dlina[cycle[i]]<<" ";
// cout <<colichestvo[cycle[i]]<<"\n";
}
for (i=0;i<m;++i){
for (j=i+1;j<m;++j){
calc(i,j,m);
}
}
cout <<cont;
}
Compilation message
shymbulak.cpp: In function 'void dfs_cycle(int, int)':
shymbulak.cpp:32:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (i=0;i<g[x].size();++i){
| ~^~~~~~~~~~~~
shymbulak.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (j=0;j<v.size();++j){
| ~^~~~~~~~~
shymbulak.cpp: In function 'void dfs_diametr(int, int)':
shymbulak.cpp:57:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (i=0;i<g[x].size();++i){
| ~^~~~~~~~~~~~
shymbulak.cpp:68:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (i=1;i<tmp.size();++i){
| ~^~~~~~~~~~~
shymbulak.cpp: In function 'int calc(int, int, int)':
shymbulak.cpp:93:1: warning: no return statement in function returning non-void [-Wreturn-type]
93 | }
| ^
shymbulak.cpp: At global scope:
shymbulak.cpp:96:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | main(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
9964 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
10220 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
115 ms |
18284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |