답안 #203422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203422 2020-02-20T15:14:30 Z karma Cat in a tree (BOI17_catinatree) C++14
51 / 100
1000 ms 25444 KB
#include<bits/stdc++.h>
#define ll            long long
#define pb            emplace_back
#define fi            first
#define se            second
#define mp            make_pair
//#define int           int64_t

using namespace std;

typedef pair<int, int> pii;
const int N = (int)2e5 + 10;

int dep[N], n, d, p[N], ans = 0;
int in[N], out[N], T, u, cur, par, rem;
vector<int> adj[N];
vector<pii> v;

struct TSegment {
    vector<int> l, h, st, lz;
    int n, inf;
    void init(int _n, int d) {
        n = ++_n; inf = 2 * d; st.resize(n << 2, inf);
        l.resize(n << 2), h.resize(n << 2), lz.resize(n << 2, inf);
        --n; build(1, 1, n);
    }
    void build(int x, int low, int high) {
        l[x] = low, h[x] = high;
        if(l[x] == h[x]) return;
        int mid = (l[x] + h[x]) >> 1;
        build(x << 1, low, mid);
        build(x << 1 | 1, mid + 1, high);
    }
    void change(int x, int val) {st[x] = min(st[x], val); lz[x] = min(lz[x], val);}
    void down(int x) {
        if(lz[x] == inf || l[x] == h[x]) return;
        change(x << 1, lz[x]), change(x << 1 | 1, lz[x]);
        lz[x] = inf;
    }
    void upd(int x, int low, int high, int val) {
        down(x);
        if(l[x] > high || h[x] < low) return;
        if(l[x] >= low && h[x] <= high) {
            st[x] = min(st[x], val);
            lz[x] = min(lz[x], val);
            return;
        }
        upd(x << 1, low, high, val); upd(x << 1 | 1, low, high, val);
        st[x] = min(st[x << 1], st[x << 1 | 1]);
    }
    int get(int x, int low, int high) {
        down(x);
        if(l[x] > high || h[x] < low) return inf;
        if(l[x] >= low && h[x] <= high) return st[x];
        return min(get(x << 1, low, high), get(x << 1 | 1, low, high));
    }
} it;

void DFS(int u) {
    in[u] = ++T;
    for(int v: adj[u]) DFS(v);
    out[u] = T;
}

int32_t main() {
//    ios_base::sync_with_stdio(0);
//    cin.tie(0); cout.tie(0);
//    #define FileName      "test"
//    if(fopen(FileName".inp", "r")) {
//       freopen(FileName".inp", "r", stdin);
//       freopen(FileName".out", "w", stdout);
//    }
    scanf("%d %d", &n, &d); v.pb(0, 0);
    for(int i = 1; i < n; ++i) {
        scanf("%d", &p[i]);
        adj[p[i]].pb(i);
        dep[i] = dep[p[i]] + 1;
        v.pb(dep[i], i);
    }
    sort(v.begin(), v.end(), greater<pii>());
    DFS(0); it.init(n, d);
    for(pii& node: v) {
        u = node.se;
        if(it.get(1, in[u], in[u]) + dep[u] >= d) {
           ++ans; cur = u;
           while(1) {
              it.upd(1, in[cur], out[cur], dep[u] - 2 * dep[cur]);
              if(cur == 0) break;
              cur = p[cur];
           }
        }
    }
    cout << ans;
}

Compilation message

catinatree.cpp: In function 'int32_t main()':
catinatree.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &d); v.pb(0, 0);
     ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &p[i]);
         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 4960 KB Output is correct
2 Correct 8 ms 4988 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 9 ms 5112 KB Output is correct
20 Correct 8 ms 4988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 4960 KB Output is correct
2 Correct 8 ms 4988 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 9 ms 5112 KB Output is correct
20 Correct 8 ms 4988 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 9 ms 5112 KB Output is correct
23 Correct 8 ms 5112 KB Output is correct
24 Correct 8 ms 5112 KB Output is correct
25 Correct 10 ms 5112 KB Output is correct
26 Correct 9 ms 5112 KB Output is correct
27 Correct 9 ms 5112 KB Output is correct
28 Correct 8 ms 5116 KB Output is correct
29 Correct 9 ms 5240 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5116 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
33 Correct 8 ms 5112 KB Output is correct
34 Correct 8 ms 5112 KB Output is correct
35 Correct 8 ms 5112 KB Output is correct
36 Correct 8 ms 5112 KB Output is correct
37 Correct 9 ms 5240 KB Output is correct
38 Correct 9 ms 5240 KB Output is correct
39 Correct 9 ms 5240 KB Output is correct
40 Correct 48 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 4960 KB Output is correct
2 Correct 8 ms 4988 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 5112 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 9 ms 5112 KB Output is correct
20 Correct 8 ms 4988 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 9 ms 5112 KB Output is correct
23 Correct 8 ms 5112 KB Output is correct
24 Correct 8 ms 5112 KB Output is correct
25 Correct 10 ms 5112 KB Output is correct
26 Correct 9 ms 5112 KB Output is correct
27 Correct 9 ms 5112 KB Output is correct
28 Correct 8 ms 5116 KB Output is correct
29 Correct 9 ms 5240 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5116 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
33 Correct 8 ms 5112 KB Output is correct
34 Correct 8 ms 5112 KB Output is correct
35 Correct 8 ms 5112 KB Output is correct
36 Correct 8 ms 5112 KB Output is correct
37 Correct 9 ms 5240 KB Output is correct
38 Correct 9 ms 5240 KB Output is correct
39 Correct 9 ms 5240 KB Output is correct
40 Correct 48 ms 5240 KB Output is correct
41 Correct 217 ms 25316 KB Output is correct
42 Correct 780 ms 15212 KB Output is correct
43 Correct 104 ms 15212 KB Output is correct
44 Correct 96 ms 15208 KB Output is correct
45 Correct 92 ms 15208 KB Output is correct
46 Execution timed out 1092 ms 25444 KB Time limit exceeded
47 Halted 0 ms 0 KB -