답안 #483017

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483017 2021-10-27T12:05:58 Z MohamedAliSaidane Cat in a tree (BOI17_catinatree) C++14
51 / 100
1000 ms 31328 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (ll)(1000000007)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
const int nx[8] = {0, 0, 1, -1,1,1,-1,-1}, ny[8] = {1, -1, 0, 0,1,-1,1,-1}; //East, West, South, North+
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}
////////////******SOLUTION******\\\\\\\\\\\

const int MAX_N = 2e5 + 4;;
int n, d;
int par[MAX_N];
vi adj[MAX_N];
int depth[MAX_N];
//int dist[MAX_N][MAX_N];
void f(int i, int d)
{
    depth[i] = d;
    for(auto e: adj[i])
    {
        if(e == par[i]) continue;
        f(e,d+1);
    }
}

void solve()
{
    cin >> n >> d;
    for(int i = 1; i < n; i ++)
    {
        cin >> par[i];
        adj[i].pb(par[i]);
        adj[par[i]].pb(i);
    }
    f(0,0);
    int maxt = 0;
    int ans = 1;
    map<int,set<int>> m;
    vi inc;
    set<int> rem;
    for(int i = 0; i <n; i++)
    {
        m[depth[i]].insert(i);
        maxt = max(maxt,depth[i]);
        rem.insert(i);
    }
    /*for(int i =0; i <n; i ++)
    {
        queue<pii> q;
        q.push({i,0});
        bitset<MAX_N> bs;
        while(!q.empty())
        {
            pii u = q.front();
            q.pop();
            bs[u.ff] = 1;
            dist[i][u.ff] = dist[u.ff][i] = u.ss;
            for(auto e: adj[u.ff])
            {
                if(bs[e]) continue;
                q.push({e,u.ss+1});
            }
        }
    }*/
    for(int i = maxt; i >= 0; i --)
    {
        vi trash;
        for(auto u: m[i])
        {
            if(rem.count(u) == 0)
                continue;
            inc.pb(u);
            queue<pair<pii,int>> q;
            q.push({{u,0},-1});
            while(!q.empty())
            {
                pair<pii,int> v = q.front();
                q.pop();
                int node = v.ff.ff;
                int ds = v.ff.ss;
                int from = v.ss;
                if(ds >= d) break;
                rem.erase(node);
                if(depth[node] != depth[u])
                    m[depth[node]].erase(node);
                for(auto e: adj[node])
                {
                    if(e == from) continue;
                    q.push({{e,ds+1},node});
                }
            }
        }
    }
    cout << inc.size();
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int tt =1;
    while(tt--)
    {
        solve();
    }

}
/*

*/

Compilation message

catinatree.cpp:24:1: warning: multi-line comment [-Wcomment]
   24 | ////////////******SOLUTION******\\\\\\\\\\\
      | ^
catinatree.cpp: In function 'void solve()':
catinatree.cpp:53:9: warning: unused variable 'ans' [-Wunused-variable]
   53 |     int ans = 1;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 2 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 5020 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 2 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 5020 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
21 Correct 3 ms 5324 KB Output is correct
22 Correct 3 ms 5068 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5196 KB Output is correct
29 Correct 4 ms 5196 KB Output is correct
30 Correct 4 ms 5196 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 4 ms 5196 KB Output is correct
33 Correct 4 ms 5196 KB Output is correct
34 Correct 4 ms 5196 KB Output is correct
35 Correct 3 ms 5196 KB Output is correct
36 Correct 4 ms 5196 KB Output is correct
37 Correct 3 ms 5196 KB Output is correct
38 Correct 4 ms 5196 KB Output is correct
39 Correct 4 ms 5196 KB Output is correct
40 Correct 3 ms 5324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 2 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 5020 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
21 Correct 3 ms 5324 KB Output is correct
22 Correct 3 ms 5068 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5196 KB Output is correct
29 Correct 4 ms 5196 KB Output is correct
30 Correct 4 ms 5196 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 4 ms 5196 KB Output is correct
33 Correct 4 ms 5196 KB Output is correct
34 Correct 4 ms 5196 KB Output is correct
35 Correct 3 ms 5196 KB Output is correct
36 Correct 4 ms 5196 KB Output is correct
37 Correct 3 ms 5196 KB Output is correct
38 Correct 4 ms 5196 KB Output is correct
39 Correct 4 ms 5196 KB Output is correct
40 Correct 3 ms 5324 KB Output is correct
41 Execution timed out 1099 ms 31328 KB Time limit exceeded
42 Halted 0 ms 0 KB -