結果
| 問題 |
No.3272 Separate Contractions
|
| コンテスト | |
| ユーザー |
遭難者
|
| 提出日時 | 2025-08-04 16:50:41 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 469 ms / 3,000 ms |
| コード長 | 6,517 bytes |
| コンパイル時間 | 5,935 ms |
| コンパイル使用メモリ | 335,476 KB |
| 実行使用メモリ | 75,492 KB |
| 最終ジャッジ日時 | 2025-09-11 00:35:10 |
| 合計ジャッジ時間 | 21,891 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using std::cerr;
using std::cin;
using std::cout;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using mint = atcoder::modint998244353;
istream &operator>>(istream &is, mint &a) {
long long t;
is >> t;
a = t;
return is;
}
ostream &operator<<(ostream &os, mint a) { return os << a.val(); }
#endif
typedef long double ld;
#define long long long
#define uint unsigned int
#define ull unsigned long
#define overload3(a, b, c, name, ...) name
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(__i, n)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define per3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define per2(i, n) per3(i, 0, n)
#define per1(n) per2(__i, n)
#define per(...) overload3(__VA_ARGS__, per3, per2, per1)(__VA_ARGS__)
#define all(a) a.begin(), a.end()
#define UNIQUE(a) \
sort(all(a)); \
a.erase(unique(all(a)), a.end())
#define sz(a) (int)a.size()
#define vec vector
#ifndef DEBUG
#define cerr \
if (0) \
cerr
// #undef assert
// #define assert(...) void(0)
#undef endl
#define endl '\n'
#endif
template <typename T> ostream &operator<<(ostream &os, vector<T> a) {
const int n = a.size();
rep(i, n) {
os << a[i];
if (i + 1 != n)
os << " ";
}
return os;
}
template <typename T, size_t n>
ostream &operator<<(ostream &os, array<T, n> a) {
rep(i, n) {
os << a[i];
if (i + 1 != n)
os << " ";
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
for (T &i : a)
is >> i;
return is;
}
template <typename T, typename S> bool chmin(T &x, S y) {
if ((T)y < x) {
x = (T)y;
return true;
}
return false;
}
template <typename T, typename S> bool chmax(T &x, S y) {
if (x < (T)y) {
x = (T)y;
return true;
}
return false;
}
template <typename T> void operator++(vector<T> &a) {
for (T &i : a)
++i;
}
template <typename T> void operator--(vector<T> &a) {
for (T &i : a)
--i;
}
template <typename T> void operator++(vector<T> &a, int) {
for (T &i : a)
i++;
}
template <typename T> void operator--(vector<T> &a, int) {
for (T &i : a)
i--;
}
vec<long> solve(vec<int> uu, vec<int> vv) {
const int n = sz(uu) + 1;
vec<vec<int>> g(n);
vec<vec<pair<int, int>>> gi(n);
rep(i, n - 1) {
g[uu[i]].push_back(vv[i]);
g[vv[i]].push_back(uu[i]);
gi[uu[i]].emplace_back(vv[i], i);
gi[vv[i]].emplace_back(uu[i], i);
}
constexpr int INF = 1e9;
auto bfs = [&](int to) {
queue<int> q;
q.push(to);
vec<int> ans(n, INF);
ans[to] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : g[x]) {
if (ans[i] != INF)
continue;
ans[i] = ans[x] + 1;
q.push(i);
}
}
return ans;
};
auto res0 = bfs(0);
int t1 = 0;
rep(i, n) if (res0[i] > res0[t1]) t1 = i;
auto res1 = bfs(t1);
int t2 = 0;
rep(i, n) if (res1[i] > res1[t2]) t2 = i;
auto res2 = bfs(t2);
int di = res1[t2];
long ans0 = 0;
rep(i, n) ans0 += max(res1[i], res2[i]);
assert(di % 2 == 0);
cerr << res1 << endl << res2 << endl;
int c = -1;
rep(i, n) if (res1[i] == di / 2 && res2[i] == di / 2) {
c = i;
break;
}
cerr << c << endl;
auto resc = bfs(c);
vec<int> toi;
rep(i, n) if (resc[i] == di / 2) toi.push_back(i);
assert(sz(toi) >= 2);
vec<bool> cri_ty(n);
queue<int> q;
for (int st : toi) {
cri_ty[st] = true;
q.push(st);
}
vec<vec<pair<int, int>>> gg(n);
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto [i, idx] : gi[x]) {
if (resc[i] > resc[x])
continue;
gg[i].emplace_back(x, idx);
if (cri_ty[i])
continue;
cri_ty[i] = true;
q.push(i);
break;
}
}
cerr << "cri_ty : " << cri_ty << endl;
vec<int> cri(n - 1, -1); // 親が gg[c] のどちらの頂点か
assert(sz(gg[c]) >= 2);
if (sz(gg[c]) == 2) {
auto f = [&](auto f, int to, int v) -> void {
if (sz(gg[to]) >= 2)
return;
for (auto [i, idx] : gg[to]) {
cri[idx] = v;
f(f, i, v);
}
};
for (auto [i, idx] : gg[c]) {
cri[idx] = i;
f(f, i, i);
}
}
vec<int> buc(n, 1); // c を根とした部分木の大きさ
auto f = [&](auto f, int to, int no) -> void {
for (int i : g[to]) {
if (i == no)
continue;
f(f, i, to);
buc[to] += buc[i];
}
};
f(f, c, -1);
vec<long> rans(n - 1);
rep(i, n - 1) {
int u = uu[i], v = vv[i];
if (resc[u] > resc[v])
swap(u, v);
long ans = ans0;
ans -= max(res1[v], res2[v]);
ans -= buc[v] - 1;
if (cri[i] != -1) {
assert(gg[c][0].first == cri[i] || gg[c][1].first == cri[i]);
int x = gg[c][0].first + gg[c][1].first - cri[i];
ans -= buc[x];
}
rans[i] = ans;
}
return rans;
}
void solve() {
int n;
cin >> n;
vec<vec<int>> g(n);
vec<vec<pair<int, int>>> gi(n);
vec<int> uu(n - 1), vv(n - 1);
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--, v--;
uu[i] = u, vv[i] = v;
g[u].push_back(v);
g[v].push_back(u);
gi[u].emplace_back(v, i);
gi[v].emplace_back(u, i);
}
constexpr int INF = 1e9;
auto bfs = [&](int to) {
queue<int> q;
q.push(to);
vec<int> ans(n, INF);
ans[to] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : g[x]) {
if (ans[i] != INF)
continue;
ans[i] = ans[x] + 1;
q.push(i);
}
}
return ans;
};
auto res0 = bfs(0);
int t1 = 0;
rep(i, n) if (res0[i] > res0[t1]) t1 = i;
auto res1 = bfs(t1);
int t2 = 0;
rep(i, n) if (res1[i] > res1[t2]) t2 = i;
auto res2 = bfs(t2);
int di = res1[t2];
long ans0 = 0;
rep(i, n) ans0 += max(res1[i], res2[i]);
if (di % 2 == 0) {
auto ans = solve(uu, vv);
for (long v : ans)
cout << v << endl;
return;
}
int idx = -1;
rep(i, n - 1) {
bool ok = false;
ok |= res1[uu[i]] == di / 2 && res2[vv[i]] == di / 2;
ok |= res1[vv[i]] == di / 2 && res2[uu[i]] == di / 2;
if (ok) {
assert(idx == -1);
idx = i;
}
}
assert(idx != -1);
auto u = uu, v = vv;
int u0 = uu[idx], v0 = vv[idx];
u[idx] = u0, v[idx] = n;
u.push_back(n), v.push_back(v0);
auto ans = solve(u, v);
ans[idx] = ans0;
rep(i, n - 1) cout << ans[i] - di / 2 - n << endl;
return;
}
int main() {
// srand((unsigned)time(NULL));
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int t = 1;
// cin >> t;
while (t--)
solve();
}
遭難者