이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rep(n) for (int i = 0; i < n; ++i)
#define rep2(n) for (int j = 0; j < n; ++j)
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int mxn = 2e5;
vii neibs[mxn];
int n, k, ord[mxn], d[mxn], st[mxn][14], cnt[mxn], o;
vi nodes_ids[mxn], ans;
bool compare (int a, int b) {
if (ord[a]<ord[b]) return 1;
return 0;
}
void dfs (int u, int prev, int depth) {
st[u][0] = prev, d[u] = depth, ord[u] = o++;
for (ii v : neibs[u]) if (v.fi!=prev) dfs(v.fi,u,depth+1);
}
void complete_st () {
for (int i = 1; i <= 13; ++i)
for (int j = 1; j <= n; ++j)
st[j][i] = st[st[j][i-1]][i-1];
}
int find_lca (int a, int b) {
if (d[a]>d[b]) return find_lca(b,a);
int dif = d[b]-d[a], step = 0;
while (dif>0) {
if (dif%2) b = st[b][step];
dif/=2, step++;
}
if (a==b) return a;
for (int i = 13; i >= 0; --i) {
int anc_a = st[a][i], anc_b = st[b][i];
if (anc_a!=anc_b) a = anc_a, b = anc_b;
}
return (st[a][0]);
}
int solve (int u, int prev) {
int sum = 0;
for (ii vid : neibs[u]) {
int v = vid.fi, id = vid.se;
if (v!=prev) {
int get = solve(v, u);
if (get>=k) ans.pb(id);
sum += get;
}
}
return sum + cnt[u];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int m;
cin >> n >> m >> k;
rep (n-1) {
int u, v; cin >> u >> v;
neibs[u].pb({v,i+1});
neibs[v].pb({u,i+1});
}
dfs (1,1,0);
complete_st ();
rep (m) {
int s; cin >> s;
int nodes[s];
rep2 (s) cin >> nodes[j];
sort(nodes, nodes+s, compare);
rep2 (s) {
cnt[find_lca(nodes[j], nodes[(j+1)%s])]--;
cnt[nodes[j]]++;
}
}
solve (1, 1);
cout << ans.size() << "\n";
sort(ans.begin(), ans.end());
for (int a : ans) cout << a << " ";
cout << "\n";
}
# | 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... |