이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author : Nguyễn Trọng Nguyễn - ITK22 NBK
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define fi first
#define sc second
const int maxn = (int)1e5;
const int inf = (int)1e9;
const int lg = 16;
int n, m, k;
vector <int> adj[maxn + 5];
int timer = 0, tin[maxn + 5], tout[maxn + 5];
int up[lg + 5][maxn + 5];
int cnt[maxn + 5];
ii edge[maxn + 5];
void init () {
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
edge[i] = {u, v};
}
}
void dfs (int u = 1, int p = 1) {
tin[u] = ++timer;
up[0][u] = p;
for (int e = 1; e <= lg; e++)
up[e][u] = up[e - 1][up[e - 1][u]];
for (auto v : adj[u])
if (v != p)
dfs(v, u);
tout[u] = timer;
}
bool IsAncestor (int u, int v) {
return tin[u] <= tin[v] and tout[u] >= tout[v];
}
int lca (int u, int v) {
if (IsAncestor(u, v)) return u;
if (IsAncestor(v, u)) return v;
for (int e = lg; e >= 0; e--)
if (!IsAncestor(up[e][u], v))
u = up[e][u];
return up[0][u];
}
void dfs2 (int u = 1) {
for (auto v : adj[u]) {
if (v == up[0][u]) continue;
dfs2(v);
cnt[u] += cnt[v];
}
}
void process () {
dfs();
vector <ii> fix;
for (int i = 1; i <= m; i++) {
fix.clear();
int x; cin >> x;
while (x--) {
int u; cin >> u;
fix.push_back({tin[u], u});
}
sort(fix.begin(), fix.end());
int cur = fix[0].sc;
for (auto v : fix) {
cnt[v.sc]++;
cnt[lca(cur, v.sc)]--;
cur = v.sc;
}
}
dfs2();
// for (int u = 1; u <= n; u++)
// cout << cnt[u] << ' ';
// cout << '\n';
vector <int> node;
for (int i = 1; i < n; i++) {
int u, v; tie(u, v) = edge[i];
int ans = up[0][u] == v ? cnt[u] : cnt[v];
if (ans >= k) node.push_back(i);
}
cout << (int)node.size() << '\n';
for (int v : node) cout << v << ' ';
}
signed main () {
cin.tie(0)->sync_with_stdio(false);
init();
process();
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... |