Submission #103631

#TimeUsernameProblemLanguageResultExecution timeMemory
103631luciocfElection Campaign (JOI15_election_campaign)C++14
0 / 100
601 ms14740 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; int n, m; int A[maxn], B[maxn], C[maxn]; int nivel[maxn], pai[maxn], sz[maxn]; int chain[maxn], head[maxn], pos[maxn]; int cc, pp; int tree[4*maxn], lazy[4*maxn]; int dp[maxn]; vector<int> grafo[maxn], op[maxn]; void flush(int node, int l, int r) { if (lazy[node] == -1) return; if (l != r) lazy[2*node] = lazy[2*node+1] = lazy[node]; tree[node] = (r-l+1)*lazy[node]; } void upd(int node, int l, int r, int a, int b, int v) { flush(node, l, r); if (l > b || r < a) return; if (l >= a && r <= b) { lazy[node] = v; flush(node, l, r); return; } int mid = (l+r)>>1; upd(2*node, l, mid, a, b, v); upd(2*node+1, mid+1, r, a, b, v); } int soma(int node, int l, int r, int a, int b) { if (a > b) return 0; flush(node, l, r); if (l > b || r < a) return 0; if (l >= a && r <= b) return tree[node]; int mid = (l+r)>>1; return soma(2*node, l, mid, a, b)+soma(2*node+1, mid+1, r, a, b); } void dfs(int u, int p) { sz[u] = 1; for (auto v: grafo[u]) { if (v == p) continue; pai[v] = u, nivel[v] = nivel[u]+1; dfs(v, u); sz[u] += sz[v]; } } void hld(int u) { if (!head[cc]) head[cc] = u; chain[u] = cc, pos[u] = ++pp; int maior = -1, ind = -1; for (auto v: grafo[u]) if (v != pai[u] && sz[v] > maior) maior = sz[v], ind = v; if (ind != -1) hld(ind); for (auto v: grafo[u]) if (v != pai[u] && v != ind) cc++, hld(v); } int lca(int u, int v) { while (chain[u] != chain[v]) { if (nivel[head[chain[u]]] > nivel[head[chain[v]]]) u = pai[head[chain[u]]]; else v = pai[head[chain[v]]]; } if (nivel[u] > nivel[v]) return v; return u; } int query(int u, int v) { int ans = 0; while (true) { if (chain[u] == chain[v]) return ans+soma(1, 1, n, pos[v], pos[u]); ans += soma(1, 1, n, pos[head[chain[u]]], pos[u]); u = pai[head[chain[u]]]; } } int solve(int u, int p) { int ans = 0; for (auto v: grafo[u]) { if (v == p) continue; solve(v, u); ans += dp[v]; } dp[u] = ans; for (auto i: op[u]) { int a = A[i], b = B[i]; int x = query(a, u) + query(b, u); if (x == 0) { dp[u] = ans+1; upd(1, 1, n, pos[u], pos[u], 1); } } } int main(void) { memset(lazy, -1, sizeof lazy); scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs(1, 0); hld(1); scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &A[i], &B[i], &C[i]); op[lca(A[i], B[i])].push_back(i); } solve(1, 0); printf("%d\n", dp[1]); }

Compilation message (stderr)

election_campaign.cpp: In function 'int solve(int, int)':
election_campaign.cpp:148:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:154:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:159:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:167:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:171:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &A[i], &B[i], &C[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...