이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define f first
#define s second
struct segTree{
int l, r;
segTree *left, *right;
int val = 0;
segTree(int a, int b){
l = a;
r = b;
if(l != r){
int mid = (l + r) / 2;
left = new segTree(l, mid);
right = new segTree(mid + 1, r);
}
}
void add(int x, int v){
if(x < l || r < x) return;
if(l == r){
val += v;
return;
}
left->add(x, v);
right->add(x, v);
val = left->val + right->val;
}
int qry(int a, int b){
if(b < l || r < a) return 0;
if(a <= l && r <= b) return val;
return left->qry(a, b) + right->qry(a, b);
}
};
const int maxn = 100000, w = 18;
int n, m, k;
int a[maxn], b[maxn], l[maxn], r[maxn], d[maxn], dp[maxn];
int p[w][maxn];
vector<pi> graph[maxn];
segTree tre(0, maxn);
vector<int> ans;
int dfs(int c){
r[c] = l[c];
for(pi i : graph[c]){
if(i.f == p[0][c]) continue;
d[i.f] = d[c] + 1;
l[i.f] = r[c] + 1;
p[0][i.f] = c;
r[c] = dfs(i.f);
}
return r[c];
}
int lca(int x, int y){
if(d[x] < d[y]) swap(x, y);
for(int i = w - 1; i >= 0; i--) if(p[i][x] != -1 && d[p[i][x]] >= d[y]) x = p[i][x];
for(int i = w - 1; i >= 0; i--){
if(p[i][x] != p[i][y]){
x = p[i][x];
y = p[i][y];
}
}
return x == y ? x : p[0][x];
}
void dfs2(int c){
for(pi i : graph[c]){
if(i.f == p[0][c]) continue;
dfs2(i.f);
if(dp[i.f] >= k) ans.push_back(i.s);
dp[c] += dp[i.f];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back({v, i + 1});
graph[v].push_back({u, i + 1});
}
p[0][0] = -1;
dfs(0);
for(int i = 1; i < w; i++)
for(int j = 0; j < n; j++){
p[i][j] = p[i - 1][j] == -1 ? -1 : p[i - 1][p[i - 1][j]];
}
for(int i = 0; i < m; i++){
int s;
cin >> s;
for(int j = 0; j < s; j++){
cin >> a[j];
a[j]--;
}
sort(a, a + s, [&](int x, int y){
return d[x] > d[y];
});
int lc = a[0];
for(int j = 0; j < s; j++){
lc = lca(lc, a[j]);
b[j] = -tre.qry(l[a[j]], r[a[j]]) + 1;
dp[a[j]] += b[j];
tre.add(l[a[j]], b[j]);
}
dp[lc] -= tre.qry(l[lc], r[lc]);
for(int j = 0; j < s; j++){
tre.add(l[a[j]], -b[j]);
}
}
dfs2(0);
cout << ans.size() << endl;
if(!ans.empty()) cout << ans[0];
for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
cout << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:139:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
~~^~~~~~~~~~~~| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |