結果
| 問題 | No.3514 Majority Driven Tree |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-24 21:59:13 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 1,050 bytes |
| 記録 | |
| コンパイル時間 | 2,270 ms |
| コンパイル使用メモリ | 221,244 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-04-24 21:59:25 |
| 合計ジャッジ時間 | 3,356 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 37 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
vector<vector<int>> Graph(N);
for(int i=0; i<N-1; i++){
int u,v; cin >> u >> v;
u--; v--;
Graph.at(u).push_back(v);
Graph.at(v).push_back(u);
}
auto dfs = [&](auto dfs,int pos,int back) -> pair<int,int> {
vector<pair<int,int>> kid;
for(auto to : Graph.at(pos)) if(to != back) kid.push_back(dfs(dfs,to,pos));
if(kid.size() == 0) return {1,0};
int retw = 0,retb = 0,sumb = 0,sumw = 0;
vector<int> D;
for(auto [w,b] : kid) sumb += b,D.push_back(w-b);
sort(D.begin(),D.end());
D.push_back(1001001);
retw = retb = sumb+1;
int n = Graph.at(pos).size();
for(int i=0; i<n/2; i++) sumb += D.at(i);
retb = min(retb,sumb);
sumb += D.at(n/2);
retw = min(retw,sumb);
return {retw,retb};
};
auto [W,B] = dfs(dfs,0,-1);
cout << W << endl;
}