이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
#define LSOne(s) (s & -(s))
using ll = long long;
const int LOG = 18;
class BIT {
int n;
vector<int> ft;
public:
BIT(int _n) : n(_n), ft(n + 1, 0) {}
void update(int i, int v) {
for (; i <= n; i += LSOne(i)) ft[i] += v;
}
int query(int l, int r) {
int ans = 0;
for (; r; r -= LSOne(r)) ans += ft[r];
for (; l; l -= LSOne(l)) ans -= ft[l];
return ans;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<int> depth(n + 1), in(n + 1), out(n + 1), par_edge(n + 1);
vector<array<int, 2>> edges(n);
vector<vector<array<int, 2>>> adj(n + 1, vector<array<int, 2>>());
vector<array<int, LOG>> par(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edges[i] = {u, v};
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
int cnt = 1;
function<void(int, int, int)> dfs = [&] (int u, int p, int pe) {
par[u][0] = p;
par_edge[u] = pe;
for (int i = 1; i < LOG; i++) par[u][i] = par[par[u][i - 1]][i - 1];
depth[u] = depth[p] + 1;
in[u] = cnt++;
for (auto [v, j] : adj[u]) {
if (v == p) continue;
dfs(v, u, j);
}
out[u] = cnt;
};
dfs(1, 0, 0);
BIT ft(cnt);
auto is_anc = [&] (int u, int v) -> bool {
return in[u] <= in[v] && out[v] <= out[u];
};
auto lca = [&] (int u, int v) -> int {
if (depth[u] > depth[v]) swap(u, v);
if (is_anc(u, v)) return u;
int d = depth[v] - depth[u];
for (int i = LOG - 1; i >= 0; i--) {
if (d&(1<<i)) v = par[v][i];
}
for (int i = LOG - 1; i >= 0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
};
while (m--) {
int s;
cin >> s;
vector<int> b(s);
for (int i = 0; i < s; i++) cin >> b[i];
sort(b.begin(), b.end(), [&] (int a, int b) { return in[a] < in[b]; });
b.push_back(b.front());
for (int i = 0; i < s; i++) {
int u = b[i], v = b[i + 1];
int w = lca(u, v);
ft.update(in[u], 1);
ft.update(in[v], 1);
ft.update(in[w], -2);
}
}
vector<int> res;
for (int i = 1; i < n; i++) {
if (ft.query(in[i] - 1, out[i]) >= 2*k) res.push_back(par_edge[i]);
}
cout << res.size() << '\n';
for (auto u : res) cout << u << ' ';
return 0;
}
# | 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... |