이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 1e5+7, M=18;
const int MOD = 1e9+7;
int n, m, k, t;
int h[N], P[N][M];
vector<int> adj[N];
int vsz[N], sum[N];
void dfs(int u) {
vsz[u]=1;
for(int v, i=0;i<sz(adj[u]);i++) {
v=adj[u][i], h[v]=h[u]+1, P[v][0]=u;
dfs(v), vsz[u]+=vsz[v];
}
}
void dfs2(int u) {
for(int v, i=0;i<sz(adj[u]);i++) {
v=adj[u][i], sum[u]-=sum[v];
dfs2(v), sum[u]+=sum[v];
if(!k) break;
}
if(k) {
k--, sum[u]++;
if(!k) printf("%d\n", u);
}
}
void process(int s) {
memset(P, -1, sizeof P), h[s]=0, dfs(s);
memset(sum, 0, sizeof sum);
for(int j=0;(1<<j)<=n;j++)
for(int i=1;i<=n;i++)
if(P[i][j-1]!=-1)
P[i][j]=P[P[i][j-1]][j-1];
}
int kth(int u, int k) {
int log=log2(h[u]);
if(h[u]<k) return 0;
for(int i=log;i>=0;i--)
if(k>=(1<<i)) u=P[u][i], k-=(1<<i);
return u;
}
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d%d", &n, &m);
int u;
for(int i=1;i<=n;adj[u].pb(i++)) scanf("%d", &u);
for(int i=1;i<=n;i++) sort(adj[i].begin(), adj[i].end());
process(1);
for(int i=1;i<=m;i++) {
scanf("%d%d", &t, &k);
if(t&1) dfs2(1);
else {
int l=0, r=h[u], mid, ans=h[u];
while(l<=r) {
mid=(l+r)/2, t=kth(k, mid);
if(vsz[t]==sum[t]) l=mid+1, ans=mid;
else r=mid-1;
}
printf("%d\n", ans);t=kth(k, ans);
while(t) sum[t]--, t=P[t][0];
}
}
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special cases (n=1?)
- do smth instead of do nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
ballmachine.cpp:64:43: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | for(int i=1;i<=n;adj[u].pb(i++)) scanf("%d", &u);
| ~~~~~^~~~~~~~~~
ballmachine.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &t, &k);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |