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>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int N = 1e5 + 1;
int n, m, k, tin[N], pe[N], s[N], h[N], p[N][17], timer = 0;
pi e[N];
vi adj[N], ans;
bool cmp(const int &a, const int &b) { return tin[a] < tin[b]; }
int lca(int a, int b) {
if (h[a] > h[b]) swap(a, b);
int diff = h[b] - h[a];
FOR(i, 0, 16)
if (diff & (1 << i))
b = p[b][i];
if (a == b) return a;
FOS(i, 16, 0)
if (p[a][i] != p[b][i]) {
a = p[a][i];
b = p[b][i];
}
return p[a][0];
}
void dfs(int u, int p) {
tin[u] = ++timer;
::p[u][0] = p;
FOR(i, 1, 16) ::p[u][i] = ::p[::p[u][i - 1]][i - 1];
EACH(j, adj[u]) {
int v = (e[j].st == u? e[j].nd : e[j].st);
if (v == p) continue;
pe[v] = j;
h[v] = h[u] + 1;
dfs(v, u);
}
}
void dfs1(int u, int p) {
EACH(j, adj[u]) {
int v = (e[j].st == u? e[j].nd : e[j].st);
if (v == p) continue;
dfs1(v, u);
s[u] += s[v];
}
if (s[u] >= k && pe[u]) ans.pb(pe[u]);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> m >> k;
FOR(i, 1, n - 1) {
int a, b;
cin >> a >> b;
e[i] = {a, b};
adj[a].pb(i);
adj[b].pb(i);
}
dfs(1, 0);
FOR(i, 1, m) {
int s;
cin >> s;
vi a(s);
EACH(j, a) cin >> j;
sort(all(a), cmp);
int LCA = a[0], prev = a[0];
FOR(j, 1, s - 1) {
int nLCA = lca(prev, a[j]);
++::s[a[j]];
--::s[nLCA];
if (h[nLCA] < h[LCA]) {
++::s[LCA];
--::s[nLCA];
LCA = nLCA;
}
prev = a[j];
}
}
dfs1(1, 0);
sort(all(ans));
cout << sz(ans) << '\n';
EACH(i, ans) cout << i << ' ';
return 0;
}
/*
6 3 2
1 3
2 3
3 4
6 4
4 5
4 1 3 2 5
2 6 3
2 3 2
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message (stderr)
railway.cpp: In function 'int lca(int, int)':
railway.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
railway.cpp:59:2: note: in expansion of macro 'FOR'
59 | FOR(i, 0, 16)
| ^~~
railway.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
railway.cpp:63:2: note: in expansion of macro 'FOS'
63 | FOS(i, 16, 0)
| ^~~
railway.cpp: In function 'void dfs(int, int)':
railway.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
railway.cpp:73:2: note: in expansion of macro 'FOR'
73 | FOR(i, 1, 16) ::p[u][i] = ::p[::p[u][i - 1]][i - 1];
| ^~~
railway.cpp:36:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
railway.cpp:74:2: note: in expansion of macro 'EACH'
74 | EACH(j, adj[u]) {
| ^~~~
railway.cpp: In function 'void dfs1(int, int)':
railway.cpp:36:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
railway.cpp:83:2: note: in expansion of macro 'EACH'
83 | EACH(j, adj[u]) {
| ^~~~
railway.cpp: In function 'int main()':
railway.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
railway.cpp:96:5: note: in expansion of macro 'FOR'
96 | FOR(i, 1, n - 1) {
| ^~~
railway.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
railway.cpp:104:5: note: in expansion of macro 'FOR'
104 | FOR(i, 1, m) {
| ^~~
railway.cpp:36:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
railway.cpp:108:6: note: in expansion of macro 'EACH'
108 | EACH(j, a) cin >> j;
| ^~~~
railway.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
railway.cpp:111:6: note: in expansion of macro 'FOR'
111 | FOR(j, 1, s - 1) {
| ^~~
railway.cpp:36:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
36 | #define EACH(i, x) for (auto &(i) : (x))
| ^
railway.cpp:126:5: note: in expansion of macro 'EACH'
126 | EACH(i, ans) cout << i << ' ';
| ^~~~
# | 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... |