이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |