Submission #880833

#TimeUsernameProblemLanguageResultExecution timeMemory
880833serifefedartarUsmjeri (COCI17_usmjeri)C++17
28 / 140
315 ms88120 KiB
#include <bits/stdc++.h> using namespace std; #define fast ios::sync_with_stdio(0);cin.tie(0); #define s second #define f first typedef long long ll; const ll MOD = 1e9 + 7; const ll LOGN = 21; const ll MAXN = 3e5 + 10000; vector<vector<int>> graph; vector<vector<pair<int,int>>> G; int up[LOGN][MAXN], par[MAXN], sz[MAXN], depth[MAXN], p[MAXN]; int find(int node) { if (node == par[node]) return node; return par[node] = find(par[node]); } void unite(int a, int b) { a = find(a); b = find(b); if (sz[b] > sz[a]) swap(a, b); sz[a] += sz[b]; par[b] = a; int pa = p[a]; int pb = p[b]; if (depth[pa] > depth[pb]) p[a] = pb; else p[a] = pa; } void dfs(int node, int parent) { for (auto u : graph[node]) { if (u == parent) continue; depth[u] = depth[node] + 1; up[0][u] = node; for (int i = 1; i < LOGN; i++) up[i][u] = up[i-1][up[i-1][u]]; dfs(u, node); } } int lift(int node, int k) { for (int i = 0; i < LOGN; i++) { if ((1<<i) & k) node = up[i][node]; } return node; } int lca(int a, int b) { if (depth[b] > depth[a]) swap(a, b); a = lift(a, depth[a] - depth[b]); if (a == b) return a; for (int i = LOGN - 1; i >= 0; i--) { if (up[i][a] != up[i][b]) { a = up[i][a]; b = up[i][b]; } } return up[0][a]; } void merge(int a, int b) { a = find(a); int lst = p[a]; while (depth[lst] > depth[b]) { a = find(a); lst = up[0][lst]; if (depth[lst] > depth[b]) { G[p[a]].push_back({lst, 0}); G[lst].push_back({p[a], 1}); unite(lst, a); lst = p[find(lst)]; } else break; } } int c[MAXN], vis[MAXN]; bool q; void check(int node, int parent, int color) { if (vis[node]) return ; vis[node] = true; c[node] = color; for (auto u : G[node]) { if (u.f == parent) continue; int need = c[node] ^ u.s; if (c[u.f] != -1 && c[u.f] != need) q = false; else check(u.f, node, need); } } int main() { fast memset(c, -1, sizeof(c)); int N, M, a, b; cin >> N >> M; graph = vector<vector<int>>(N+1, vector<int>()); G = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>()); for (int i = 1; i < N; i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } for (int i = 0; i < LOGN; i++) up[i][1] = 1; dfs(1, 1); depth[0] = 1e8; for (int i = 1; i <= N; i++) sz[i] = 1, par[i] = p[i] = i; for (int i = 0; i < M; i++) { cin >> a >> b; int LCA = lca(a, b); merge(a, LCA); merge(b, LCA); if (a != LCA && b != LCA) { G[a].push_back({b, 1}); G[b].push_back({a, 1}); } } ll ans = 1; for (int i = 2; i <= N; i++) { if (!vis[i]) { q = true; check(i, i, 0); if (q) ans = (ans * 2) % MOD; else ans = 0; } } cout << ans << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...