Submission #65655

# Submission time Handle Problem Language Result Execution time Memory
65655 2018-08-08T11:03:24 Z sebinkim None (KOI18_family) C++14
100 / 100
789 ms 129812 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

vector <int> V1[303030], V2[303030];
int L1[303030], L2[303030], R1[303030], R2[303030];
int P1[303030][20], P2[303030];
int I[303030];
multiset <int> S[303030];
int n, m, k, cnt1, cnt2, r1, r2;

void check(int x, int l, int r)
{
	int i, p = I[l], s = 0, f1, f2;
	
	for(i=19; i>=0; i--){
		if(!(L1[P1[p][i]] <= l && r <= R1[P1[p][i]])) p = P1[p][i];
	}
	p = P1[p][0];
	
	for(int t: V1[p]){
		auto it = S[x].lower_bound(L1[t]);
		if(it != S[x].end() && L1[t] <= *it && *it <= R1[t]) s += R1[t] - L1[t] + 1;
	}
	
	if(s != R2[x] - L2[x] + 1){
		printf("NO\n");
		exit(0);
	}
}

void dfs1(int p)
{
	if(p <= k){
		cnt1 ++;
		L1[p] = R1[p] = cnt1;
		I[cnt1] = p;
		return;
	}
	L1[p] = cnt1 + 1;
	for(int t: V1[p]) dfs1(t);
	R1[p] = cnt1;
}

pii dfs2(int p)
{
	if(p <= k){
		cnt2 ++;
		L2[p] = R2[p] = cnt2;
		S[p].insert(L1[p]);
		return pii(L1[p], L1[p]);
	}
	
	pii ret = pii(1e9, -1e9), k;
	
	L2[p] = cnt2 + 1;
	
	for(int t: V2[p]){
		k = dfs2(t);
		if(S[p].size() < S[t].size()) swap(S[t], S[p]);
		for(auto &v: S[t]) S[p].insert(v);
		
		ret.first = min(ret.first, k.first);
		ret.second = max(ret.second, k.second);
	}
	
	R2[p] = cnt2;
	
	check(p, ret.first, ret.second);
	
	return ret;
}

int main()
{
	int i, j;
	
	scanf("%d%d%d", &n, &m, &k);
	
	for(i=1; i<=n; i++){
		scanf("%d", P1[i]);
		if(P1[i][0] == 0) r1 = i;
		else V1[P1[i][0]].push_back(i);
	}
	
	for(i=1; i<=19; i++){
		for(j=1; j<=n; j++){
			P1[j][i] = P1[P1[j][i-1]][i-1];
		}
	}
	
	for(i=1; i<=m; i++){
		scanf("%d", P2+i);
		if(P2[i] == 0) r2 = i;
		V2[P2[i]].push_back(i);
	}
	
	L1[0] = 1, R1[0] = k;
	dfs1(r1);
	
	L2[0] = 1, R2[0] = k;
	dfs2(r2);
	
	printf("YES\n");
	
	return 0;
}

Compilation message

family.cpp: In function 'void check(int, int, int)':
family.cpp:16:26: warning: unused variable 'f1' [-Wunused-variable]
  int i, p = I[l], s = 0, f1, f2;
                          ^~
family.cpp:16:30: warning: unused variable 'f2' [-Wunused-variable]
  int i, p = I[l], s = 0, f1, f2;
                              ^~
family.cpp: In function 'int main()':
family.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &m, &k);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
family.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", P1[i]);
   ~~~~~^~~~~~~~~~~~~
