Submission #763779

#TimeUsernameProblemLanguageResultExecution timeMemory
763779Sam_a17Star Trek (CEOI20_startrek)C++17
100 / 100
86 ms29064 KiB
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> // #include <atcoder/all> #include <cstdio> using namespace std; #ifndef ONLINE_JUDGE #define dbg(x) cerr << #x <<" "; print(x); cerr << endl; #else #define dbg(x) #endif #define sz(x) (int((x).size())) #define len(x) (int)x.length() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(x) (x).clear() #define uniq(x) x.resize(unique(all(x)) - x.begin()); #define pb push_back #define popf pop_front #define popb pop_back #define ld long double #define ll long long void print(long long t) {cerr << t;} void print(int t) {cerr << t;} void print(string t) {cerr << t;} void print(char t) {cerr << t;} void print(double t) {cerr << t;} void print(unsigned long long t) {cerr << t;} void print(long double t) {cerr << t;} template <class T, class V> void print(pair <T, V> p); template <class T> void print(vector <T> v); template <class T> void print(set <T> v); template <class T, class V> void print(map <T, V> v); template <class T> void print(multiset <T> v); template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";} template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";} template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define nl '\n' // for random generations mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); // mt19937 myrand(131); // for grid problems int dx[8] = {-1,0,1,0,1,-1,1,-1}; int dy[8] = {0,1,0,-1,1,1,-1,-1}; long long ka() { long long x = 0; bool z = false; while (1) { char y = getchar(); if (y == '-') z = true; else if ('0' <= y && y <= '9') x = x * 10 + y - '0'; else { if (z) x *= -1; return x; } } } // lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6 void fastIO() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } // file in/out void setIO(string str = "") { fastIO(); if (str != "") { freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } else if(str == "input") { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } } // Indexed Set template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const long long mod = 1e9 + 7; const int N = 2e5 + 10, M = 2e5 + 10; vector<int> adj[N]; long long n, d; long long binPowByMod(long long a, long long b) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long add(long long a, long long b) { return (a + b) % mod; } long long mult(long long a, long long b) { return (a * b) % mod; } long long sub(long long a, long long b) { return (a - b + 2 * mod) % mod; } long long c[N], win, lose; bool lw[N], curr[N]; int dfs_win(int node, int parent) { int ok = 0; for(auto i: adj[node]) { if(i == parent) continue; int cur = dfs_win(i, node); if(!cur) ok = 1; } curr[node] = ok; return ok; } void dfs_up(int node, int parent, bool par) { int on = 0, off = 0; for(auto i: adj[node]) { if(i == parent) continue; if(curr[i]) on++; else off++; } if(par) on++; else off++; if(off) { lw[node] = 1; win++; } else lose++; for(auto i: adj[node]) { if(i == parent) continue; if(curr[i]) on--; else off--; if(off) dfs_up(i, node, true); else dfs_up(i, node, false); if(curr[i]) on++; else off++; } } long long cc = 0; struct tp { bool valid, state; int crit; }; tp dp[N], ans[N]; void dfs_ct(int node, int parent) { dp[node].valid = true; dp[node].state = 0; dp[node].crit = 1; int los = 0; for(auto i: adj[node]) { if(i == parent) continue; dfs_ct(i, node); if(dp[i].state == 0) { dp[node].state = 1; los++; } } if(los >= 2) { dp[node].valid = false; dp[node].crit = 0; } else if(los == 1) { dp[node].crit = 0; for(auto i: adj[node]) { if(i == parent) continue; if(dp[i].state == dp[node].state) { continue; } dp[node].crit += dp[i].crit; } } else if(los == 0) { for(auto i: adj[node]) { if(i == parent) continue; dp[node].crit += dp[i].crit; } } } void dfs_ct_up(int node, int parent, tp par) { int los = 0; ans[node].valid = true; ans[node].state = 0; ans[node].crit = 1; vector<pair<long long, long long>> pri; for(auto i: adj[node]) { if(i == parent) continue; if(dp[i].state == 0) { ans[node].state = 1; pri.emplace_back(i, dp[i].crit); los++; } } if(par.state == 0) { ans[node].state = 1; pri.emplace_back(n + 1, par.crit); los++; } long long lans = 0; if(los >= 2) { ans[node].valid = false; ans[node].crit = 0; } else if(los == 1) { ans[node].crit = 0; for(auto i: adj[node]) { if(i == parent) continue; if(dp[i].state == ans[node].state) { lans += dp[i].crit; continue; } lans += dp[i].crit; ans[node].crit += dp[i].crit; } lans += par.crit; if(ans[node].state != par.state) { ans[node].crit += par.crit; } } else if(los == 0) { for(auto i: adj[node]) { if(i == parent) continue; ans[node].crit += dp[i].crit; } ans[node].crit += par.crit; } for(auto i: adj[node]) { if(i == parent) continue; auto curent = ans[node]; if(dp[i].state == 0) { los--; if(los >= 2) { assert(curent.valid == false); assert(curent.crit == 0); assert(curent.state == 1); dfs_ct_up(i, node, curent); } else if(los == 1) { curent.valid = true; curent.crit = 0; curent.state = 1; for(auto j: pri) { if(j.first == i) continue; curent.crit += j.second; } dfs_ct_up(i, node, curent); } else { curent.valid = true; curent.state = 0; curent.crit = 1; curent.crit += lans; curent.crit -= dp[i].crit; dfs_ct_up(i, node, curent); } los++; } else { if(los == 0) { curent.crit -= dp[i].crit; } dfs_ct_up(i, node, curent); } } } struct Matrix { long long a[2][2] = {{0, 0}, {0, 0}}; Matrix operator *(const Matrix& another) { Matrix product; for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { long long cur = mult(a[i][j], another.a[j][k]); product.a[i][k] = add(product.a[i][k], cur); } } } return product; } }; Matrix binpow(Matrix a, long long power) { power--; Matrix answ = a; while(power > 0) { if(power & 1) { answ = answ * a; } a = a * a; power /= 2; } return answ; } void solve_() { cin >> n >> d; for(int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } // calc win_lose int cur = dfs_win(1, 0); dfs_up(1, 0, true); dfs_ct(1, 0); dfs_ct_up(1, 0, {0, 1, 0}); // calc crit long long E = 0; for(int i = 1; i <= n; i++) { c[i] = ans[i].crit; if(lw[i]) { E += c[i]; E %= mod; } else { E -= c[i]; E += 2 * mod; E %= mod; } } E += 2 * mod; E %= mod; long long answ = 0; Matrix mt; mt.a[0][0] = (n * n) % mod, mt.a[0][1] = 0; mt.a[1][0] = lose, mt.a[1][1] = E; mt = binpow(mt, d); mt.a[1][0] %= mod; if(lw[1]) { answ = sub(binPowByMod(n, 2 * d), mult(c[1], mt.a[1][0])); } else { answ = add(answ, mult(c[1], mt.a[1][0])); } cout << answ << '\n'; } int main() { setIO(); auto solve = [&](int test_case)-> void { while(test_case--) { solve_(); } }; int test_cases = 1; // cin >> test_cases; solve(test_cases); return 0; }

Compilation message (stderr)

startrek.cpp: In function 'void solve_()':
startrek.cpp:361:7: warning: unused variable 'cur' [-Wunused-variable]
  361 |   int cur = dfs_win(1, 0);
      |       ^~~
startrek.cpp: In function 'void setIO(std::string)':
startrek.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...