This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
struct E {
int to, i;
};
const int N = 1e5 + 5, L = 19;
int tin[N], tout[N], p[N][L], w[N];
int n, m, k, timer = 0;
vector<E> g[N];
vector<int> ans;
void dfs(int node) {
tin[node] = ++timer;
for (int i = 1; i < L; i++) {
p[node][i] = p[p[node][i - 1]][i - 1];
}
for (E e : g[node]) {
if (e.to != p[node][0]) {
p[e.to][0] = node;
dfs(e.to);
}
}
tout[node] = timer;
}
void dive(int node) {
for (E e : g[node]) {
if (e.to != p[node][0]) {
dive(e.to);
if (w[e.to] >= k)
ans.push_back(e.i);
w[node] += w[e.to];
}
}
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int getLca(int a, int b) {
if (upper(a, b))
return a;
for (int i = L - 1; i >= 0; i--) {
if (!upper(p[a][i], b))
a = p[a][i];
}
return p[a][0];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m >> k;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back({b, i});
g[b].push_back({a, i});
}
dfs(0);
for (int i = 0; i < m; i++) {
int s;
cin >> s;
vector<int> v;
for (int j = 0; j < s; j++) {
int x;
cin >> x;
v.push_back(x - 1);
}
sort(all(v), [](int x, int y) {
return tin[x] < tin[y];
});
for (int j = 0; j < v.size(); j++) {
int h = (j + 1 == v.size() ? 0 : j + 1),
lca = getLca(v[j], v[h]);
w[v[j]]++;
w[lca]--;
}
}
dive(0);
cout << ans.size() << "\n";
sort(all(ans));
for (int x : ans)
cout << x + 1 << " ";
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:89:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < v.size(); j++) {
~~^~~~~~~~~~
railway.cpp:90:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
int h = (j + 1 == v.size() ? 0 : j + 1),
~~~~~~^~~~~~~~~~~
# | 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... |