Submission #30584

# Submission time Handle Problem Language Result Execution time Memory
30584 2017-07-25T07:01:34 Z kajebiii None (KOI16_tree) C++14
100 / 100
959 ms 42832 KB
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<double, double> pd;
typedef pair<int, int> pi; typedef pair<ll, int> pli;
typedef pair<ll, ll> pll; typedef pair<ll, pi> plp;
typedef tuple<int, int, int> ti; typedef tuple<ll, int, int> tli;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = 2e5 + 100, MAX_Q = 2e5 + 100, LOG_N = 21;

int N, Q, iP[MAX_N];
vector<int> Ed[MAX_N];
int P[LOG_N][MAX_N], D[MAX_N], S[MAX_N];
int predfs(int v, int p) {
	D[v] = D[p] + 1; S[v] = 1; P[0][v] = p;
	for(int w : Ed[v]) if(w != p) S[v] += predfs(w, v);
	return S[v];
}

struct BIT {
	int val[MAX_N];
	BIT() {memset(val, 0, sizeof(val));}
	void update(int v, int k) {for(v+=10; v<MAX_N; v+=v&-v) val[v]+=k;}
	int getSum(int v) {int res=0; for(v+=10; v; v-=v&-v) res+=val[v]; return res;}
	int getSum(int a, int b) {return getSum(b) - getSum(a-1);}
}bit;
int eN = 0;
int eIx[MAX_N], fIx[MAX_N];
void makedfs(int v, int p, int h) {
//	printf("%d %d %d\n", v, p, h);
	if(v != N/2) bit.update(eN, 1), eIx[v] = eN++; fIx[v] = h;
	int ix = -1;
	for(int w : Ed[v]) if(w != p && (ix == -1 || S[ix] < S[w])) ix = w;
	if(ix == -1) return;
	makedfs(ix, v, h);
	for(int w : Ed[v]) if(w != p && w != ix) makedfs(w, v, w);
}
int getL(int a, int b) {
	if(D[a] > D[b]) swap(a, b);
	for(int p=LOG_N-1; p>=0; p--) if((D[b]-D[a])&(1<<p)) b=P[p][b];
	if(a == b) return a;
	for(int p=LOG_N-1; p>=0; p--) if(P[p][a]!=P[p][b]) a=P[p][a], b=P[p][b];
	return P[0][a];
}
bool isCan(int fr, int to) {
	int hd = fIx[fr];
	if(D[to] < D[hd]) {
		int val = bit.getSum(eIx[hd], eIx[fr]);
		if(val != D[fr] - D[hd] + 1) return false;
		return isCan(P[0][hd], to);
	} else {
		int val = bit.getSum(eIx[fr] - (D[fr] - D[to]) + 1, eIx[fr]);
		return val == (D[fr] - D[to]);
	}
	printf("what?\n");
	return false;
}
int main() {
	cin >> N >> Q;
	for(int i=2; i<=N; i++) scanf("%d", &iP[i]);
	for(int i=2; i<=N; i++) Ed[i].push_back(iP[i]), Ed[iP[i]].push_back(i);
	predfs(N/2, 0);
	for(int p=1; p<LOG_N; p++) for(int i=1; i<=N; i++) P[p][i] = P[p-1][P[p-1][i]];
	makedfs(N/2, 0, N/2);
	for(int q=1; q<=Q; q++) {
		int b, c, d; scanf("%d%d%d", &b, &c, &d);
		int l = getL(b, c);
//		printf("[%d %d -> %d]\n", b, c, l);
//		printf("b %d | c %d\n", isCan(b, l), isCan(c, l));
		bool ans = (isCan(b, l) & isCan(c, l));
		puts(ans?"YES":"NO");
		if(d == 1) {
			int x = b, y = iP[b];
			if(!ans) x = c, y = iP[c];
			if(x == 1) continue;
			if(D[x] > D[y]) swap(x, y);
			if(bit.getSum(eIx[y], eIx[y]) == 0) continue;
			bit.update(eIx[y], -1);
		}
	}
	return 0;
}

Compilation message

tree.cpp: In function 'int main()':
tree.cpp:69:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=2; i<=N; i++) scanf("%d", &iP[i]);
                                             ^
tree.cpp:75:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int b, c, d; scanf("%d%d%d", &b, &c, &d);
                                           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 27812 KB Output is correct
