제출 #203415

#제출 시각아이디문제언어결과실행 시간메모리
203415karmaCat in a tree (BOI17_catinatree)C++14
0 / 100
8 ms4988 KiB
#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;
    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); --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 upd(int x, int pos, int val) {
        if(l[x] > pos || h[x] < pos) return;
        if(l[x] >= pos && h[x] <= pos) {st[x] = val; return;}
        upd(x << 1, pos, val); upd(x << 1 | 1, pos, val);
        st[x] = min(st[x << 1], st[x << 1 | 1]);
    }
    int get(int x, int low, int high) {
        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);
    }
    cin >> n >> d;
    for(int i = 1; i < n; ++i) {
        cin >> 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, 1, in[u]) + dep[u] >= d) {
           ++ans; rem = d; cur = u;
           while(rem --) {
              it.upd(1, in[cur], dep[u] - 2 * dep[cur]);
              if(cur == 0) break;
              cur = p[cur];
           }
        }
    }
    cout << ans;
}

컴파일 시 표준 에러 (stderr) 메시지

catinatree.cpp: In function 'int32_t main()':
catinatree.cpp:58:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
        freopen(FileName".inp", "r", stdin);
        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:59:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
        freopen(FileName".out", "w", stdout);
        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...