이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n = 1e5 + 5,root = -1,q;
int timer = 0;
vvi adj(n),dp(n,vi(21));
vi out(n),minNode(n),dep(n);
vb is(n);
set<pair<int,int>>unfilled;
bool cmp(int u,int v)
{
return minNode[u] < minNode[v];
}
int compute(int u,int p)
{
minNode[u] = u;
for (int v:adj[u])if (v != p)minNode[u] = min(minNode[u],compute(v,u));
return minNode[u];
}
void dfs(int u,int p)
{
dep[u] = dep[p] + 1;
dp[u][0] = p;
for (int i = 1;i<=20;i++)dp[u][i] = dp[dp[u][i - 1]][i - 1];
sort(adj[u].begin(),adj[u].end(),cmp);
for (int v:adj[u]){
if (v == p)continue;
dfs(v,u);
}
out[u] = ++timer;
unfilled.insert({out[u],u});
}
int add()
{
auto it = unfilled.begin();
unfilled.erase(it);
is[(*it).second] = 1;
return (*it).second;
}
int sub(int u)
{
int v = u;
for (int i = 20;i>=0;i--){
if (is[dp[v][i]])v = dp[v][i];
}
is[v] = 0;
unfilled.insert({out[v],v});
return dep[u] - dep[v];
}
int main()
{
fast;
cin>>n>>q;
for (int i = 0;i<n;i++){
int p;
cin>>p;
p--;
if (p == -1)root = i;
else adj[p].pb(i);
}
compute(root,root);
dfs(root,root);
while (q--){
int t,u;
cin>>t>>u;
if (t == 1){
while (u-- > 1)add();
cout<<add() + 1<<'\n';
}
else{
cout<<sub(u - 1)<<'\n';
}
}
return 0;
}
# | 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... |