답안 #65660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
65660 2018-08-08T11:07:30 Z imeimi2000 족보 (KOI18_family) C++17
100 / 100
783 ms 83844 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n, m, k;
vector<int> child1[300001];
vector<int> child2[300001];
int st[300001];
int ed[300001];
int rev[300001];
int par[300001][20];
int dep[300001];

void fail(int i) {
    printf("NO\n");
    exit(0);
}

void dfs(int x) {
    static int ord = 1;
    rev[st[x] = ord] = x;
    for (int i : child1[x]) {
        par[i][0] = x;
        dep[i] = dep[x] + 1;
        dfs(i);
    }
    if (child1[x].empty()) ++ord;
    ed[x] = ord - 1;
}

int lca(int x, int y) {
    if (dep[x] < dep[y]) swap(x, y);
    for (int i = 20; i--; ) {
        if ((dep[x] - dep[y]) >> i) x = par[x][i];
    }
    if (x == y) return x;
    for (int i = 20; i--; ) {
        if (par[x][i] != par[y][i]) {
            x = par[x][i];
            y = par[y][i];
        }
    }
    return par[x][0];
}

int anc(int x, int c) {
    for (int i = 20; i--; ) {
        if ((c >> i) & 1) x = par[x][i];
    }
    return x;
}

int segn[1 << 20];
int segx[1 << 20];

void update(int seg[], int i, int s, int e, int x, int v) {
    if (s == e) {
        seg[i] = v;
        return;
    }
    int m = (s + e) / 2;
    if (x <= m) update(seg, i << 1, s, m, x, v);
    else update(seg, i << 1 | 1, m + 1, e, x, v);
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}

const int inf = 1e7;
int query(int seg[], int i, int s, int e, int x, int y) {
    if (e < x || y < s) return -inf;
    if (x <= s && e <= y) return seg[i];
    int m = (s + e) / 2;
    return max(query(seg, i << 1, s, m, x, y), 
               query(seg, i << 1 | 1, m + 1, e, x, y));
}

int big[300001];
int s2[300001];
int e2[300001];
void dfs2(int x) {
    static int ord = 1;
    s2[x] = ord;
    if (child2[x].empty()) {
        big[x] = x;
        update(segx, 1, 1, k, ord, st[x]);
        update(segn, 1, 1, k, ord, k - st[x]);
        e2[x] = ord;
        ++ord;
        return;
    }
    int p = -1;
    for (int i : child2[x]) {
        dfs2(i);
        if (p != -1) p = lca(p, big[i]);
        else p = big[i];
    }
    e2[x] = ord - 1;
    vector<pii> as;
    for (int i : child2[x]) {
        if (p != big[i]) {
            int t = anc(big[i], dep[big[i]] - dep[p] - 1);
            as.emplace_back(t, i);
        }
    }
    sort(as.begin(), as.end());
    int mn = k, mx = 0, sum = 0;
    for (int it = 0; it < as.size(); ++it) {
        int t, y;
        tie(t, y) = as[it];
        if (it && as[it - 1].first != t) {
            int t = as[it - 1].first;
            if (mn != st[t]) fail(0);
            if (mx != ed[t]) fail(1);
            if (mx - mn + 1 != sum) fail(2);
            mn = k;
            mx = 0;
            sum = 0;
        }
        mn = min(mn, k - query(segn, 1, 1, k, s2[y], e2[y]));
        mx = max(mx, query(segx, 1, 1, k, s2[y], e2[y]));
        sum += e2[y] - s2[y] + 1;
    }
    
    if (as.size()) {
        int t = as[as.size() - 1].first;
        if (mn != st[t]) fail(0);
        if (mx != ed[t]) fail(1);
        if (mx - mn + 1 != sum) fail(2);
    }
    
    big[x] = p;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; ++i) {
        int p;
        cin >> p;
        child1[p].push_back(i);
	}
	for (int i = 1; i <= m; ++i) {
        int p;
        cin >> p;
        child2[p].push_back(i);
	}
	dfs(child1[0][0]);
	for (int i = 1; i < 20; ++i) {
        for (int j = 1; j <= n; ++j) {
            par[j][i] = par[par[j][i - 1]][i - 1];
        }
	}
	dfs2(child2[0][0]);
	printf("YES\n");
	return 0;
}

Compilation message

family.cpp: In function 'void dfs2(int)':
family.cpp:122:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int it = 0; it < as.size(); ++it) {
                      ~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14420 KB Output is correct
2 Correct 15 ms 14572 KB Output is correct
3 Correct 14 ms 14612 KB Output is correct
4 Correct 15 ms 14612 KB Output is correct
5 Correct 14 ms 14700 KB Output is correct
6 Correct 15 ms 14752 KB Output is correct
7 Correct 15 ms 14812 KB Output is correct
8 Correct 15 ms 14812 KB Output is correct
9 Correct 14 ms 14836 KB Output is correct
10 Correct 15 ms 14836 KB Output is correct
11 Correct 17 ms 14836 KB Output is correct
12 Correct 16 ms 14836 KB Output is correct
13 Correct 14 ms 14836 KB Output is correct
14 Correct 15 ms 14872 KB Output is correct
15 Correct 15 ms 14876 KB Output is correct
16 Correct 15 ms 14880 KB Output is correct
17 Correct 18 ms 14884 KB Output is correct
18 Correct 15 ms 14884 KB Output is correct
19 Correct 15 ms 14892 KB Output is correct
20 Correct 19 ms 14900 KB Output is correct
21 Correct 18 ms 14900 KB Output is correct
22 Correct 20 ms 14904 KB Output is correct
23 Correct 21 ms 14904 KB Output is correct
24 Correct 14 ms 14912 KB Output is correct
25 Correct 14 ms 14912 KB Output is correct
26 Correct 14 ms 14912 KB Output is correct
27 Correct 15 ms 14912 KB Output is correct
28 Correct 18 ms 14912 KB Output is correct
29 Correct 17 ms 14912 KB Output is correct
30 Correct 14 ms 14912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14420 KB Output is correct
2 Correct 15 ms 14572 KB Output is correct
3 Correct 14 ms 14612 KB Output is correct
4 Correct 15 ms 14612 KB Output is correct
5 Correct 14 ms 14700 KB Output is correct
6 Correct 15 ms 14752 KB Output is correct
7 Correct 15 ms 14812 KB Output is correct
8 Correct 15 ms 14812 KB Output is correct
9 Correct 14 ms 14836 KB Output is correct
10 Correct 15 ms 14836 KB Output is correct
11 Correct 17 ms 14836 KB Output is correct
12 Correct 16 ms 14836 KB Output is correct
13 Correct 14 ms 14836 KB Output is correct
14 Correct 15 ms 14872 KB Output is correct
15 Correct 15 ms 14876 KB Output is correct
16 Correct 15 ms 14880 KB Output is correct
17 Correct 18 ms 14884 KB Output is correct
18 Correct 15 ms 14884 KB Output is correct
19 Correct 15 ms 14892 KB Output is correct
20 Correct 19 ms 14900 KB Output is correct
21 Correct 18 ms 14900 KB Output is correct
22 Correct 20 ms 14904 KB Output is correct
23 Correct 21 ms 14904 KB Output is correct
24 Correct 14 ms 14912 KB Output is correct
25 Correct 14 ms 14912 KB Output is correct
26 Correct 14 ms 14912 KB Output is correct
27 Correct 15 ms 14912 KB Output is correct
28 Correct 18 ms 14912 KB Output is correct
29 Correct 17 ms 14912 KB Output is correct
30 Correct 14 ms 14912 KB Output is correct
31 Correct 14 ms 14912 KB Output is correct
32 Correct 13 ms 14912 KB Output is correct
33 Correct 15 ms 14912 KB Output is correct
34 Correct 14 ms 14912 KB Output is correct
35 Correct 14 ms 14912 KB Output is correct
36 Correct 20 ms 14960 KB Output is correct
37 Correct 14 ms 14964 KB Output is correct
38 Correct 14 ms 14968 KB Output is correct
39 Correct 15 ms 14972 KB Output is correct
40 Correct 18 ms 14976 KB Output is correct
41 Correct 15 ms 14984 KB Output is correct
42 Correct 14 ms 14984 KB Output is correct
43 Correct 13 ms 14984 KB Output is correct
44 Correct 15 ms 14984 KB Output is correct
45 Correct 13 ms 14984 KB Output is correct
46 Correct 15 ms 15000 KB Output is correct
47 Correct 15 ms 15004 KB Output is correct
48 Correct 17 ms 15008 KB Output is correct
49 Correct 14 ms 15012 KB Output is correct
50 Correct 17 ms 15016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14420 KB Output is correct
2 Correct 15 ms 14572 KB Output is correct
3 Correct 14 ms 14612 KB Output is correct
4 Correct 15 ms 14612 KB Output is correct
5 Correct 14 ms 14700 KB Output is correct
6 Correct 15 ms 14752 KB Output is correct
7 Correct 15 ms 14812 KB Output is correct
8 Correct 15 ms 14812 KB Output is correct
9 Correct 14 ms 14836 KB Output is correct
10 Correct 15 ms 14836 KB Output is correct
11 Correct 17 ms 14836 KB Output is correct
12 Correct 16 ms 14836 KB Output is correct
13 Correct 14 ms 14836 KB Output is correct
14 Correct 15 ms 14872 KB Output is correct
15 Correct 15 ms 14876 KB Output is correct
16 Correct 15 ms 14880 KB Output is correct
17 Correct 18 ms 14884 KB Output is correct
18 Correct 15 ms 14884 KB Output is correct
19 Correct 15 ms 14892 KB Output is correct
20 Correct 19 ms 14900 KB Output is correct
21 Correct 18 ms 14900 KB Output is correct
22 Correct 20 ms 14904 KB Output is correct
23 Correct 21 ms 14904 KB Output is correct
24 Correct 14 ms 14912 KB Output is correct
25 Correct 14 ms 14912 KB Output is correct
26 Correct 14 ms 14912 KB Output is correct
27 Correct 15 ms 14912 KB Output is correct
28 Correct 18 ms 14912 KB Output is correct
29 Correct 17 ms 14912 KB Output is correct
30 Correct 14 ms 14912 KB Output is correct
31 Correct 14 ms 14912 KB Output is correct
32 Correct 13 ms 14912 KB Output is correct
33 Correct 15 ms 14912 KB Output is correct
34 Correct 14 ms 14912 KB Output is correct
35 Correct 14 ms 14912 KB Output is correct
36 Correct 20 ms 14960 KB Output is correct
37 Correct 14 ms 14964 KB Output is correct
38 Correct 14 ms 14968 KB Output is correct
39 Correct 15 ms 14972 KB Output is correct
40 Correct 18 ms 14976 KB Output is correct
41 Correct 15 ms 14984 KB Output is correct
42 Correct 14 ms 14984 KB Output is correct
43 Correct 13 ms 14984 KB Output is correct
44 Correct 15 ms 14984 KB Output is correct
45 Correct 13 ms 14984 KB Output is correct
46 Correct 15 ms 15000 KB Output is correct
47 Correct 15 ms 15004 KB Output is correct
48 Correct 17 ms 15008 KB Output is correct
49 Correct 14 ms 15012 KB Output is correct
50 Correct 17 ms 15016 KB Output is correct
51 Correct 23 ms 15788 KB Output is correct
52 Correct 26 ms 15840 KB Output is correct
53 Correct 22 ms 15840 KB Output is correct
54 Correct 19 ms 15840 KB Output is correct
55 Correct 19 ms 15852 KB Output is correct
56 Correct 20 ms 15904 KB Output is correct
57 Correct 21 ms 15904 KB Output is correct
58 Correct 19 ms 15904 KB Output is correct
59 Correct 23 ms 15988 KB Output is correct
60 Correct 18 ms 15988 KB Output is correct
61 Correct 18 ms 16244 KB Output is correct
62 Correct 17 ms 16244 KB Output is correct
63 Correct 23 ms 16244 KB Output is correct
64 Correct 15 ms 16244 KB Output is correct
65 Correct 14 ms 16244 KB Output is correct
66 Correct 21 ms 16244 KB Output is correct
67 Correct 20 ms 16244 KB Output is correct
68 Correct 15 ms 16244 KB Output is correct
69 Correct 20 ms 16244 KB Output is correct
70 Correct 19 ms 16244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14420 KB Output is correct
2 Correct 15 ms 14572 KB Output is correct
3 Correct 14 ms 14612 KB Output is correct
4 Correct 15 ms 14612 KB Output is correct
5 Correct 14 ms 14700 KB Output is correct
6 Correct 15 ms 14752 KB Output is correct
7 Correct 15 ms 14812 KB Output is correct
8 Correct 15 ms 14812 KB Output is correct
9 Correct 14 ms 14836 KB Output is correct
10 Correct 15 ms 14836 KB Output is correct
11 Correct 17 ms 14836 KB Output is correct
12 Correct 16 ms 14836 KB Output is correct
13 Correct 14 ms 14836 KB Output is correct
14 Correct 15 ms 14872 KB Output is correct
15 Correct 15 ms 14876 KB Output is correct
16 Correct 15 ms 14880 KB Output is correct
17 Correct 18 ms 14884 KB Output is correct
18 Correct 15 ms 14884 KB Output is correct
19 Correct 15 ms 14892 KB Output is correct
20 Correct 19 ms 14900 KB Output is correct
21 Correct 18 ms 14900 KB Output is correct
22 Correct 20 ms 14904 KB Output is correct
23 Correct 21 ms 14904 KB Output is correct
24 Correct 14 ms 14912 KB Output is correct
25 Correct 14 ms 14912 KB Output is correct
26 Correct 14 ms 14912 KB Output is correct
27 Correct 15 ms 14912 KB Output is correct
28 Correct 18 ms 14912 KB Output is correct
29 Correct 17 ms 14912 KB Output is correct
30 Correct 14 ms 14912 KB Output is correct
31 Correct 14 ms 14912 KB Output is correct
32 Correct 13 ms 14912 KB Output is correct
33 Correct 15 ms 14912 KB Output is correct
34 Correct 14 ms 14912 KB Output is correct
35 Correct 14 ms 14912 KB Output is correct
36 Correct 20 ms 14960 KB Output is correct
37 Correct 14 ms 14964 KB Output is correct
38 Correct 14 ms 14968 KB Output is correct
39 Correct 15 ms 14972 KB Output is correct
40 Correct 18 ms 14976 KB Output is correct
41 Correct 15 ms 14984 KB Output is correct
42 Correct 14 ms 14984 KB Output is correct
43 Correct 13 ms 14984 KB Output is correct
44 Correct 15 ms 14984 KB Output is correct
45 Correct 13 ms 14984 KB Output is correct
46 Correct 15 ms 15000 KB Output is correct
47 Correct 15 ms 15004 KB Output is correct
48 Correct 17 ms 15008 KB Output is correct
49 Correct 14 ms 15012 KB Output is correct
50 Correct 17 ms 15016 KB Output is correct
51 Correct 23 ms 15788 KB Output is correct
52 Correct 26 ms 15840 KB Output is correct
53 Correct 22 ms 15840 KB Output is correct
54 Correct 19 ms 15840 KB Output is correct
55 Correct 19 ms 15852 KB Output is correct
56 Correct 20 ms 15904 KB Output is correct
57 Correct 21 ms 15904 KB Output is correct
58 Correct 19 ms 15904 KB Output is correct
59 Correct 23 ms 15988 KB Output is correct
60 Correct 18 ms 15988 KB Output is correct
61 Correct 18 ms 16244 KB Output is correct
62 Correct 17 ms 16244 KB Output is correct
63 Correct 23 ms 16244 KB Output is correct
64 Correct 15 ms 16244 KB Output is correct
65 Correct 14 ms 16244 KB Output is correct
66 Correct 21 ms 16244 KB Output is correct
67 Correct 20 ms 16244 KB Output is correct
68 Correct 15 ms 16244 KB Output is correct
69 Correct 20 ms 16244 KB Output is correct
70 Correct 19 ms 16244 KB Output is correct
71 Correct 91 ms 22408 KB Output is correct
72 Correct 120 ms 22408 KB Output is correct
73 Correct 54 ms 22408 KB Output is correct
74 Correct 94 ms 25988 KB Output is correct
75 Correct 95 ms 25988 KB Output is correct
76 Correct 750 ms 83844 KB Output is correct
77 Correct 599 ms 83844 KB Output is correct
78 Correct 783 ms 83844 KB Output is correct
79 Correct 565 ms 83844 KB Output is correct
80 Correct 600 ms 83844 KB Output is correct
81 Correct 71 ms 83844 KB Output is correct
82 Correct 62 ms 83844 KB Output is correct
83 Correct 45 ms 83844 KB Output is correct
84 Correct 77 ms 83844 KB Output is correct
85 Correct 62 ms 83844 KB Output is correct
86 Correct 553 ms 83844 KB Output is correct
87 Correct 517 ms 83844 KB Output is correct
88 Correct 416 ms 83844 KB Output is correct
89 Correct 387 ms 83844 KB Output is correct
90 Correct 427 ms 83844 KB Output is correct