이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
#define vec vector
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
vec<vec<pii> > graph;
vec<int> euler, in, out;
vec<int> depth;
vec<int> edges;
vec<vec<int> > up;
int timer = 0;
void resize(int n) {
in.resize(n+1);
out.resize(n+1);
graph.resize(n+1);
depth.resize(n+1);
up.resize(n+1, vec<int>(LOG));
edges.resize(n+1);
}
void dfs(int u, int p) {
euler.pb(u);
in[u] = ++timer;
for(int i=1; i<LOG; i++)
up[u][i] = up[ up[u][i-1] ][i-1];
for(pii &v : graph[u]) {
if(v.first == p) continue;
depth[v.first] = depth[u] + 1;
up[v.first][0] = u;
edges[v.first] = v.second;
dfs(v.first, u);
}
euler.pb(u);
out[u] = timer;
}
int jmp(int x, int d) {
for(int j=LOG-1; j>=0; j--)
if(d & (1 << j)) x = up[x][j];
return x;
}
int get_lca(int a, int b) {
if(depth[a] < depth[b]) swap(a, b);
a = jmp(a, depth[a] - depth[b]);
if(a == b) return a;
for(int j=LOG-1; j>=0; j--)
if(up[a][j] != up[b][j])
a = up[a][j], b = up[b][j];
return up[a][0];
}
bool cmp(int a, int b) {
return (in[a] < in[b]);
}
struct BIT {
int n;
vector<ll> tree;
BIT(int n) : n(n+5), tree(n+5) {}
void update(int p, int v = 1) {
for(p++; p<n; p+=p&-p) tree[p] += v;
}
ll query(int p) {
ll ans = 0;
for(p++; p>0; p-=p&-p) ans += tree[p];
return ans;
}
ll query(int a, int b) {
return query(b) - query(a);
}
};
int32_t main() {
setIO();
int n, m, k;
cin >> n >> m >> k;
resize(n);
for(int i=0; i<n-1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back({ b, i });
graph[b].push_back({ a, i });
}
dfs(1, 0);
BIT bit(n+1);
while(m--) {
int x;
cin >> x;
vector<int> list(x);
for(int i=0; i<x; i++)
cin >> list[i];
sort(list.begin(), list.end() - 1, cmp);
list.pb(list[0]);
for(int i=0; i<x; i++) {
int lca = get_lca(list[i], list[i+1]);
bit.update(in[list[i]]);
bit.update(in[list[i+1]]);
bit.update(in[lca], -2);
}
}
vec<int> ans;
for(int i=2; i<=n; i++)
if(bit.query(in[i], out[i]) >= 2 * k)
ans.pb(edges[i]);
cout << sz(ans) << '\n';
sort(all(ans));
for(int &x : ans) cout << x + 1 << ' ';
return 0;
}
# | 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... |