//{ <defines>
#ifndef _LOCAL
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _ << ' ' <<
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;
const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;
inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 2e5 + 228;
vi g[N];
char mk[N];
int n, m, ans, i, le;
int p[N], d[N], en[N], h[N], H[N];
vi c;
void cycle(int v) {
if(mk[v] == 3) re;
c.pb(v);
mk[v] = 3;
cycle(p[v]);
}
void rec(int v) {
if(mk[v] == 1) re cycle(v);
mk[v] = 1;
for(int u : g[v]) if(u != p[v] && mk[u] < 2) {
p[u] = v;
rec(u);
}
if(mk[v] == 1) mk[v] = 2;
}
void recc(int v, int pr = -1, int de = 0) {
// cerr << v + 1 _ pr + 1 << endl;
if(~pr) p[v] = pr; d[v] = de; en[v] = c[i]; h[v] = 0, H[v] = 1;
vi z;
for(int u : g[v]) if(u != pr && mk[u] != 3) {
recc(u, v, de + 1);
z.pb(u);
}
int n = sz(z);
ifn(n) re;
sort(bnd(z), [&] (int a, int b) {re h[a] > h[b];});
h[v] = h[z[0]] + 1;
int t = 0;
fo(n) if(h[z[i]] == h[v] - 1) t += H[z[i]];
H[v] = t;
if(h[v] > le) le = h[v], ans = H[v];
ef(h[v] == le) ans += H[v];
if(n == 1) re;
int w = h[z[0]] + h[z[1]];
if(w < le) re;
t = 0;
if(h[z[0]] == h[z[1]]) {
vi e;
fo(n) if(h[z[i]] == h[z[0]]) e.pb(H[z[i]]);
int s = 0; for(int E : e) s += E;
for(int E : e) t += (s - E) * E;
} else {
fo(n) if(h[z[i]] == h[z[1]]) t += H[z[i]];
t *= H[z[0]];
}
if(w == le) ans += t;
ef(w > le) ans = t, le = w;
}
void solve() {
cin >> n;
fo(n) g[i].clear();
c.clear();
fo(n) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].pb(y);
g[y].pb(x);
}
clr(mk, 0);
fo(n) ifn(mk[i]) rec(i);
m = sz(c);
fo(m) p[c[i]] = i;
le = ans = 0;
for(i = 0; i < m; ++i) recc(c[i]);
fo(n) fr(j, i) if(en[i] != en[j]) {
int D = d[i] + d[j];
int p0 = p[en[i]];
int p1 = p[en[j]];
if(p0 > p1) swap(p0, p1);
int w0 = p1 - p0 + D;
int w1 = p0 + m - p1 + D;
int w = min(w0, w1);
if(w == le) ++ans;
ef(w > le) le = w, ans = 1;
if(le == w && w0 == w1) ++ans;
}
cout << ans << endl;
// cerr << le << endl;
}
int main() {
#ifdef _LOCAL
freopen("in.txt", "r", stdin);
int tests; cin >> tests;
fo(tests) {
cerr << "case #" << i+1 << endl;
solve();
}
#else
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
#endif
return 0;
}
Compilation message
shymbulak.cpp: In function 'void recc(int, int, int)':
shymbulak.cpp:78:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(~pr) p[v] = pr; d[v] = de; en[v] = c[i]; h[v] = 0, H[v] = 1;
^~
shymbulak.cpp:78:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(~pr) p[v] = pr; d[v] = de; en[v] = c[i]; h[v] = 0, H[v] = 1;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5244 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5368 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5368 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1557 ms |
10056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |