제출 #1115813

#제출 시각아이디문제언어결과실행 시간메모리
1115813ThanhsCat in a tree (BOI17_catinatree)C++17
100 / 100
52 ms24556 KiB
#include <bits/stdc++.h>
using namespace std;

#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))

mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 2e5 + 5;

vector<int> g[NM];
int n, d;
pair<int, int> dp[NM];

void dfs(int u)
{
    vector<int> V;
    dp[u] = {0, 0};
    for (int v : g[u])
    {
        dfs(v);
        pair<int, int> t = {dp[v].fi, dp[v].se + 1};
        dp[u].fi += t.fi;
        V.push_back(t.se);
    }
    sort(V.begin(), V.end(), greater<>());
    while (V.size() >= 2 && V[V.size() - 1] + V[V.size() - 2] < d)
    {
        dp[u].fi--;
        V.pop_back();
    }
    dp[u].se = V.empty() ? d : V.back();
    if (dp[u].se >= d)
        dp[u].fi++, dp[u].se = 0;

}

void solve()
{
    cin >> n >> d;
    for (int i = 2; i <= n; i++)
    {
        int t;
        cin >> t;
        t++;
        g[t].push_back(i);
    }
    dfs(1);
    cout << dp[1].fi << endl;
}

signed main()
{
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    else if (fopen(name".inp", "r"))
    {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int tc = 1; 
    // cin >> tc;
    while (tc--)
        solve();
}

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

catinatree.cpp: In function 'int main()':
catinatree.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...