Submission #483010

#TimeUsernameProblemLanguageResultExecution timeMemory
483010MohamedAliSaidaneCat in a tree (BOI17_catinatree)C++14
51 / 100
1092 ms23984 KiB
#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,vi> m; vi inc; set<int> rem; for(int i = 0; i <n; i++) { m[depth[i]].pb(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 --) { 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> u = q.front(); q.pop(); int node = u.ff.ff; int ds = u.ff.ss; int from = u.ss; if(ds >= d) continue; rem.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 (stderr)

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;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...