結果

問題 No.1928 Make a Binary Tree
ユーザー SSRS
提出日時 2022-05-06 22:27:05
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 809 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 175,888 KB
実行使用メモリ 27,504 KB
最終ジャッジ日時 2024-07-05 23:45:38
合計ジャッジ時間 8,395 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<int> bfs;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    A[i] = c[i].size();
  }
  for (int v : bfs){
    if (A[v] >= 3 && v > 0){
      A[p[v]] += A[v] - 2;
      A[v] = 2;
    }
  }
  cout << N - max(A[0] - 2, 0) << endl;
}
0