제출 #336898

#제출 시각아이디문제언어결과실행 시간메모리
336898dolphingarlicStar Trek (CEOI20_startrek)C++14
38 / 100
83 ms16748 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; const ll MOD = 1e9 + 7; vector<int> graph[100001]; ll win_as_root[100001], critical[100001]; void dfs1(int node = 1, int parent = 0) { for (int i : graph[node]) if (i != parent) { dfs1(i, node); win_as_root[node] += (win_as_root[i] == 0); } if (!win_as_root[node]) critical[node] = 1; for (int i : graph[node]) if (i != parent) { if ((win_as_root[node] == 1 && !win_as_root[i]) || !win_as_root[node]) critical[node] += critical[i]; } } void dfs2(int node = 1, int parent = 0, bool par_win = true, ll par_crit = 0) { win_as_root[node] += !par_win; ll all_crit = 0, lose_crit = 0; critical[node] = (win_as_root[node] == 0); all_crit += par_crit; if (!par_win) all_crit += par_crit; if ((win_as_root[node] == 1 && !par_win) || !win_as_root[node]) critical[node] += par_crit; for (int i : graph[node]) if (i != parent) { all_crit += critical[i]; if (!win_as_root[i]) lose_crit += critical[i]; if ((win_as_root[node] == 1 && !win_as_root[i]) || !win_as_root[node]) critical[node] += critical[i]; } for (int i : graph[node]) if (i != parent) { if (win_as_root[node] - (win_as_root[i] == 0) == 0) dfs2(i, node, false, all_crit - critical[i] + 1); else if (win_as_root[node] - (win_as_root[i] == 0) == 1) dfs2(i, node, true, lose_crit - (win_as_root[i] ? critical[i] : 0)); else dfs2(i, node, true, 0); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, d; cin >> n >> d; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } dfs1(); dfs2(); ll lose_cnt = 0; for (int i = 1; i <= n; i++) lose_cnt += (win_as_root[i] == 0); if (win_as_root[1]) cout << (n * n - lose_cnt * critical[1]) % MOD; else cout << lose_cnt * critical[1] % MOD; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...