Submission #43358

#TimeUsernameProblemLanguageResultExecution timeMemory
43358szawinisSynchronization (JOI13_synchronization)C++14
100 / 100
279 ms20348 KiB
#include <bits/stdc++.h> #define rep(i, a, b) for(int i = a; i < b; i++) #define repi(i, a, b) for(int i = a; i <= b; i++) #define drep(i, a, b) for(int i = a; i >= b; i--) using namespace std; const int N = 1 << 18; int n, m, q, u[N], v[N], d[N], s[N], e[N], ord[N], res[N], last[N]; bool state[N]; vector<int> g[N]; void dfs(int u, int p) { static int idx = 0; s[u] = ++idx; ord[s[u]] = u; for(int v: g[u]) if(v != p) d[v] = d[u] + 1, dfs(v, u); e[u] = idx; } int t[2*N]; void build(int i = 1, int l = 0, int r = N-1) { if(l == r) { t[i] = e[ord[l]]; return; } int mid = l+r >> 1; build(i<<1, l, mid); build(i<<1|1, mid+1, r); t[i] = max(t[i<<1], t[i<<1|1]); } void update(int targ, int val, int i = 1, int l = 0, int r = N-1) { if(l == r) { t[i] = val; return; } int mid = l+r >> 1; if(targ <= mid) update(targ, val, i<<1, l, mid); else update(targ, val, i<<1|1, mid+1, r); t[i] = max(t[i<<1], t[i<<1|1]); } int get_root(int tl, int tr, int i = 1, int l = 0, int r = N-1) { if(l > tl || t[i] < tr) return -1; if(l == r) return ord[l]; int mid = l+r >> 1, ret = get_root(tl, tr, i<<1|1, mid+1, r); if(ret != -1) return ret; return get_root(tl, tr, i<<1, l, mid); } int main() { scanf("%d %d %d", &n, &m, &q); rep(i, 1, n) { scanf("%d %d", u+i, v+i); g[u[i]].push_back(v[i]); g[v[i]].push_back(u[i]); } dfs(1, 0); build(); fill(res+1, res+n+1, 1); rep(zz, 0, m) { int i; scanf("%d", &i); if(d[u[i]] > d[v[i]]) swap(u[i], v[i]); int root = get_root(s[u[i]], e[u[i]]); if(!state[i]) { res[root] += res[v[i]] - last[v[i]]; update(s[v[i]], -1); // no longer a candidate for being root of tree in forest } else { res[v[i]] = res[root]; last[v[i]] = res[root]; update(s[v[i]], e[v[i]]); // candidacy of being root restored } state[i] ^= 1; } rep(zz, 0, q) { int vert; scanf("%d", &vert); printf("%d\n", res[get_root(s[vert], e[vert])]); } }

Compilation message (stderr)

synchronization.cpp: In function 'void build(int, int, int)':
synchronization.cpp:24:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l+r >> 1;
             ^
synchronization.cpp: In function 'void update(int, int, int, int, int)':
synchronization.cpp:34:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l+r >> 1;
             ^
synchronization.cpp: In function 'int get_root(int, int, int, int, int)':
synchronization.cpp:42:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l+r >> 1, ret = get_root(tl, tr, i<<1|1, mid+1, r);
             ^
synchronization.cpp: In function 'int main()':
synchronization.cpp:47:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &q);
                               ^
synchronization.cpp:49:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", u+i, v+i);
                           ^
synchronization.cpp:57:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int i; scanf("%d", &i);
                         ^
synchronization.cpp:71:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int vert; scanf("%d", &vert);
                               ^
#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...