Submission #577762

#TimeUsernameProblemLanguageResultExecution timeMemory
577762amunduzbaevStar Trek (CEOI20_startrek)C++17
38 / 100
91 ms17112 KiB
#include "bits/stdc++.h" using namespace std; #define ar array typedef int64_t ll; const int N = 1e5 + 5; const int mod = 1e9 + 7; vector<int> edges[N]; int dp[N], sub[N], up[N]; int is[N][2], cnt[N]; ar<int, 2> C{}; void dfs(int u, int p = -1){ cnt[u] = 1; for(auto x : edges[u]){ if(x == p) continue; dfs(x, u); if(dp[x] == 0) dp[u] = 1; cnt[u] += cnt[x]; } sub[u] = dp[u]; } void re(int u, int p = -1){ if(up[u] == 0) dp[u] = 1; ar<int, 2> c {}; c[up[u]]++; for(auto x : edges[u]){ if(x == p) continue; c[dp[x]]++; } for(auto x : edges[u]){ if(x == p) continue; c[dp[x]]--; if(c[0]) up[x] = 1; c[dp[x]]++; } for(auto x : edges[u]){ if(x == p) continue; re(x, u); } } int n; void dfs2(int u, int p = -1){ int f = 0, v = -1; for(auto x : edges[u]){ if(x == p) continue; dfs2(x, u); if(!sub[x]){ f++, v = x; } } if(f > 1){ is[u][1] = cnt[u] * 1ll * n % mod; } else if(f == 1) { is[u][1] = (cnt[u] - cnt[v]) * 1ll * n % mod; is[u][1] = (is[u][1] + is[v][0]) % mod, is[u][0] = (is[u][0] + is[v][1]) % mod; } else { is[u][1] = C[0], is[u][0] = C[1]; for(auto x : edges[u]){ if(x == p) continue; is[u][0] = (is[u][0] + is[x][1]) % mod, is[u][1] = (is[u][1] + is[x][0]) % mod; } } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int d; cin>>n>>d; for(int i=1;i<n;i++){ int a, b; cin>>a>>b; edges[a].push_back(b); edges[b].push_back(a); } dfs(1); up[1] = 1; re(1); for(int i=1;i<=n;i++){ C[dp[i]]++; } dfs2(1); cout<<is[1][1]<<"\n"; } /* 5 1 1 2 2 3 2 4 1 5 */
#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...