Submission #1232999

#TimeUsernameProblemLanguageResultExecution timeMemory
1232999LaMatematica14Star Trek (CEOI20_startrek)C++20
38 / 100
65 ms18244 KiB
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int main() { long long N, D; cin >> N >> D; vector<vector<long long>> adj(N+1); // occhio 1-based for (long long i = 0; i < N-1; i++) { long long u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<long long> nl(N+1, 0); // count number of loosing from there function<void(long long,long long)> dfs = [&](long long a, long long p) { for (long long x : adj[a]) { if (x == p) continue; dfs(x, a); if (nl[x] == 0) nl[a]++; } }; vector<long long> w(N+1, 0); // is this a winning root? function<void(long long,long long)> decroot = [&](long long a, long long p) { if (w[p] == 0) nl[a]++; else if (nl[a] == 0 && nl[p] == 1) nl[a]++; if (nl[a] > 0) w[a] = 1; for (long long x : adj[a]) { if (x == p) continue; decroot(x, a); } }; long long swappable = 0; function<void(long long, long long)> cswap = [&](long long a, long long p) { if (nl[a] > 1) return; if (nl[a] == 0) { swappable++; for (long long x : adj[a]) { if (x == p) continue; cswap(x, a); } } else { for (long long x : adj[a]) { if (x == p || nl[x] > 0) continue; cswap(x, a); } } }; dfs(1, 0); long long ln = 0; for (long long i = 1; i <= N; i++) ln += ((nl[i] == 0) ? 1 : 0); long long wn = N-ln; cswap(1, 0); w[1] = nl[1] > 0; for (long long x : adj[1]) decroot(x, 1); long long wr = 0; for (long long i = 1; i <= N; i++) wr += (w[i] ? 1 : 0); long long tot = 0; if (w[1]) { // non cambio nulla tot += wn*N; tot += (ln-swappable)*N; tot += swappable*wr; } else { tot += swappable*(N-wr); } cout << tot%mod << "\n"; }
#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...