제출 #42666

#제출 시각아이디문제언어결과실행 시간메모리
42666XmtosXBall Machine (BOI13_ballmachine)C++14
100 / 100
317 ms38548 KiB
#include <bits/stdc++.h> using namespace std; int n,minn[100005],root,q,sprs[100005][30],cnt[100005],pro[100005],cur,b; vector <int> v[100005]; set <pair<int,int > > s; bool yes[100005]; void dfs (int x,int p) { minn[x]=x; if (p!=-1) sprs[x][0]=p; for (int i=1;i<30;i++) sprs[x][i]=sprs[sprs[x][i-1]][i-1]; for (int i=0;i<v[x].size();i++) { if (v[x][i]!=p) { dfs(v[x][i],x); minn[x]=min(minn[x],minn[v[x][i]]); cnt[x]++; } } } void build (int x,int p) { set <pair<int,int> > s1; for (int i=0;i<v[x].size();i++) { if (v[x][i]!=p) { s1.insert({minn[v[x][i]],v[x][i]}); } } while (!s1.empty()) { build(s1.begin()->second,x); s1.erase(s1.begin()); } pro[x]=++cur; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >>n>>q; for (int i=1;i<=n;i++) { int a; cin >>a; if (!a) root=i; else { v[a].push_back(i); v[i].push_back(a); } } dfs (root,-1); build (root,-1); for (int i=1;i<=n;i++) { if (!cnt[i]) { s.insert({pro[i],i}); } } while (q--) { int t,k; cin >>t>>k; if (t==1) { int x; while (k--) { x=s.begin()->second; s.erase(s.begin()); yes[x]=true; if (x!=root) { cnt[sprs[x][0]]--; if (!cnt[sprs[x][0]]) s.insert({pro[sprs[x][0]],sprs[x][0]}); } } cout <<x<<"\n"; } else { int ans=0; for (int i=20;i>=0;i--) { if (yes[sprs[k][i]]) { k=sprs[k][i]; ans+= (1<<i); } } cout <<ans<<"\n"; yes[k]=false; s.insert({pro[k],k}); if (k!=root) { if (!cnt[sprs[k][0]]) s.erase({pro[sprs[k][0]],sprs[k][0]}); cnt[sprs[k][0]]++; } } } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'void dfs(int, int)':
ballmachine.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                   ^
ballmachine.cpp: In function 'void build(int, int)':
ballmachine.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                   ^
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:87:20: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
             cout <<x<<"\n";
                    ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...