2 Correct 3 ms 27812 KB Output is correct
3 Correct 3 ms 27812 KB Output is correct
4 Correct 0 ms 27812 KB Output is correct
5 Correct 3 ms 27812 KB Output is correct
6 Correct 0 ms 27812 KB Output is correct
7 Correct 3 ms 27812 KB Output is correct
8 Correct 3 ms 27812 KB Output is correct
9 Correct 0 ms 27812 KB Output is correct
10 Correct 0 ms 27812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 27812 KB Output is correct
2 Correct 3 ms 27812 KB Output is correct
3 Correct 3 ms 27812 KB Output is correct
4 Correct 0 ms 27812 KB Output is correct
5 Correct 3 ms 27812 KB Output is correct
6 Correct 0 ms 27812 KB Output is correct
7 Correct 3 ms 27812 KB Output is correct
8 Correct 3 ms 27812 KB Output is correct
9 Correct 0 ms 27812 KB Output is correct
10 Correct 0 ms 27812 KB Output is correct
11 Correct 0 ms 27812 KB Output is correct
12 Correct 0 ms 27812 KB Output is correct
13 Correct 3 ms 27812 KB Output is correct
14 Correct 0 ms 27812 KB Output is correct
15 Correct 3 ms 27812 KB Output is correct
16 Correct 0 ms 27812 KB Output is correct
17 Correct 3 ms 27812 KB Output is correct
18 Correct 3 ms 27812 KB Output is correct
19 Correct 0 ms 27812 KB Output is correct
20 Correct 0 ms 27812 KB Output is correct
21 Correct 0 ms 27812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 27812 KB Output is correct
2 Correct 3 ms 27812 KB Output is correct
3 Correct 3 ms 27812 KB Output is correct
4 Correct 0 ms 27812 KB Output is correct
5 Correct 3 ms 27812 KB Output is correct
6 Correct 0 ms 27812 KB Output is correct
7 Correct 3 ms 27812 KB Output is correct
8 Correct 3 ms 27812 KB Output is correct
9 Correct 0 ms 27812 KB Output is correct
10 Correct 0 ms 27812 KB Output is correct
11 Correct 0 ms 27812 KB Output is correct
12 Correct 0 ms 27812 KB Output is correct
13 Correct 3 ms 27812 KB Output is correct
14 Correct 0 ms 27812 KB Output is correct
15 Correct 3 ms 27812 KB Output is correct
16 Correct 0 ms 27812 KB Output is correct
17 Correct 3 ms 27812 KB Output is correct
18 Correct 3 ms 27812 KB Output is correct
19 Correct 0 ms 27812 KB Output is correct
20 Correct 0 ms 27812 KB Output is correct
21 Correct 0 ms 27812 KB Output is correct
22 Correct 156 ms 27944 KB Output is correct
23 Correct 166 ms 27944 KB Output is correct
24 Correct 139 ms 27944 KB Output is correct
25 Correct 146 ms 27944 KB Output is correct
26 Correct 183 ms 27944 KB Output is correct
27 Correct 129 ms 27944 KB Output is correct
28 Correct 176 ms 27944 KB Output is correct
29 Correct 143 ms 27944 KB Output is correct
30 Correct 213 ms 27944 KB Output is correct
31 Correct 159 ms 27944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 27812 KB Output is correct
2 Correct 3 ms 27812 KB Output is correct
3 Correct 3 ms 27812 KB Output is correct
4 Correct 0 ms 27812 KB Output is correct
5 Correct 3 ms 27812 KB Output is correct
6 Correct 0 ms 27812 KB Output is correct
7 Correct 3 ms 27812 KB Output is correct
8 Correct 3 ms 27812 KB Output is correct
9 Correct 0 ms 27812 KB Output is correct
10 Correct 0 ms 27812 KB Output is correct
11 Correct 939 ms 40540 KB Output is correct
12 Correct 909 ms 42212 KB Output is correct
13 Correct 916 ms 42148 KB Output is correct
14 Correct 959 ms 38992 KB Output is correct
15 Correct 763 ms 41812 KB Output is correct
16 Correct 723 ms 42220 KB Output is correct
17 Correct 426 ms 39800 KB Output is correct
18 Correct 376 ms 40360 KB Output is correct
19 Correct 529 ms 42832 KB Output is correct
20 Correct 446 ms 42204 KB Output is correct
21 Correct 603 ms 39192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 27812 KB Output is correct
2 Correct 3 ms 27812 KB Output is correct
3 Correct 3 ms 27812 KB Output is correct
4 Correct 0 ms 27812 KB Output is correct
5 Correct 3 ms 27812 KB Output is correct
6 Correct 0 ms 27812 KB Output is correct
7 Correct 3 ms 27812 KB Output is correct
8 Correct 3 ms 27812 KB Output is correct
9 Correct 0 ms 27812 KB Output is correct
10 Correct 0 ms 27812 KB Output is correct
11 Correct 0 ms 27812 KB Output is correct
12 Correct 0 ms 27812 KB Output is correct
13 Correct 3 ms 27812 KB Output is correct
14 Correct 0 ms 27812 KB Output is correct
15 Correct 3 ms 27812 KB Output is correct
16 Correct 0 ms 27812 KB Output is correct
17 Correct 3 ms 27812 KB Output is correct
18 Correct 3 ms 27812 KB Output is correct
19 Correct 0 ms 27812 KB Output is correct
20 Correct 0 ms 27812 KB Output is correct
21 Correct 0 ms 27812 KB Output is correct
22 Correct 156 ms 27944 KB Output is correct
23 Correct 166 ms 27944 KB Output is correct
24 Correct 139 ms 27944 KB Output is correct
25 Correct 146 ms 27944 KB Output is correct
26 Correct 183 ms 27944 KB Output is correct
27 Correct 129 ms 27944 KB Output is correct
28 Correct 176 ms 27944 KB Output is correct
29 Correct 143 ms 27944 KB Output is correct
30 Correct 213 ms 27944 KB Output is correct
31 Correct 159 ms 27944 KB Output is correct
32 Correct 939 ms 40540 KB Output is correct
33 Correct 909 ms 42212 KB Output is correct
34 Correct 916 ms 42148 KB Output is correct
35 Correct 959 ms 38992 KB Output is correct
36 Correct 763 ms 41812 KB Output is correct
37 Correct 723 ms 42220 KB Output is correct
38 Correct 426 ms 39800 KB Output is correct
39 Correct 376 ms 40360 KB Output is correct
40 Correct 529 ms 42832 KB Output is correct
41 Correct 446 ms 42204 KB Output is correct
42 Correct 603 ms 39192 KB Output is correct
43 Correct 596 ms 34808 KB Output is correct
44 Correct 446 ms 34808 KB Output is correct
45 Correct 523 ms 34808 KB Output is correct
46 Correct 619 ms 34808 KB Output is correct
47 Correct 609 ms 34808 KB Output is correct
48 Correct 276 ms 35076 KB Output is correct
49 Correct 353 ms 35408 KB Output is correct
50 Correct 273 ms 35348 KB Output is correct
51 Correct 479 ms 37312 KB Output is correct