結果
問題 |
No.1928 Make a Binary Tree
|
ユーザー |
![]() |
提出日時 | 2022-05-06 21:35:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 360 ms / 3,000 ms |
コード長 | 1,073 bytes |
コンパイル時間 | 4,661 ms |
コンパイル使用メモリ | 259,324 KB |
最終ジャッジ日時 | 2025-01-29 03:18:36 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 int n; vector<vector<int>> E; vector<int> dp; vector<priority_queue<int>> Q; void dfs(int cur,int p){ //cout<<cur<<endl; dp[cur] = 1; vector<int> t; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; dfs(to,cur); t.push_back(to); Q[cur].push(dp[to]); } if(t.size()==0)return; sort(t.begin(),t.end(),[&](int a,int b){ return Q[a].size()<Q[b].size(); }); swap(Q[cur],Q[t.back()]); rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; while(Q[to].size()!=0){ Q[cur].push(Q[to].top()); Q[to].pop(); } } rep(i,2){ if(Q[cur].size()==0)break; dp[cur] += Q[cur].top(); Q[cur].pop(); } } int main(){ cin>>n; E.resize(n); rep(i,n-1){ int u,v; cin>>u>>v; u--,v--; E[u].push_back(v); E[v].push_back(u); } dp.resize(n); Q.resize(n); dfs(0,-1); cout<<dp[0]<<endl; return 0; }