답안 #43358

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
43358 2018-03-14T14:54:25 Z szawinis 동기화 (JOI13_synchronization) C++14
100 / 100
279 ms 20348 KB
#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

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);
                               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 8568 KB Output is correct
2 Correct 10 ms 8672 KB Output is correct
3 Correct 11 ms 8672 KB Output is correct
4 Correct 10 ms 8760 KB Output is correct
5 Correct 10 ms 8760 KB Output is correct
6 Correct 13 ms 8800 KB Output is correct
7 Correct 26 ms 9332 KB Output is correct
8 Correct 27 ms 9332 KB Output is correct
9 Correct 25 ms 9336 KB Output is correct
10 Correct 246 ms 15292 KB Output is correct
11 Correct 263 ms 15300 KB Output is correct
12 Correct 245 ms 19912 KB Output is correct
13 Correct 161 ms 19912 KB Output is correct
14 Correct 153 ms 19912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 19912 KB Output is correct
2 Correct 123 ms 19912 KB Output is correct
3 Correct 110 ms 19912 KB Output is correct
4 Correct 99 ms 19912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19912 KB Output is correct
2 Correct 10 ms 19912 KB Output is correct
3 Correct 10 ms 19912 KB Output is correct
4 Correct 11 ms 19912 KB Output is correct
5 Correct 10 ms 19912 KB Output is correct
6 Correct 14 ms 19912 KB Output is correct
7 Correct 30 ms 19912 KB Output is correct
8 Correct 229 ms 20104 KB Output is correct
9 Correct 233 ms 20104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 20200 KB Output is correct
2 Correct 130 ms 20232 KB Output is correct
3 Correct 138 ms 20232 KB Output is correct
4 Correct 136 ms 20232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 20232 KB Output is correct
2 Correct 14 ms 20232 KB Output is correct
3 Correct 10 ms 20232 KB Output is correct
4 Correct 9 ms 20232 KB Output is correct
5 Correct 11 ms 20232 KB Output is correct
6 Correct 29 ms 20232 KB Output is correct
7 Correct 279 ms 20232 KB Output is correct
8 Correct 220 ms 20232 KB Output is correct
9 Correct 202 ms 20232 KB Output is correct
10 Correct 194 ms 20232 KB Output is correct
11 Correct 151 ms 20232 KB Output is correct
12 Correct 163 ms 20232 KB Output is correct
13 Correct 129 ms 20348 KB Output is correct