Submission #1084893

# Submission time Handle Problem Language Result Execution time Memory
1084893 2024-09-07T07:19:53 Z Sunbae Synchronization (JOI13_synchronization) C++17
0 / 100
151 ms 23864 KB
#include <bits/stdc++.h>
#define z exit(0)
#define mp make_pair
#define F first
#define S second
using namespace std;
using pii = pair<int,int>;
const int N = 1e5 + 5;
pii A[N], e[N];
vector<int> g[N];
int tin[N], tout[N], timer = 1, up[N][18], LOG, bit[N<<1];
bool active[N];
void upd(int i, int val){ for(; i<timer; 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 qry_u = qry(tin[u]), v = u;
	for(int j = LOG-1; j>=0; --j) if(qry(tin[up[u][j]]) == qry_u) v = up[u][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;
		e[i] = mp(u, v);
		g[u].emplace_back(v); g[v].emplace_back(u);
	}
	fill(A, A+n, mp(1, 0));
	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);
	}
	for(int i = 0, j; i<m; ++i){
		scanf("%d", &j); --j;
		int u = e[j].F, v = e[j].S;
		if(tin[u] > tin[v]) swap(u, v);
		if(active[j]){
			int root = find_root(u);
			A[v] = mp(A[root].F, A[root].F);
			upd(tin[v], +1); upd(tout[v], -1);
		}else{
			int root = find_root(u);
			A[root].F += A[v].F - A[v].S;
			upd(tin[v], -1); upd(tout[v], +1);
		}
		
		active[j] ^= 1;
	}
	while(q--){
		int u; scanf("%d", &u); --u; printf("%d\n", A[find_root(u)].F);
	}
}

Compilation message

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:42:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |   scanf("%d", &j); --j;
      |   ~~~~~^~~~~~~~~~
synchronization.cpp:58:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   int u; scanf("%d", &u); --u; printf("%d\n", A[find_root(u)].F);
      |          ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2804 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 66 ms 20564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 151 ms 23864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 1 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -