Submission #650313

#TimeUsernameProblemLanguageResultExecution timeMemory
650313zaneyuCat in a tree (BOI17_catinatree)C++14
100 / 100
94 ms22708 KiB
/*input 3 1000 0 0 */ #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #pragma GCC optimize("unroll-loops,no-stack-protector") //order_of_key #of elements less than x // find_by_order kth element using ll = long long; using ld = long double; using pii = pair<ll,ll>; #define f first #define s second #define pb push_back #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define FILL(n,x) memset(n,x,sizeof(n)) #define ALL(_a) _a.begin(),_a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) const ll INF64=4e18; const int INF=0x3f3f3f3f; const ll MOD=998244353; const ld PI=acos(-1); const ld eps=1e-9; #define lowb(x) x&(-x) #define MNTO(x,y) x=min(x,(__typeof__(x))y) #define MXTO(x,y) x=max(x,(__typeof__(x))y) void io(string x){ freopen((x+".in").c_str(),"r",stdin); freopen((x+".out").c_str(),"w",stdout); } ll mult(ll a,ll b){ return a*b%MOD; } ll mypow(ll a,ll b){ if(b<=0) return 1; ll res=1LL; while(b){ if(b&1) res=(res*a)%MOD; a=(a*a)%MOD; b>>=1; } return res; } const ll maxn=2e5+5; const ll maxlg=__lg(maxn)+2; vector<int> v[maxn]; int dp[maxn],far[maxn],id[maxn]; int k; vector<int> ans; void dfs(int u,int p){ int cnt=0; far[u]=k; dp[u]=0; id[u]=u; for(int x:v[u]){ if(x==p) continue; dfs(x,u); MNTO(far[u],far[x]+1); int d=dp[x]+1; if(k%2){ if(d*2+1>=k){ if(cnt) continue; ++cnt; ans.pb(id[x]); } else{ if(dp[u]<d){ dp[u]=d; id[u]=id[x]; } } } else{ if(d*2>=k){ ++cnt; ans.pb(id[x]); } else{ if(dp[u]<d){ dp[u]=d; id[u]=id[x]; } } } } if(cnt) far[u]=k/2; if(dp[u]+far[u]<k) dp[u]=-k; if(!u and dp[u]>=0 and dp[u]+far[u]>=k) ans.pb(id[u]); } int main(){ ios::sync_with_stdio(false),cin.tie(0); int n; cin>>n>>k; if(k==1){ cout<<n<<'\n'; return 0; } REP1(i,n-1){ int a,b=i; cin>>a; v[a].pb(b); v[b].pb(a); } dfs(0,-1); cout<<sz(ans)<<'\n'; //for(auto x:ans) cout<<x+1<<' '; }

Compilation message (stderr)

catinatree.cpp: In function 'void io(std::string)':
catinatree.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen((x+".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen((x+".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...