Submission #418661

#TimeUsernameProblemLanguageResultExecution timeMemory
418661parsabahramiSubtree (INOI20_subtree)C++17
50 / 100
2075 ms33028 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O2", "fast-math") using namespace std; typedef long long int ll; typedef pair<int, int> pii; #define SZ(x) (int) x.size() #define F first #define S second const int N = 1e5 + 10, MOD = 1e9 + 7; int dp[N], M[N], dp2[N], up[N], P[N], H[N], rt[N], fr[N], to[N], n, m; vector<int> vec[N], adj[N]; int pd[N]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; if (x < 0) x += MOD; } int Find(int v) { return !rt[v] ? v : rt[v] = Find(rt[v]); } void Union(int u, int v) { u = Find(u), v = Find(v); if (u == v) return; vec[v].push_back(u); rt[u] = v; } void preDFS(int v, int p = -1, int pid = -1) { M[v] = 1; P[v] = p; for (int id : adj[v]) { int u = fr[id] ^ to[id] ^ v; if (!M[u]) { H[u] = H[v] + 1; preDFS(u, v, id); } else if (id != pid && H[u] < H[v]) { int w = v; while (w != u) Union(P[w], w), w = P[w]; } } } void DFS(int v, int p = -1) { vector<int> tmp; for (int &u : vec[v]) for (int &w : adj[u]) if (w != p) DFS(w, v); else up[v] = u; } inline int calc_dp2(vector<int> &A) { int ret = 0, S = 0; for (int i = 0; i < SZ(A); i++) { S = A[i] * 1ll * (S + 1) % MOD; add(ret, S); } return ret; } inline int calc_dp1(vector<int> &A) { int ret = calc_dp2(A), S = 1; for (int i = 0; i < SZ(A); i++) { S = 1ll * S * A[i] % MOD; pd[i] = S; if (i > 0) add(pd[i], pd[i - 1]); } S = 1; for (int i = SZ(A) - 1; i > 1; i--) { S = 1ll * S * A[i] % MOD; add(ret, 1ll * S * pd[i - 2] % MOD); } return ret; } void dpDFS(int v, int p = -1) { vector<int> A; if (~p) { vector<int> tmp; while (vec[v].back() != up[v]) tmp.push_back(vec[v].back()), vec[v].pop_back(); vec[v].swap(tmp); for (int &x : tmp) vec[v].push_back(x); } for (int &u : vec[v]) { A.push_back(1); for (int &w : adj[u]) if (w != p) dpDFS(w, v), A.back() = A.back() * 1ll * (dp2[w] + 1) % MOD; } dp[v] = calc_dp1(A); if (SZ(vec[v]) > 1) { int S = 1; for (int &x : A) S = 1ll * S * x % MOD; add(dp[v], -S); } if (~p) { A.pop_back(); add(dp2[v], dp[v] - calc_dp2(A)); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) vec[i] = {i}; for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); adj[fr[i] = u].push_back(i); adj[to[i] = v].push_back(i); } preDFS(1); for (int i = 1; i <= n; i++) Find(i); for (int i = 1; i <= n; i++) if (!rt[i]) rt[i] = i; for (int i = 1; i <= n; i++) { vector<int> tm; for (int id : adj[i]) { int u = fr[id] ^ to[id] ^ i; if (rt[i] != rt[u]) tm.push_back(rt[u]); } adj[i].swap(tm); } DFS(rt[1]); dpDFS(rt[1]); int ret = 0; for (int i = 1; i <= n; i++) if (rt[i] == i) add(ret, dp[i]); printf("%d\n", ret); return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:111:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...