답안 #700074

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
700074 2023-02-18T15:46:00 Z tamthegod Cat in a tree (BOI17_catinatree) C++17
0 / 100
19 ms 23816 KB
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, d;
vector<int> adj[maxN];
int mark[maxN];
int f[maxN];
int depth[maxN];
struct cmp
{
    bool operator ()(int i, int j) const
    {
        return (depth[i] > depth[j] || depth[i] == depth[j] && i < j);
    }
};
set<int, cmp> S;
void ReadInput()
{
    cin >> n >> d;
    for(int i=2; i<=n; i++)
    {
        int p;
        cin >> p;
        p++;
        adj[p].pb(i);
        adj[i].pb(p);
    }
}
void dfs(int u, int par)
{
    for(int v : adj[u])
    {
        if(v == par) continue;
        depth[v] = depth[u] + 1;
        dfs(v, u);
    }
}
void del(int u, int par, int cnt)
{
    mark[u] = 1;
    S.erase(u);
    if(cnt == d - 1) return;
    for(int v : adj[u])
    {
        if(mark[v]) continue;
        del(v, u, cnt + 1);
    }
}
void Solve()
{
    dfs(1, 0);
    for(int i=1; i<=n; i++)
        S.insert(i);
    int res = 0;
    while(!S.empty())
    {
        res++;
        del(*S.begin(), 0, 0);
    }
    cout << res;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}

Compilation message

catinatree.cpp: In member function 'bool cmp::operator()(long long int, long long int) const':
catinatree.cpp:26:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   26 |         return (depth[i] > depth[j] || depth[i] == depth[j] && i < j);
      |                                        ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23764 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Incorrect 14 ms 23816 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23764 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Incorrect 14 ms 23816 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23764 KB Output is correct
2 Correct 12 ms 23760 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Incorrect 14 ms 23816 KB Output isn't correct
5 Halted 0 ms 0 KB -