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;
typedef long long ll;
#define name "main"
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, b, a) for (int i = (b); i >= (a); i--)
template <class U, class V>
bool maximize(U &x, V y) {
if (x < y) { x = y; return true; }
return false;
}
template <class U, class V>
bool minimize(U &x, V y) {
if (x > y) { x = y; return true; }
return false;
}
const int INF = 1e9;
const int N = 1e5 + 5;
int numNode, numQuery, limit;
vector<pair<int, int>> adj[N];
vector<int> query[N];
int in[N];
int cur = 0;
struct LCA {
int par[20][N], high[N];
void dfs(int u) {
in[u] = ++cur;
for (pair<int, int> e : adj[u]) {
int v, id;
tie(v, id) = e;
if (v == par[0][u]) continue;
par[0][v] = u;
high[v] = high[u] + 1;
dfs(v);
}
}
void setup() {
dfs(1);
FOR(i, 1, 18) FOR(u, 1, numNode)
par[i][u] = par[i - 1][par[i - 1][u]];
high[0] = -1;
}
int getLCA(int u, int v) {
if (high[u] < high[v]) swap(u, v);
FOD(i, 18, 0) if (high[par[i][u]] >= high[v])
u = par[i][u];
if (u == v) return u;
FOD(i, 18, 0) if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
return par[0][u];
}
} lca;
bool cmp(int x, int y) {
return in[x] < in[y];
}
int cnt[N], ans[N];
void dfs(int u, int p) {
for (pair<int, int> e : adj[u]) {
int v, id;
tie(v, id) = e;
if (v == p) continue;
dfs(v, u);
cnt[u] += cnt[v];
ans[id] = cnt[v];
}
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
if (fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
cin >> numNode >> numQuery >> limit;
FOR(i, 1, numNode - 1) {
int u, v;
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
FOR(i, 1, numQuery) {
int s;
cin >> s;
while (s--) {
int u;
cin >> u;
query[i].push_back(u);
}
}
lca.setup();
FOR(i, 1, numQuery) {
vector<int> nodes;
FOR(j, 0, query[i].size() - 1) {
nodes.push_back(query[i][j]);
if (j > 0) nodes.push_back(lca.getLCA(query[i][j - 1], query[i][j]));
}
sort(nodes.begin(), nodes.end(), cmp);
nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
nodes.push_back(nodes[0]);
FOR(j, 1, nodes.size() - 1) {
int p = lca.getLCA(nodes[j], nodes[j - 1]);
cnt[nodes[j]]++, cnt[nodes[j - 1]]++;
cnt[p] -= 2;
}
}
dfs(1, -1);
int res = 0;
vector<int> trace;
FOR(i, 1, numNode - 1) if (ans[i] / 2 >= limit) res++, trace.push_back(i);
cout << res << "\n";
for (int i : trace) cout << i << " ";
return 0;
}
// Lak lu theo dieu nhac ^.^
Compilation message (stderr)
railway.cpp: In function 'int main()':
railway.cpp:8:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define FOR(i, a, b) for (int i = (a); i <= (b); i++)
| ^
railway.cpp:112:9: note: in expansion of macro 'FOR'
112 | FOR(j, 0, query[i].size() - 1) {
| ^~~
railway.cpp:8:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define FOR(i, a, b) for (int i = (a); i <= (b); i++)
| ^
railway.cpp:120:9: note: in expansion of macro 'FOR'
120 | FOR(j, 1, nodes.size() - 1) {
| ^~~
railway.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | 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... |