이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5 + 5, M = 2e5 + 5;
vector<int> g[N], vt_g[N];
pair<int,int> Edge[N];
int tin[N], tout[N], idx[N], d[N], V[M], E[M], st[M], T[M], ST[M][20], m, mm, sz;
bool anc(int u, int v){ return tin[u] <= tin[v] && tout[v] <= tout[u];}
void dfs(int u){
E[m++] = u;
for(int v: g[u]){
g[v].erase(find(g[v].begin(), g[v].end(), u)); dfs(v); E[m++] = u;
}
}
bool cmp(int u, int v){ return tin[u] < tin[v];}
void efs(int u){
for(int v: vt_g[u]){
++d[v]; --d[u];
efs(v);
}
}
void ffs(int u){
for(int v: g[u]){
ffs(v); d[u] += d[v];
}
}
int lca(int u, int v){
if(tin[u] > tin[v]) swap(u, v);
int j = 31 - __builtin_clz(tin[v] - tin[u] + 1);
return T[min(ST[tin[u]][j], ST[tin[v]-(1<<j)+1][j])];
}
signed main(){
int n, q, k; scanf("%d %d %d", &n, &q, &k);
for(int i = 0, u, v; i<n-1; ++i){
scanf("%d %d", &u, &v); --u; --v;
g[u].push_back(v); g[v].push_back(u);
Edge[i] = {u, v};
}
dfs(0);
for(int i = m-1; i>=0; --i) tin[E[i]] = i, T[i] = E[i];
for(int i = 0; i<m; ++i) ST[i][0] = tin[E[i]], tout[E[i]] = i;
for(int j = 1; j<32 - __builtin_clz(m); ++j) for(int i = 0; i+(1<<j)-1<m; ++i) ST[i][j] = min(ST[i][j-1], ST[i+(1<<(j-1))][j-1]);
for(int i = 0; i<n-1; ++i){
int u = Edge[i].first, v = Edge[i].second;
if(tin[u] > tin[v]) swap(u, v);
idx[v] = i;
}
for(int j = 0; j<q; ++j){
scanf("%d", &m); mm = m;
for(int i = 0; i<m; ++i) scanf("%d", V+i), --V[i];
sort(V, V+m, cmp);
for(int i = 1; i<m; ++i) V[mm++] = lca(V[i], V[i-1]);
m = mm; sort(V, V+m, cmp); m = unique(V, V+m) - V;
sz = 0;
st[sz++] = V[0];
for(int i = 1; i<m; ++i){
for(; sz > 1 && !anc(st[sz-1], V[i]); --sz) vt_g[st[sz-2]].push_back(st[sz-1]);
st[sz++] = V[i];
}
for(; sz > 1; --sz) vt_g[st[sz-2]].push_back(st[sz-1]);
efs(V[0]);
for(int i = 0; i<m; ++i) vt_g[V[i]].clear();
}
ffs(0);
sz = 0;
for(int i = 0; i<n; ++i) if(d[i] >= k) V[sz++] = idx[i];
sort(V, V+sz);
printf("%d\n", sz);
for(int i = 0; i<sz; ++i) printf("%d ", V[i] + 1);
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:34:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | int n, q, k; scanf("%d %d %d", &n, &q, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:36:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d %d", &u, &v); --u; --v;
| ~~~~~^~~~~~~~~~~~~~~~~
railway.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d", &m); mm = m;
| ~~~~~^~~~~~~~~~
railway.cpp:51:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | for(int i = 0; i<m; ++i) scanf("%d", V+i), --V[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... |