이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define F first
#define S second
const int N = 300000 + 5;
const int LOG = 30;
int n, m, k;
vector < pair <int, int> > g[N];
int par[LOG][N], h[N];
vector <int> ans;
set <int> st[N];
vector <int> out[N];
void dfs(int v, int pr){
for (auto it : g[v]){
int u = it.F;
if (u == pr) continue;
par[0][u] = v;
h[u] = h[v] + 1;
dfs(u, v);
}
return;
}
inline void pre(){
for (int i = 1; i < LOG; i ++){
for (int v = 1; v <= n; v ++){
par[i][v] = par[i - 1][par[i - 1][v]];
}
}
return;
}
inline int get_par(int v, int dif){
for (int i = 0; i < LOG; i ++){
if ((dif >> i & 1)){
v = par[i][v];
}
}
return v;
}
inline int LCA(int u, int v){
if (h[v] < h[u]) swap(u, v);
v = get_par(v, h[v] - h[u]);
if (u == v) return v;
for (int i = LOG - 1; i >= 0; i --){
if (par[i][u] != par[i][v]){
u = par[i][u];
v = par[i][v];
}
}
return par[0][v];
}
inline void merge(int pr, int v){
if ((int)st[pr].size() < (int)st[v].size()){
swap(st[pr], st[v]);
}
for (int col : st[v]){
st[pr].insert(col);
}
st[v].clear();
return;
}
inline void del(int v){
for (int col : out[v]){
st[v].erase(st[v].lower_bound(col));
}
return;
}
void dfsCalc(int v, int pr){
for (auto it : g[v]){
int u = it.F;
int id = it.S;
if (id == pr) continue;
dfsCalc(u, id);
merge(v, u);
}
del(v);
if ((v != 1) && (k <= (int)st[v].size())) ans.pb(pr);
return;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0); cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i < n; i ++){
int u, v;
cin >> u >> v;
g[u].pb({v, i}); g[v].pb({u, i});
}
dfs(1, 0); pre();
for (int i = 1; i <= m; i ++){
int sz, v, lca = 0;
cin >> sz;
vector <int> C = {};
for (int j = 0; j < sz; j ++){
int v;
cin >> v;
C.pb(v);
if (lca == 0){
lca = C[j];
}
else {
lca = LCA(lca, C[j]);
}
}
out[lca].pb(i);
for (int u: C){
st[u].insert(i);
}
//cout << "dbg " << i << ' ' << lca << '\n';
}
dfsCalc(1, 0);
sort(ans.begin(), ans.end());
cout << (int)ans.size() << '\n';
for (int i : ans) cout << i << ' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:106:17: warning: unused variable 'v' [-Wunused-variable]
106 | int sz, v, lca = 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... |