이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXLOG = 18;
int par[100005][MAXLOG+1];
vector <pair <int, int>> graf[100005];
int depth[100005];
int in[100005];
int out[100005];
int x[100005];
int pre[100005];
vector <int> res;
int tajm;
void dfs_pre(int v, int parent){
depth[v] = depth[parent] + 1;
par[v][0] = parent;
for(int j=1; j<MAXLOG; j++){
par[v][j] = par[par[v][j-1]][j-1];
}
in[v] = ++tajm;
for(auto d : graf[v]){
int c = d.first;
if(c != parent) dfs_pre(c, v);
}
out[v] = tajm;
}
bool is_parent(int a, int b){
return in[a] <= in[b] && out[b] <= out[a];
}
bool cmp(int a, int b){
return in[a] < in[b];
}
int k;
int lca(int a, int b){
if(is_parent(a, b)) return a;
if(is_parent(b, a)) return b;
for(int j=MAXLOG-1; j>=0; j--){
if(!is_parent(par[a][j], b)) a = par[a][j];
}
return par[a][0];
}
int dfs(int v){
int tren = 0;
int koji = 0;
for(auto c : graf[v]){
if(c.first != par[v][0]) tren += dfs(c.first);
else koji = c.second;
}
tren += pre[v];
if(tren >= k) res.push_back(koji);
return tren;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
out[0] = 2e5;
int n, m;
cin >> n >> m >> k;
for(int i=1; i<n; i++){
int a, b;
cin >> a >> b;
graf[a].push_back({b, i});
graf[b].push_back({a, i});
}
dfs_pre(1, 0);
for(int i=1; i<=m; i++){
int len;
cin >> len;
for(int j=1; j<=len; j++){
cin >> x[j];
}
sort(x+1, x+1+len, cmp);
int mn = n+5;
int v = x[len];
int kolko = 0;
vector <int> sad;
for(int j=len; j>=1; j--){
v = lca(v, x[j]);
if(out[x[j]] < mn){
pre[x[j]]++;
kolko++;
mn = out[x[j]];
sad.push_back(x[j]);
}
}
for(int j=0; j<sad.size()-1; j++){
pre[lca(sad[j], sad[j+1])]--;
}
pre[v]--;
}
dfs(1);
sort(res.begin(), res.end());
cout << res.size() << "\n";
for(auto c : res) cout << c << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'int main()':
railway.cpp:101:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int j=0; j<sad.size()-1; j++){
| ~^~~~~~~~~~~~~
# | 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... |