이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100005;
const int logn = 20;
int n, q, k;
vector<pair<int, int>> g[maxn];
int depth[maxn];
int jump[maxn][logn];
int parentEdgeIndex[maxn];
int in[maxn];
int out[maxn];
int timer = 0;
int s;
int cities[maxn];
void dfs(int node, int parent, int dep) {
in[node] = ++timer;
depth[node] = dep;
for (auto [v, idx] : g[node]) {
if (v != parent) {
parentEdgeIndex[v] = idx;
jump[v][0] = node;
dfs(v, node, dep + 1);
}
}
out[node] = timer;
}
void build_sparse() {
jump[1][0] = 1;
for (int i = 1; i < logn; ++i) {
for (int j = 1; j <= n; ++j) {
jump[j][i] = jump[jump[j][i - 1]][i - 1];
}
}
}
int get_lca(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
int diff = depth[x] - depth[y];
for (int i = 0; i < logn; ++i) {
if ((diff & (1 << i)) != 0) {
x = jump[x][i];
diff -= (1 << i);
if (diff == 0) break;
}
}
if (x == y) return x;
for (int i = logn - 1; i >= 0; --i) {
if (jump[x][i] != jump[y][i]) {
x = jump[x][i];
y = jump[y][i];
}
}
return jump[x][0];
}
bool cmp(int i, int j) {
return in[i] < in[j];
}
int tree[4 * maxn];
void update(int node, int l, int r, int idx, int delta) {
if (idx < l || idx > r) return;
if (l == r) {
tree[node] += delta;
return;
}
int mid = (l + r) / 2;
update(node * 2, l, mid, idx, delta);
update(node * 2 + 1, mid + 1, r, idx, delta);
tree[node] = tree[node * 2] + tree[node * 2 + 1];
}
int query(int node, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return 0;
if (ql <= l && r <= qr) return tree[node];
int mid = (l + r) / 2;
return query(node * 2, l, mid, ql, qr) + query(node * 2 + 1, mid + 1, r, ql, qr);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q >> k;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(1, 0, 1);
build_sparse();
while (q--) {
cin >> s;
for (int i = 1; i <= s; ++i) {
cin >> cities[i];
}
sort(cities + 1, cities + s + 1, cmp);
cities[s + 1] = cities[1];
for (int i = 1; i <= s; ++i) {
int lca = get_lca(cities[i], cities[i + 1]);
update(1, 1, n, in[cities[i]], +1);
update(1, 1, n, in[cities[i + 1]], +1);
update(1, 1, n, in[lca], -2);
}
}
vector<int> ans;
for (int i = 1; i <= n; ++i) {
if (query(1, 1, n, in[i], out[i]) >= 2 * k) {
ans.push_back(parentEdgeIndex[i]);
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " ";
cout << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:159:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for (int i = 0; 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... |