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 int long long
#define float double
#define pb push_back
#define F first
#define S second
#define T int t; cin >> t; while(t--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
/// Benzema is the best player in the world
const int N = 1e6 + 6;
const int M = 3e4 + 3;
const int mod = 1e9 + 7;
const int inf = 1e18;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int LOG = 28;
int n, m, k, timer, par[N], in[N], depth[N], sp[N][LOG];
vector<pair<int, int>> adj[N];
void getDepth(int node, int pa) {
par[node] = pa;
in[node] = ++timer;
depth[node] = depth[pa] + 1;
for(auto x: adj[node])
if (x.F != pa) getDepth(x.F, node);
}
void buildsparce() {
for(int i = 1; i <= n; i++) sp[i][0] = par[i];
for(int j = 1; j < 18; j++) {
for(int i = 1; i <= n; i++)
sp[i][j] = sp[sp[i][j - 1]][j - 1];
}
}
int LCA(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for(int i = 18; i >= 0; i--) {
if (depth[u] == depth[v]) break;
if (depth[sp[u][i]] >= depth[v])
u = sp[u][i];
}
if (u == v) return u;
for(int i = 18; i >= 0; i--) {
if (sp[u][i] != sp[v][i])
u = sp[u][i], v = sp[v][i];
}
return par[u];
}
set<int> st[N];
vector<int> ans, trees[N];
void dfs(int node, int par) {
for(auto x: adj[node]) {
if (x.F == par) continue;
dfs(x.F, node);
if (st[x.F].size() >= k)
ans.push_back(x.S);
if (st[node].size() < st[x.F].size())
swap(st[x.F], st[node]);
for(auto y: st[x.F])
st[node].insert(y);
}
for(auto x: trees[node])
st[node].erase(st[node].find(x));
}
main() {
IOS
cin >> n >> m >> k;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
getDepth(1, 0);
buildsparce();
for(int i = 1; i <= m; i++) {
int s;
cin >> s;
int p = 0;
for(int j = 1; j <= s; j++) {
int x;
cin >> x;
if (p != 0) p = LCA(p, x);
else p = x;
st[x].insert(i);
}
trees[p].push_back(i);
}
dfs(1, 0);
cout << ans.size() << '\n';
for(auto x: ans) cout << x << ' ';
}
Compilation message (stderr)
railway.cpp: In function 'void dfs(long long int, long long int)':
railway.cpp:59:28: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
59 | if (st[x.F].size() >= k)
| ~~~~~~~~~~~~~~~^~~~
railway.cpp: At global scope:
railway.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main() {
| ^~~~
# | 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... |