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 TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r) {
return uniform_int_distribution<long long> (l, r)(rng);
}
int n, m, k;
vector<ii> g[N];
int d[N], par[20][N], timedfs = 0, tin[N];
int sum[N];
void DFS(int u) {
tin[u] = ++timedfs;
for (auto [v, id]: g[u]) {
if (v == par[0][u]) continue;
d[v] = d[u] + 1;
par[0][v] = u;
for (int i = 1; i <= 16; i++) {
par[i][v] = par[i - 1][par[i - 1][v]];
}
DFS(v);
}
}
bool cmp(int a, int b) {
return tin[a] < tin[b];
}
int LCA(int u, int v) {
if (u == v) return u;
if (d[u] < d[v]) swap(u, v);
int delta = d[u] - d[v];
for (int i = 0; i <= 16; i++) {
if (delta & (1 << i)) {
u = par[i][u];
}
}
if (u == v) return u;
for (int i = 16; i >= 0; i--) {
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
void up(int u, int v) {
int lc = LCA(u, v);
// cout << u << " " << v << '\n';
sum[u]++;
sum[v]++;
sum[lc] -= 2;
}
vector<int> ans;
void dfs(int u) {
for (auto [v, id] : g[u]) {
if (v == par[0][u]) continue;
dfs(v);
sum[u] += sum[v];
if (sum[v] >= 2 * k) ans.push_back(id);
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m >> k;
For (i, 1, n - 1) {
int u, v; cin >> u >> v;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
memset(d, 0, sizeof d);
memset(sum ,0, sizeof sum);
DFS(1);
For (i, 1, m) {
int s;
cin >> s;
vector<int> spe(s);
For (j, 1, s) {
cin >> spe[j - 1];
}
sort (all(spe), cmp);
for (int j = 0; j < s; j++) {
up(spe[j], spe[(j + 1) % s]);
}
}
dfs(1);
sort(all(ans));
cout << ans.size() << '\n';
for (int x: ans) cout << x << " ";
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
Compilation message (stderr)
railway.cpp: In function 'void DFS(int)':
railway.cpp:29:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for (auto [v, id]: g[u]) {
| ^
railway.cpp: In function 'void dfs(int)':
railway.cpp:76:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | for (auto [v, id] : g[u]) {
| ^
railway.cpp: In function 'int main()':
railway.cpp:89:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen (".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
railway.cpp:90:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen (".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
railway.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |