Submission #1086699

#TimeUsernameProblemLanguageResultExecution timeMemory
1086699nghia0912Railway (BOI17_railway)C++17
100 / 100
85 ms30668 KiB
#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

*/

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...