結果
問題 | No.763 Noelちゃんと木遊び |
ユーザー |
|
提出日時 | 2018-12-13 13:33:28 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 106 ms / 2,000 ms |
コード長 | 914 bytes |
コンパイル時間 | 597 ms |
コンパイル使用メモリ | 70,548 KB |
実行使用メモリ | 17,672 KB |
最終ジャッジ日時 | 2025-03-22 10:34:46 |
合計ジャッジ時間 | 3,273 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include<iostream>#include<vector>#include<cstring>#include<stack>using namespace std;int dp[100000][2] = {};bool memo[100000][2] = {};vector<int> path[100000];int dfs(int cur, int par, int flag){if(memo[cur][flag]) return dp[cur][flag];memo[cur][flag] = true;int ret = flag;if(flag){for(int next : path[cur]){if(next == par) continue;ret += dfs(next, cur, 0);}}else{for(int next : path[cur]){if(next == par) continue;ret += max(dfs(next, cur, 0), dfs(next, cur, 1));}}dp[cur][flag] = ret;return ret;}int main(){int n;cin >> n;int u, v;for(int i = 0; i < n-1; i++){cin >> u >> v;u--, v--;path[u].push_back(v);path[v].push_back(u);}cout << max(dfs(0, -1, 0) , dfs(0, -1, 1)) << endl;return 0;}