이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 1e5 + 1;
const string NAME = "";
int n, m, k;
vector<ii> adj[N];
int in[N], out[N], cnt, euler[20][2 * N], lg[2 * N];
int sum[N];
vector<int> ans;
int min_by_time(int u, int v){
return (in[u] < in[v] ? u : v);
}
int LCA(int u, int v){
int L = min(in[u], in[v]);
int R = max(out[u], out[v]);
int k = lg[R - L + 1];
return min_by_time(euler[k][L], euler[k][R - (1 << k) + 1]);
}
void dfs(int u = 1, int p = 0){
in[u] = ++cnt;
euler[0][cnt] = u;
for(auto [v, i] : adj[u]){
if(v == p)
continue;
dfs(v, u);
euler[0][++cnt] = u;
}
out[u] = cnt;
}
void DFS(int u = 1, int p = 0){
for(auto [v, i] : adj[u]){
if(v == p)
continue;
DFS(v, u);
if(sum[v] >= k)
ans.pb(i);
sum[u] += sum[v];
}
}
void init(){
dfs();
for(int i = 2; i <= cnt; ++i)
lg[i] = lg[i / 2] + 1;
for(int l = 1; l <= lg[cnt]; ++l)
for(int i = 1; i <= cnt - (1 << l) + 1; ++i)
euler[l][i] = min_by_time(euler[l - 1][i], euler[l - 1][i + (1 << (l - 1))]);
}
bool isAncestor(int u, int v){
return (in[u] <= in[v] && in[v] <= out[u]);
}
void build_aux(vector<int> &vec){
sort(vec.begin(), vec.end(), [&](int u, int v){
return in[u] < in[v];
});
int z = vec.size();
for(int i = 1; i < z; ++i)
vec.pb(LCA(vec[i - 1], vec[i]));
sort(vec.begin(), vec.end(), [&](int u, int v){
return in[u] < in[v];
});
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
stack<int> stk;
stk.push(vec[0]);
for(int i = 1; i < vec.size(); ++i){
while(!stk.empty() && !isAncestor(stk.top(), vec[i]))
stk.pop();
if(!stk.empty())
++sum[vec[i]], --sum[stk.top()];
stk.push(vec[i]);
}
}
void inp(){
cin >> n >> m >> k;
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb({v, i});
adj[v].pb({u, i});
}
}
void solve(){
init();
while(m--){
int s;
cin >> s;
vector<int> vec;
for(int i = 1; i <= s; ++i){
int x;
cin >> x;
vec.pb(x);
}
build_aux(vec);
}
DFS();
sort(ans.begin(), ans.end());
ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
cout << ans.size() << "\n";
for(int i : ans)
cout << i << " ";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'void build_aux(std::vector<int>&)':
railway.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 1; i < vec.size(); ++i){
| ~~^~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen((NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |