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 int long long
int n, m, k;
const int MXN = 300005;
vector<int> adj[MXN];
int st_start[MXN];
int st_end[MXN];
int at = 0;
int adder[MXN];
void dfs_tr(int node, int last) {
st_start[node] = at;
at++;
for (int i=0; i<adj[node].size(); i++) {
int x = adj[node][i];
if (x == last) continue;
dfs_tr(x, node);
}
st_end[node] = at;
at++;
}
const int MXK = 20;
int depth[MXN];
int par[MXN][MXK];
void dfs_dep(int node, int last, int dep) {
par[node][0] = last;
depth[node] = dep;
for (int i=1; i<MXK; i++) {
int p = par[node][i-1];
if (p == -1) continue;
par[node][i] = par[p][i-1];
}
for (int i=0; i<adj[node].size(); i++) {
int o = adj[node][i];
if (o == last) continue;
dfs_dep(o, node, dep+1);
}
}
int kth_parent(int n, int k) {
for (int i=0; i<MXK; i++) {
if (k & (1 << i)) {
n = par[n][i];
}
}
return n;
}
int lca(int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
b = kth_parent(b, depth[b]-depth[a]);
// cout << a << ' ' << b << endl;
if (a == b) return a;
for (int i=MXK-1; i>=0; i--) {
if (par[a][i] != par[b][i]) {
a = par[a][i];
b = par[b][i];
}
}
return par[a][0];
}
bool byt(int a, int b) {
return st_start[a] < st_start[b];
}
int delta[MXN];
vector<int> chosen;
int att[MXN];
int dfs_rec(int node, int last) {
int ans = delta[node];
for (int i=0; i<adj[node].size(); i++) {
int ad = adj[node][i];
if (ad == last) continue;
ans += dfs_rec(ad, node);
}
return att[node] = ans;
}
signed main() {
for (int i=0; i<MXN; i++) for (int j=0; j<MXK; j++) par[i][j] = -1;
cin >> n >> m >> k;
vector<pair<int, int> > ed;
for (int i=1; i<n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
ed.push_back(make_pair(a, b));
}
dfs_tr(1, -1);
dfs_dep(1, -1, 0);
int above[n+1];
for (int i=0; i<n-1; i++) {
int a = ed[i].first, b = ed[i].second;
if (depth[a] > depth[b]) above[a] = i+1;
else above[b] = i+1;
}
// for (int i=1; i<=n; i++) cout << i << ' ' << st_start[i] << ' ' << st_end[i] << endl;
for (int T=0; T<m; T++) {
int nm;
cin >> nm;
int arr[nm];
for (int i=0; i<nm; i++) cin >> arr[i];
sort(arr, arr+nm, byt);
// for (int i=0; i<nm; i++) cout << arr[i] << ' '; cout << endl;
vector<pair<int, int> > ranges;
for (int i=0; i<nm; i++) {
int a = arr[i], b = arr[(i+1)%nm];
int l = lca(a, b);
// cout << a << ' ' << b << ' ' << l << endl;
delta[l] -= 2;
delta[a]++;
delta[b]++;
}
}
dfs_rec(1, -1);
vector<int> f;
for (int i=1; i<=n; i++) {
if (att[i] >= 2 * k) {
f.push_back(above[i]);
}
}
sort(f.begin(), f.end());
cout << f.size() << endl;
for (int i=0; i<f.size(); i++) cout << f[i] << ' ';
cout << endl;
}
Compilation message (stderr)
railway.cpp: In function 'void dfs_tr(int, int)':
railway.cpp:18:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<adj[node].size(); i++) {
~^~~~~~~~~~~~~~~~~
railway.cpp: In function 'void dfs_dep(int, int, int)':
railway.cpp:39:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<adj[node].size(); i++) {
~^~~~~~~~~~~~~~~~~
railway.cpp: In function 'int dfs_rec(int, int)':
railway.cpp:84:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<adj[node].size(); i++) {
~^~~~~~~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:161:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<f.size(); i++) cout << f[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... |