family.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", P2+i);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 28792 KB Output is correct
2 Correct 27 ms 28936 KB Output is correct
3 Correct 30 ms 28936 KB Output is correct
4 Correct 26 ms 28956 KB Output is correct
5 Correct 27 ms 28956 KB Output is correct
6 Correct 26 ms 29012 KB Output is correct
7 Correct 28 ms 29068 KB Output is correct
8 Correct 28 ms 29120 KB Output is correct
9 Correct 26 ms 29120 KB Output is correct
10 Correct 27 ms 29176 KB Output is correct
11 Correct 27 ms 29188 KB Output is correct
12 Correct 29 ms 29188 KB Output is correct
13 Correct 29 ms 29188 KB Output is correct
14 Correct 28 ms 29216 KB Output is correct
15 Correct 26 ms 29260 KB Output is correct
16 Correct 28 ms 29264 KB Output is correct
17 Correct 26 ms 29264 KB Output is correct
18 Correct 26 ms 29264 KB Output is correct
19 Correct 27 ms 29264 KB Output is correct
20 Correct 30 ms 29280 KB Output is correct
21 Correct 32 ms 29280 KB Output is correct
22 Correct 27 ms 29280 KB Output is correct
23 Correct 27 ms 29280 KB Output is correct
24 Correct 28 ms 29296 KB Output is correct
25 Correct 27 ms 29296 KB Output is correct
26 Correct 27 ms 29360 KB Output is correct
27 Correct 26 ms 29360 KB Output is correct
28 Correct 26 ms 29360 KB Output is correct
29 Correct 26 ms 29360 KB Output is correct
30 Correct 26 ms 29360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 28792 KB Output is correct
2 Correct 27 ms 28936 KB Output is correct
3 Correct 30 ms 28936 KB Output is correct
4 Correct 26 ms 28956 KB Output is correct
5 Correct 27 ms 28956 KB Output is correct
6 Correct 26 ms 29012 KB Output is correct
7 Correct 28 ms 29068 KB Output is correct
8 Correct 28 ms 29120 KB Output is correct
9 Correct 26 ms 29120 KB Output is correct
10 Correct 27 ms 29176 KB Output is correct
11 Correct 27 ms 29188 KB Output is correct
12 Correct 29 ms 29188 KB Output is correct
13 Correct 29 ms 29188 KB Output is correct
14 Correct 28 ms 29216 KB Output is correct
15 Correct 26 ms 29260 KB Output is correct
16 Correct 28 ms 29264 KB Output is correct
17 Correct 26 ms 29264 KB Output is correct
18 Correct 26 ms 29264 KB Output is correct
19 Correct 27 ms 29264 KB Output is correct
20 Correct 30 ms 29280 KB Output is correct
21 Correct 32 ms 29280 KB Output is correct
22 Correct 27 ms 29280 KB Output is correct
23 Correct 27 ms 29280 KB Output is correct
24 Correct 28 ms 29296 KB Output is correct
25 Correct 27 ms 29296 KB Output is correct
26 Correct 27 ms 29360 KB Output is correct
27 Correct 26 ms 29360 KB Output is correct
28 Correct 26 ms 29360 KB Output is correct
29 Correct 26 ms 29360 KB Output is correct
30 Correct 26 ms 29360 KB Output is correct
31 Correct 32 ms 29360 KB Output is correct
32 Correct 31 ms 29360 KB Output is correct
33 Correct 31 ms 29360 KB Output is correct
34 Correct 31 ms 29360 KB Output is correct
35 Correct 30 ms 29360 KB Output is correct
36 Correct 27 ms 29424 KB Output is correct
37 Correct 27 ms 29424 KB Output is correct
38 Correct 26 ms 29428 KB Output is correct
39 Correct 31 ms 29432 KB Output is correct
40 Correct 26 ms 29548 KB Output is correct
41 Correct 28 ms 29548 KB Output is correct
42 Correct 31 ms 29548 KB Output is correct
43 Correct 26 ms 29548 KB Output is correct
44 Correct 26 ms 29548 KB Output is correct
45 Correct 27 ms 29548 KB Output is correct
46 Correct 26 ms 29548 KB Output is correct
47 Correct 25 ms 29548 KB Output is correct
48 Correct 26 ms 29548 KB Output is correct
49 Correct 26 ms 29548 KB Output is correct
50 Correct 29 ms 29548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 28792 KB Output is correct
2 Correct 27 ms 28936 KB Output is correct
3 Correct 30 ms 28936 KB Output is correct
4 Correct 26 ms 28956 KB Output is correct
5 Correct 27 ms 28956 KB Output is correct
6 Correct 26 ms 29012 KB Output is correct
7 Correct 28 ms 29068 KB Output is correct
8 Correct 28 ms 29120 KB Output is correct
9 Correct 26 ms 29120 KB Output is correct
10 Correct 27 ms 29176 KB Output is correct
11 Correct 27 ms 29188 KB Output is correct
12 Correct 29 ms 29188 KB Output is correct
13 Correct 29 ms 29188 KB Output is correct
14 Correct 28 ms 29216 KB Output is correct
15 Correct 26 ms 29260 KB Output is correct
16 Correct 28 ms 29264 KB Output is correct
17 Correct 26 ms 29264 KB Output is correct
18 Correct 26 ms 29264 KB Output is correct
19 Correct 27 ms 29264 KB Output is correct
20 Correct 30 ms 29280 KB Output is correct
21 Correct 32 ms 29280 KB Output is correct
22 Correct 27 ms 29280 KB Output is correct
23 Correct 27 ms 29280 KB Output is correct
24 Correct 28 ms 29296 KB Output is correct
25 Correct 27 ms 29296 KB Output is correct
26 Correct 27 ms 29360 KB Output is correct
27 Correct 26 ms 29360 KB Output is correct
28 Correct 26 ms 29360 KB Output is correct
29 Correct 26 ms 29360 KB Output is correct
30 Correct 26 ms 29360 KB Output is correct
31 Correct 32 ms 29360 KB Output is correct
32 Correct 31 ms 29360 KB Output is correct
33 Correct 31 ms 29360 KB Output is correct
34 Correct 31 ms 29360 KB Output is correct
35 Correct 30 ms 29360 KB Output is correct
36 Correct 27 ms 29424 KB Output is correct
37 Correct 27 ms 29424 KB Output is correct
38 Correct 26 ms 29428 KB Output is correct
39 Correct 31 ms 29432 KB Output is correct
40 Correct 26 ms 29548 KB Output is correct
41 Correct 28 ms 29548 KB Output is correct
42 Correct 31 ms 29548 KB Output is correct
43 Correct 26 ms 29548 KB Output is correct
44 Correct 26 ms 29548 KB Output is correct
45 Correct 27 ms 29548 KB Output is correct
46 Correct 26 ms 29548 KB Output is correct
47 Correct 25 ms 29548 KB Output is correct
48 Correct 26 ms 29548 KB Output is correct
49 Correct 26 ms 29548 KB Output is correct
50 Correct 29 ms 29548 KB Output is correct
51 Correct 38 ms 31016 KB Output is correct
52 Correct 39 ms 31016 KB Output is correct
53 Correct 33 ms 31016 KB Output is correct
54 Correct 36 ms 31016 KB Output is correct
55 Correct 34 ms 31112 KB Output is correct
56 Correct 32 ms 31224 KB Output is correct
57 Correct 40 ms 31224 KB Output is correct
58 Correct 32 ms 31224 KB Output is correct
59 Correct 34 ms 31464 KB Output is correct
60 Correct 31 ms 31464 KB Output is correct
61 Correct 29 ms 31464 KB Output is correct
62 Correct 30 ms 31464 KB Output is correct
63 Correct 51 ms 31464 KB Output is correct
64 Correct 28 ms 31464 KB Output is correct
65 Correct 27 ms 31464 KB Output is correct
66 Correct 31 ms 31464 KB Output is correct
67 Correct 35 ms 31464 KB Output is correct
68 Correct 36 ms 31464 KB Output is correct
69 Correct 30 ms 31464 KB Output is correct
70 Correct 29 ms 31464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 28792 KB Output is correct
2 Correct 27 ms 28936 KB Output is correct
3 Correct 30 ms 28936 KB Output is correct
4 Correct 26 ms 28956 KB Output is correct
5 Correct 27 ms 28956 KB Output is correct
6 Correct 26 ms 29012 KB Output is correct
7 Correct 28 ms 29068 KB Output is correct
8 Correct 28 ms 29120 KB Output is correct
9 Correct 26 ms 29120 KB Output is correct
10 Correct 27 ms 29176 KB Output is correct
11 Correct 27 ms 29188 KB Output is correct
12 Correct 29 ms 29188 KB Output is correct
13 Correct 29 ms 29188 KB Output is correct
14 Correct 28 ms 29216 KB Output is correct
15 Correct 26 ms 29260 KB Output is correct
16 Correct 28 ms 29264 KB Output is correct
17 Correct 26 ms 29264 KB Output is correct
18 Correct 26 ms 29264 KB Output is correct
19 Correct 27 ms 29264 KB Output is correct
20 Correct 30 ms 29280 KB Output is correct
21 Correct 32 ms 29280 KB Output is correct
22 Correct 27 ms 29280 KB Output is correct
23 Correct 27 ms 29280 KB Output is correct
24 Correct 28 ms 29296 KB Output is correct
25 Correct 27 ms 29296 KB Output is correct
26 Correct 27 ms 29360 KB Output is correct
27 Correct 26 ms 29360 KB Output is correct
28 Correct 26 ms 29360 KB Output is correct
29 Correct 26 ms 29360 KB Output is correct
30 Correct 26 ms 29360 KB Output is correct
31 Correct 32 ms 29360 KB Output is correct
32 Correct 31 ms 29360 KB Output is correct
33 Correct 31 ms 29360 KB Output is correct
34 Correct 31 ms 29360 KB Output is correct
35 Correct 30 ms 29360 KB Output is correct
36 Correct 27 ms 29424 KB Output is correct
37 Correct 27 ms 29424 KB Output is correct
38 Correct 26 ms 29428 KB Output is correct
39 Correct 31 ms 29432 KB Output is correct
40 Correct 26 ms 29548 KB Output is correct
41 Correct 28 ms 29548 KB Output is correct
42 Correct 31 ms 29548 KB Output is correct
43 Correct 26 ms 29548 KB Output is correct
44 Correct 26 ms 29548 KB Output is correct
45 Correct 27 ms 29548 KB Output is correct
46 Correct 26 ms 29548 KB Output is correct
47 Correct 25 ms 29548 KB Output is correct
48 Correct 26 ms 29548 KB Output is correct
49 Correct 26 ms 29548 KB Output is correct
50 Correct 29 ms 29548 KB Output is correct
51 Correct 38 ms 31016 KB Output is correct
52 Correct 39 ms 31016 KB Output is correct
53 Correct 33 ms 31016 KB Output is correct
54 Correct 36 ms 31016 KB Output is correct
55 Correct 34 ms 31112 KB Output is correct
56 Correct 32 ms 31224 KB Output is correct
57 Correct 40 ms 31224 KB Output is correct
58 Correct 32 ms 31224 KB Output is correct
59 Correct 34 ms 31464 KB Output is correct
60 Correct 31 ms 31464 KB Output is correct
61 Correct 29 ms 31464 KB Output is correct
62 Correct 30 ms 31464 KB Output is correct
63 Correct 51 ms 31464 KB Output is correct
64 Correct 28 ms 31464 KB Output is correct
65 Correct 27 ms 31464 KB Output is correct
66 Correct 31 ms 31464 KB Output is correct
67 Correct 35 ms 31464 KB Output is correct
68 Correct 36 ms 31464 KB Output is correct
69 Correct 30 ms 31464 KB Output is correct
70 Correct 29 ms 31464 KB Output is correct
71 Correct 103 ms 39708 KB Output is correct
72 Correct 113 ms 44440 KB Output is correct
73 Correct 83 ms 44440 KB Output is correct
74 Correct 104 ms 44440 KB Output is correct
75 Correct 107 ms 44440 KB Output is correct
76 Correct 764 ms 104132 KB Output is correct
77 Correct 715 ms 104132 KB Output is correct
78 Correct 789 ms 129812 KB Output is correct
79 Correct 643 ms 129812 KB Output is correct
80 Correct 598 ms 129812 KB Output is correct
81 Correct 85 ms 129812 KB Output is correct
82 Correct 75 ms 129812 KB Output is correct
83 Correct 78 ms 129812 KB Output is correct
84 Correct 119 ms 129812 KB Output is correct
85 Correct 102 ms 129812 KB Output is correct
86 Correct 578 ms 129812 KB Output is correct
87 Correct 575 ms 129812 KB Output is correct
88 Correct 451 ms 129812 KB Output is correct
89 Correct 505 ms 129812 KB Output is correct
90 Correct 550 ms 129812 KB Output is correct