Submission #168317

#TimeUsernameProblemLanguageResultExecution timeMemory
168317muhi1112Birokracija (COCI18_birokracija)C++17
100 / 100
92 ms25780 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define f1 first #define s2 second #define pb push_back #define mp make_pair #define int long long #define fri(a) freopen(a,"r",stdin); #define fro(a) freopen(a,"w",stdout); const int N=2e5+5; int n,p[N],dp[N]; vector<int>v[N]; pair<int,int> dfs(int x){ int ans=0; int child=0; for(auto i:v[x]){ if(i!=p[i]){ pair<int,int>k=dfs(i); ans+=k.f1; child+=k.s2; } } //cout<<x<<" "<<ans<<" "<<child<<endl; dp[x]=ans+child+1; pair<int,int>sa={ans+child+1,child+1}; return sa; } int32_t main(){ //fri("in.txt"); //fro("out.txt"); ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n; p[1]=1; for(int i=2;i<=n;i++){ cin>>p[i]; v[p[i]].pb(i); } dfs(1); for(int i=1;i<=n;i++){ cout<<dp[i]<<" "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...