結果
問題 |
No.1928 Make a Binary Tree
|
ユーザー |
![]() |
提出日時 | 2022-05-08 21:48:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 150 ms / 3,000 ms |
コード長 | 1,267 bytes |
コンパイル時間 | 538 ms |
コンパイル使用メモリ | 58,332 KB |
実行使用メモリ | 29,476 KB |
最終ジャッジ日時 | 2024-07-08 10:56:07 |
合計ジャッジ時間 | 7,776 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
/* -*- coding: utf-8 -*- * * 1928.cc: No.1928 Make a Binary Tree - yukicoder */ #include<cstdio> #include<vector> #include<queue> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ typedef vector<int> vi; typedef priority_queue<int> pqi; /* global variables */ vi nbrs[MAX_N]; int ps[MAX_N], cis[MAX_N]; pqi qs[MAX_N]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; nbrs[u].push_back(v); nbrs[v].push_back(u); } ps[0] = -1; for (int u = 0; u >= 0;) { vi &nbru = nbrs[u]; int up = ps[u]; if (cis[u] < nbru.size()) { int v = nbru[cis[u]++]; if (v != up) { ps[v] = u; u = v; } } else { int x0 = 0, x1 = 0; if (! qs[u].empty()) x0 = qs[u].top(), qs[u].pop(); if (! qs[u].empty()) x1 = qs[u].top(), qs[u].pop(); qs[u].push(1 + x0 + x1); //printf("dp[%d] = %d\n", u, qs[u].top()); if (up >= 0) { if (qs[up].size() < qs[u].size()) swap(qs[up], qs[u]); while (! qs[u].empty()) qs[up].push(qs[u].top()), qs[u].pop(); } u = up; } } int x = qs[0].top(); printf("%d\n", x); return 0; }