#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int SZ = 1 << 21;
int N, Q, On[808080], R[808080], T[SZ<<1];
vector<int> G[80808];
vector<pair<int,int>> Tour;
vector<int> Idx[808080]; // Idx[t] : t에 켜지는 정점
void Add(int x, int v){
for(x|=SZ; x; x>>=1) T[x] += 1;
}
int Kth(int k){
int x = 1;
while(x < SZ){
if(k <= T[x<<1]) x = x << 1;
else k -= T[x<<1], x = x << 1 | 1;
}
return x ^ SZ;
}
inline void AddEdge(int s, int e){
Idx[max(On[s], On[e])].push_back(Tour.size());
Tour.emplace_back(s, e);
}
void DFS(int v, int b=-1, int t=1){
On[v] = t;
bool flag = false;
for(auto i : G[v]){
if(i == b) flag = true;
else if(flag) On[i] = t + 1, AddEdge(v, i), DFS(i, v, t+1), AddEdge(i, v);
}
for(auto i : G[v]){
if(i == b) break;
else On[i] = t, AddEdge(v, i), DFS(i, v, t), AddEdge(i, v);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> Q;
for(int i=1; i<=N; i++){
int sz; cin >> sz; G[i].resize(sz);
for(auto &j : G[i]) cin >> j;
rotate(G[i].begin(), G[i].begin()+1, G[i].end());
}
DFS(1);
vector<pair<ll,ll>> V(Q);
for(int i=0; i<Q; i++) cin >> V[i].first, V[i].second = i + 1;
sort(V.begin(), V.end());
ll now = 0;
for(int i=0, j=1; i<V.size(); i++){
for(; j<=N && now<V[i].first; j++){
for(auto e : Idx[j]) Add(e, 1);
now += T[1];
}
ll k = V[i].first - (now - T[1]);
R[V[i].second] = Kth((k - 1) % (2*N-2) + 1);
}
for(int i=1; i<=Q; i++) cout << Tour[R[i]].second << "\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:56:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i=0, j=1; i<V.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21204 KB |
Output is correct |
2 |
Correct |
19 ms |
23836 KB |
Output is correct |
3 |
Incorrect |
92 ms |
46732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21332 KB |
Output is correct |
2 |
Correct |
13 ms |
21368 KB |
Output is correct |
3 |
Correct |
12 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21444 KB |
Output is correct |
5 |
Correct |
16 ms |
21460 KB |
Output is correct |
6 |
Correct |
16 ms |
21460 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
16 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21588 KB |
Output is correct |
10 |
Correct |
13 ms |
21588 KB |
Output is correct |
11 |
Correct |
13 ms |
21628 KB |
Output is correct |
12 |
Correct |
13 ms |
21716 KB |
Output is correct |
13 |
Correct |
12 ms |
21716 KB |
Output is correct |
14 |
Correct |
13 ms |
21588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
22432 KB |
Output is correct |
2 |
Correct |
37 ms |
27128 KB |
Output is correct |
3 |
Incorrect |
80 ms |
36096 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21204 KB |
Output is correct |
2 |
Correct |
19 ms |
23836 KB |
Output is correct |
3 |
Incorrect |
92 ms |
46732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |