Submission #1084900

#TimeUsernameProblemLanguageResultExecution timeMemory
1084900SunbaeSynchronization (JOI13_synchronization)C++17
100 / 100
213 ms23388 KiB
#include <bits/stdc++.h> #define mp make_pair #define F first #define S second using namespace std; using pii = pair<int,int>; const int N = 1e5 + 5; vector<int> g[N]; pii e[N], A[N]; int timer, tin[N], tout[N], up[N][17], bit[N<<1], LOG; bool active[N]; void upd(int i, int val){ for(; i<(N<<1); i+=i&-i) bit[i] += val;} int qry(int i){ int res = 0; for(; i; i-=i&-i) res += bit[i]; return res;} void dfs(int u, int p){ up[u][0] = p; tin[u] = timer++; for(int j = 1; j<LOG; ++j) up[u][j] = up[up[u][j-1]][j-1]; for(int v : g[u]) if(v != p) dfs(v, u); tout[u] = timer++; } int find_root(int u){ int v = u, qry_u = qry(tin[u]); for(int j = LOG-1; j>=0; --j) if(qry(tin[up[v][j]]) == qry_u) v = up[v][j]; return v; } signed main(){ int n, m, q; scanf("%d %d %d", &n, &m, &q); LOG = 32 - __builtin_clz(n); for(int i = 0, u, v; i<n-1; ++i){ scanf("%d %d", &u, &v); --u; --v; g[u].push_back(v); g[v].push_back(u); e[i] = mp(u, v); } timer = 1; dfs(0, 0); for(int i= 0; i<n-1; ++i){ int u = e[i].F, v = e[i].S; if(tin[u] > tin[v]) swap(u, v); upd(tin[v], +1); upd(tout[v], -1); } fill(A, A+n, mp(1, 0)); for(int i = 0; i<m; ++i){ int j; scanf("%d", &j); --j; int u = e[j].F, v = e[j].S; if(tin[u] > tin[v]) swap(u, v); if(active[j]){ //remove A[v] = mp(A[find_root(u)].F, A[find_root(u)].F); upd(tin[v], +1); upd(tout[v], -1); }else{ //add A[find_root(u)].F += A[v].F - A[v].S; upd(tin[v], -1); upd(tout[v], +1); } active[j] ^= 1; } for(int u; q--; ) scanf("%d", &u), printf("%d\n", A[find_root(--u)].F); }

Compilation message (stderr)

synchronization.cpp: In function 'int main()':
synchronization.cpp:28:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |  int n, m, q; scanf("%d %d %d", &n, &m, &q); LOG = 32 - __builtin_clz(n);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |   scanf("%d %d", &u, &v); --u; --v;
      |   ~~~~~^~~~~~~~~~~~~~~~~
synchronization.cpp:41:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   int j; scanf("%d", &j); --j;
      |          ~~~~~^~~~~~~~~~
synchronization.cpp:52:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |  for(int u; q--; ) scanf("%d", &u), printf("%d\n", A[find_root(--u)].F);
      |                    ~~~~~^~~~~~~~~~
#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...