#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int N = 1.6e6 + 5, MOD = 1e9 + 7;
int n, q;
vector<pii> adj[N];
int p[N], pt[N], eid[N], vid[N], ans[N], timedfs = 0, ee = 0, s = 0;
vector<int> tid[N];
template <typename T>
struct Fenwick {
int n;
std::vector<T> a;
Fenwick(int n_ = 0) {
init(n_);
}
void init(int n_) {
n = n_;
a.assign(n + 5, T{});
}
void upd(int x, const T &v) {
for (int i = x; i <= n; i += i & -i) {
a[i] = a[i] + v;
}
}
T get(int x) {
T ans{};
for (int i = x; i > 0; i -= i & -i) {
ans = ans + a[i];
}
return ans;
}
T get(int l, int r) {
return get(r) - get(l - 1);
}
int kth(const T &k) {
int x = 0;
T cur{};
for (int i = 20; i >= 0; i--) {
if (x + (1 << i) <= n && cur + a[x + (1 << i)] < k) {
x += (1 << i);
cur = cur + a[x];
}
}
return x + 1;
}
};
Fenwick<int> bit;
bool vis[N];
vector<int> nxt;
void dfs(int i, int pre){
for(auto &[x, id] : adj[i]){
if(x == pre) continue;
id = ++ee;
p[x] = i, dfs(x, i);
}
}
void dfs2(int i){
for(int j = (pt[i] + 1) % adj[i].size(); ; j = (j + 1) % adj[i].size()){
if(j == pt[i] && i > 1) break;
int x = adj[i][j].first;
vid[++timedfs] = x, eid[timedfs] = adj[i][j].second;
dfs2(x);
vid[++timedfs] = i, eid[timedfs] = adj[i][j].second;
if(j == pt[i] && i == 1) break;
}
}
void dfs3(int i){
while(true){
pt[i] = (pt[i] + 1) % adj[i].size();
int j = pt[i], x = adj[i][j].first, ee = adj[i][j].second;
if(vis[ee]) break;
if(x == p[i]){
nxt.push_back(i);
break;
}
bit.upd(tid[ee][0], 1);
bit.upd(tid[ee][1], 1);
s += 2;
dfs3(x);
vis[ee] = true;
}
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; i++){
int sz; cin >> sz;
for(int j = 0; j < sz; j++){
int x; cin >> x;
adj[i].push_back({x, 0});
}
}
dfs(1, 1);
for(int i = 2; i <= n; i++){
for(int j = 0; j < (int) adj[i].size(); j++){
if(adj[i][j].first == p[i]) pt[i] = j;
}
}
dfs2(1);
bit.init(timedfs);
memset(pt, 0, sizeof pt);
vector<pii> b;
for(int i = 1; i <= q; i++){
int x; cin >> x; b.push_back({x, i});
}
sort(b.begin(), b.end());
for(int i = 1; i <= timedfs; i++) tid[eid[i]].push_back(i);
queue<int> qu; qu.push(1);
int total = 0, qid = 0;
while(s < timedfs){
nxt.clear();
int last = total;
while(!qu.empty()){
dfs3(qu.front());
qu.pop();
}
for(int x : nxt){
qu.push(x);
}
total += s;
while(qid < b.size() && b[qid].first <= total){
ans[b[qid++].second] = vid[bit.kth(b[qid].first - last)];
}
}
while(qid < b.size()){
ans[b[qid++].second] = vid[(b[qid].first - total - 1) % timedfs + 1];
}
for(int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "MAZE"
if(fopen(task ".inp", "r")){
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(task ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |