이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define COPYRIGHT CODE BY TRINH TUAN NGHIA
#include<bits/stdc++.h>
#define Boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long
#define endl "\n"
#define st first
#define nd second
#define ii pair <ll,ll>
#define iii pair <ll,ii>
#define iiii pair <ii,ii>
#define pb push_back
#define NAME "railway"
using namespace std;
const int N = 1e5 + 10;
int n, m, k;
vector<pair<int,int>> adj[N];
ii canh[N];
int in[N], out[N], nchild[N], h[N], TT;
int st[N][20];
int LG;
vector<int> adj2[N];
vector<int> resv;
int f[N];
int par[N];
int rev[N];
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});
canh[i] = {u, v};
}
LG = __lg(n);
}
void pre(int u, int p){
if (u == 1)h[u] = 1;
in[u] = ++TT;
for (int i = 1; i <= LG; ++i){
st[u][i] = st[st[u][i - 1]][i - 1];
}
for (auto x : adj[u]){
int v = x.st;
if (v == p)continue;
h[v] = h[u] + 1;
st[v][0] = u;
par[v] = u;
rev[v] = x.nd;
pre(v, u);
}
out[u] = TT;
}
int lca(int u, int v){
if (h[u] < h[v])swap(u, v);
for (int i = LG; i >= 0; --i){
if (h[st[u][i]] >= h[v])u = st[u][i];
}
if (u == v)return u;
for (int i = LG; i >= 0; --i){
if (st[u][i] != st[v][i]){
u = st[u][i], v = st[v][i];
}
}
return st[u][0];
}
bool isdad(int u, int v){
if (in[u] <= in[v] and out[u] >= out[v])return true;
return false;
}
void reset(){
}
void buildVtree(vector<int> V){
sort(V.begin(),V.end(), [&] (int a, int b){
return in[a] < in[b];
});
vector<int> V2 = V;
for (int i = 1; i < V.size(); ++i){
V2.pb(lca(V[i], V[i - 1]));
}
sort(V2.begin(),V2.end(), [&] (int a, int b){
return in[a] < in[b];
});
stack<int> st;
for (int i = 0; i < V2.size(); ++i){
if (i > 0 and V2[i] == V2[i - 1])continue;
while (!st.empty() and !isdad(st.top(), V2[i])){
st.pop();
}
if (!st.empty()){
f[st.top()] --;
f[V2[i]] ++;
// cout << "Dcm " << V2[i] << " " << st.top() << endl;
}
st.push(V2[i]);
}
}
void DFS(int u, int p){
for (auto x : adj[u]){
int v= x.st;
if (v == p)continue;
DFS(v, u);
f[u] += f[v];
// cout << "cmm " << f[4] << endl;
}
}
void solve(){
pre(1, 0);
for (int tv = 1; tv <= m; ++tv){
int s;
cin >> s;
vector<int> V;
for (int i = 1; i <= s; ++i){
int x;
cin >> x;
V.pb(x);
}
buildVtree(V);
}
DFS(1, 0);
for (int i =2 ; i <= n; ++i){
// cout << i << " " << f[i] << endl;
if (f[i] >= k){
resv.pb(rev[i]);
}
}
cout << resv.size() << endl;
sort(resv.begin(), resv.end());
for (auto x : resv)cout << x << " ";
// cout << "cmm " << f[6] << endl
}
signed main (){
if (fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
Boost;
inp();
solve();
}
/*
input
*/
/*
output
*/
컴파일 시 표준 에러 (stderr) 메시지
railway.cpp: In function 'void buildVtree(std::vector<int>)':
railway.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 1; i < V.size(); ++i){
| ~~^~~~~~~~~~
railway.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < V2.size(); ++i){
| ~~^~~~~~~~~~~
railway.cpp: In function 'int main()':
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".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(NAME".out", "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... |