結果

問題 No.1752 Up-Down Tree
ユーザー 👑 NachiaNachia
提出日時 2021-11-19 21:52:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,336 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 86,740 KB
実行使用メモリ 11,040 KB
最終ジャッジ日時 2023-08-30 08:12:55
合計ジャッジ時間 2,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
10,532 KB
testcase_01 AC 71 ms
10,544 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 55 ms
10,860 KB
testcase_05 AC 63 ms
10,764 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 32 ms
8,092 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)

using m32 = atcoder::static_modint<1'000'000'007>;

int main(){
  int N; cin >> N;
  vector<vector<int>> E(N);
  rep(i,N-1){
    int u,v; cin >> u >> v; u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }

  vector<int> I = {0};
  vector<int> P(N,-1);
  vector<int> D(N,0);
  rep(i,I.size()){
    int p = I[i];
    for(int e : E[p]) if(P[p] != e){
      P[e] = p;
      D[e] = D[p] + 1;
      I.push_back(e);
    }
  }

  vector<int> Z(N,1);
  for(int i=N-1; i>=1; i--) Z[P[I[i]]] += Z[I[i]];

  vector<int> maxBlack(N,N);
  rep(i,N) maxBlack[i] = min(Z[i],(Z[i]+D[i])/2);
  for(int i=N-1; i>=0; i--){
    int p = I[i];
    if(Z[p] <= D[p]){ maxBlack[p] = Z[p]; continue; }
    int sum = 0;
    for(int e : E[p]) if(P[p] != e) sum += maxBlack[e];
    maxBlack[p] = min(maxBlack[p], sum);
  }

  // rep(i,N) cout << maxBlack[i] << " "; cout << endl;

  int ans = min(N, maxBlack[0] * 2 + 1);
  cout << ans << endl;
  return 0;
}




struct ios_do_not_sync {
    ios_do_not_sync() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0