This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 200'002;
int c[N], used[N];
vector<int> g[N], h;
struct el {
int l, q;
}mx1[N], mx2[N], dp[N];
vector<el> a;
el operator+(const el& a, const el& b) {
return { a.l,a.q + b.q };
}
bool operator<(const el& a, const el& b) {
if (a.l == b.l)
return a.q < b.q;
return a.l < b.l;
}
bool ystmx(const int& a, const int& b) {
return mx1[b] < mx1[a];
}
int Dfs(int v, int p) {
used[v] = 1;
for(int to: g[v])
if (to != p)
{
if (used[to])
{
c[v] = 1;
h.push_back(v);
return to;
}
int x = Dfs(to, v);
if (x != 0)
{
c[v] = 1;
h.push_back(v);
return (x == v) ? 0 : x;
}
}
return 0;
}
void DfsDp(int v, int p) {
for (int to : g[v])
if (to != p && c[to] != 1) {
DfsDp(to, v);
el x = { mx1[to].l + 1, mx1[to].q };
if (x.l > mx1[v].l) {
mx2[v] = mx1[v];
mx1[v] = x;
}
else if (x.l == mx1[v].l)
mx1[v].q += x.q;
else if (x.l > mx2[v].l)
mx2[v] = x;
else if (x.l == mx2[v].l)
mx2[v].q += x.q;
if (dp[v].l == dp[to].l)
dp[v] = dp[v] + dp[to];
if (dp[v].l < dp[to].l)
dp[v] = dp[to];
}
if (mx1[v].q == 0)
mx1[v].q++;
el x = { 0,0 }, x1;
x1 = { mx1[v].l + mx2[v].l,mx1[v].q * mx2[v].q };
int sum = 0;
for (int to : g[v])
if (to != p && c[to] != 1)
if (mx1[to].l == mx1[v].l)
sum += (mx1[v].q - mx1[to].q) * mx1[to].q;
sum /= 2;
if (sum != 0)
x = { 2 * mx1[v].l,sum };
if (x.l == dp[v].l)
dp[v].q += x.q;
dp[v] = max(dp[v], x);
if (x1.l == dp[v].l)
dp[v].q += x1.q;
dp[v] = max(dp[v], x1);
}
el t[4 * N];
void Build(int v, int vl, int vr)
{
if (vl == vr)
{
t[v] = { a[vl].l,a[vl].q };
return;
}
int m = (vl + vr) / 2;
Build(v * 2, vl, m);
Build(v * 2 + 1, m + 1, vr);
if (t[v * 2].l == t[v * 2 + 1].l)
{
t[v].l = t[v * 2].l;
t[v].q = t[v * 2].q + t[v * 2 + 1].q;
}
else t[v] = max(t[v * 2], t[v * 2 + 1]);
}
el GetMax(int v, int vl, int vr, int l, int r)
{
if (vl == l && vr == r)
return t[v];
int m = (vl + vr) / 2;
el r1, r2;
if (r <= m)
return GetMax(v * 2, vl, m, l, r);
if (l > m)
return GetMax(v * 2 + 1, m + 1, vr, l, r);
r1 = GetMax(v * 2, vl, m, l, m);
r2 = GetMax(v * 2, m + 1, vr, m + 1, r);
if (r1.l == r2.l)
return { r1.l,r1.q + r2.q };
return max(r1, r2);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
Dfs(1, -1);
el ans = { 0,1 };
for (int i = 1; i <= n; i++)
{
if (c[i])
{
DfsDp(i, -1);
if (dp[i].l == ans.l)
ans.q += dp[i].q;
else ans = max(ans, dp[i]);
}
// cerr << "dp" << i << ' ' << dp[i].l << ' ' << dp[i].q << '\n';
//cerr << "ans " << ans.l << ' ' << ans.q << '\n';
}
for (int v : h)
{
// cerr << v << ' ';
a.push_back(mx1[v]);
}
//cerr << '\n';
for (int v : h)
{
//cerr << mx1[v].l << ' ';
a.push_back(mx1[v]);
}
//cerr << '\n';
//for (auto v : a)
//cerr << v.l << ' ';
//cerr << '\n';
n = (int)a.size() / 2;
for (int i = 0; i < a.size(); i++)
{
a[i].l += i;
// cerr << a[i].l << ' ' << a[i].q << '\n';
//if (a[i].q == 0)a[i].q++;
}
//cerr << '\n';
Build(1, 0, (int)a.size() - 1);
//cerr << "ans " << ans.l << ' ' << ans.q << '\n';
for (int i = 0; i < n; i++)
{
el cur = GetMax(1, 0, 2 * n - 1, i + 1, i + n / 2);
//cerr << i + 1 << "..." << i + n / 2 << "..." << cur.l << ' ' << cur.q << '\n';
cur.l -= 2 * i;
cur.l += a[i].l;
cur.q *= a[i].q;
//cerr << i + 1 << "..." << i + n / 2 << "..." << cur.l << ' ' << cur.q << "\n\n";
if (cur.l == ans.l)
ans.q += cur.q;
else ans = max(ans, cur);
}
cout << ans.q << '\n';
return 0;
}
Compilation message (stderr)
shymbulak.cpp: In function 'int main()':
shymbulak.cpp:158:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<el>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |