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;
#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 2e5 + 5;
const int LG = 20;
vector<int> ans;
vector<pair<int, int>> g[NM];
int n, m, k, tin[NM], tout[NM], dep[NM], timer, dp[NM];
pair<int, int> st[NM][LG], p[NM];
int lca(int u, int v)
{
u = tin[u], v = tin[v];
if (u > v)
swap(u, v);
int L = __lg(v - u + 1);
return min(st[u][L], st[v - (1 << L) + 1][L]).se;
}
bool ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
void upd(vector<int>& V)
{
sort(V.begin(), V.end(), [&](const int& x, const int& y){return tin[x] < tin[y];});
for (int j = V.size(), i = 1; i < j; i++)
V.push_back(lca(V[i], V[i - 1]));
sort(V.begin(), V.end(), [&](const int& x, const int& y){return tin[x] < tin[y];});
V.erase(unique(V.begin(), V.end()), V.end());
vector<int> st;
for (int u : V)
{
if (st.size() && !ancestor(st.back(), u))
st.pop_back();
if (st.size())
dp[st.back()]--, dp[u]++;
st.push_back(u);
}
}
void dfs(int u)
{
st[++timer][0] = {dep[u], u};
tin[u] = timer;
for (auto v : g[u])
if (v.fi != p[u].fi)
{
dep[v.fi] = dep[u] + 1;
p[v.fi] = {u, v.se};
dfs(v.fi);
st[++timer][0] = {dep[u], u};
}
tout[u] = timer;
}
void DFS(int u)
{
for (auto v : g[u])
if (v.fi != p[u].fi)
{
DFS(v.fi);
dp[u] += dp[v.fi];
}
if (u != 1 && dp[u] >= k)
ans.push_back(p[u].se);
}
void solve()
{
cin >> n >> m >> k;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
dfs(1);
for (int i = 1; (1 << i) <= timer; i++)
for (int j = 1; j + (1 << i) - 1 <= timer; j++)
st[j][i] = min(st[j][i - 1], st[j + (1 << i - 1)][i - 1]);
while (m--)
{
int t;
cin >> t;
vector<int> V;
while (t--)
{
int u;
cin >> u;
V.push_back(u);
}
upd(V);
}
DFS(1);
cout << ans.size() << endl;
for (int t : ans)
cout << t << ' ';
}
signed main()
{
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
else if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
railway.cpp: In function 'void solve()':
railway.cpp:96:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
96 | st[j][i] = min(st[j][i - 1], st[j + (1 << i - 1)][i - 1]);
| ~~^~~
railway.cpp: In function 'int main()':
railway.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
railway.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(name".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... |