이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int LG = log2(N) + 10;
vector<int> adj[N];
int n,m,k,l, timer = 0;
vector<pair<int,int>> edges;
int lca_util[N][LG];
int tin[N],tout[N];
int dist[N];
int ans[N];
void dfs(int i,int p){
tin[i] = ++timer;
lca_util[i][0] = p;
for(int j = 1;j<=l;j++){
lca_util[i][j] = lca_util[lca_util[i][j - 1]][j - 1];
}
for(int e : adj[i]){
if(e != p){
dfs(e,i);
}
}
tout[i] = ++timer;
}
bool is_ancestor(int i,int j){
return (tin[i] <= tin[j] && tout[i] >= tin[j]);
}
int lca(int u, int v){
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = l; i >= 0; --i) {
if (!is_ancestor(lca_util[u][i], v))
u = lca_util[u][i];
}
return lca_util[u][0];
}
void dfs2(int i,int p){
for(int e : adj[i]){
if(e != p){
dist[e] = dist[i] + 1;
dfs2(e,i);
ans[i] += ans[e];
}
}
}
signed main(){
iostream::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
l = ceil(log2(n)) + 1;
for(int i = 0;i<n-1;i++){
int a,b;cin >> a >> b;
a--;b--;
edges.push_back({a,b});
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0,0);
for(int __ = 0;__ < m;__++){
int siz;cin >> siz;
vector<int> vec(siz);
for(int &e : vec){
cin >> e;
e--;
}
sort(vec.begin(),vec.end(),[&](int i,int j){
return tin[i] < tin[j];
});
for(int i = 0;i<vec.size();i++){
int ll = lca(vec[i], vec[(i + 1)%siz]);
// cout << vec[i] << " " << vec[(i + 1)%siz] << " " << ll << "\n";
ans[vec[i]]++;
ans[vec[(i + 1)%siz]]++;
ans[ll] -= 2;
}
}
dist[0] = 0;
dfs2(0,0);
// for(int i = 0;i<n;i++)
// cout << ans[i] << " ";
// cout << "\n";
vector<int> temp;
for(int i = 0;i<n - 1;i++){
int asd = edges[i].first;
if(dist[asd] < dist[edges[i].second])
asd =edges[i].second;
if(ans[asd] >= 2*k)
temp.push_back(i);
}
cout << temp.size() << "\n";
for(int &e : temp)
cout << e + 1 << " ";
cout << "\n";
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i = 0;i<vec.size();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... |