Submission #135786

#TimeUsernameProblemLanguageResultExecution timeMemory
135786popovicirobertRailway (BOI17_railway)C++14
100 / 100
184 ms32120 KiB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44


/*const int MOD = (int) 1e9 + 7;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}*/

/*int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}*/

using namespace std;

const int MAXN = (int) 1e5;

vector < pair <int, int> > g[MAXN + 1];
int idl[MAXN + 1], idr[MAXN + 1], id;

int rmq[20][2 * MAXN + 1], szr;
int lvl[MAXN + 1], first[MAXN + 1];

void dfs(int nod, int par) {
    lvl[nod] = lvl[par] + 1;
    rmq[0][++szr] = nod; first[nod] = szr;

    idl[nod] = ++id;
    for(auto it : g[nod]) {
        if(it.first != par) {
            dfs(it.first, nod);
            rmq[0][++szr] = nod;
        }
    }
    idr[nod] = id;
}

char lg[2 * MAXN + 1];

inline void prec() {
    int i;
    for(i = 2; i <= szr; i++) {
        lg[i] = lg[i >> 1] + 1;
    }
    for(int bit = 1; (1 << bit) <= szr; bit++) {
        for(i = 1; i + (1 << bit) <= szr + 1; i++) {
            int a = rmq[bit - 1][i], b = rmq[bit - 1][i + (1 << (bit - 1))];
            rmq[bit][i] = (lvl[a] < lvl[b] ? a : b);
        }
    }
}

inline int get_lca(int x, int y) {
    x = first[x], y = first[y];
    if(x > y) {
        swap(x, y);
    }
    int bit = lg[y - x + 1];
    int a = rmq[bit][x], b = rmq[bit][y - (1 << bit) + 1];
    return (lvl[a] < lvl[b] ? a : b);
}

inline bool in(int x, int y) {
    return idl[x] <= idl[y] && idr[y] <= idr[x];
}

int sp[MAXN + 1];
vector <int> sol;

void dfs_sp(int nod, int par, int k) {
    for(auto it : g[nod]) {
        if(it.first != par) {
            dfs_sp(it.first, nod, k);
            sp[nod] += sp[it.first];
            if(sp[it.first] >= k) {
                sol.push_back(it.second);
            }
        }
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, m, k;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m >> k;

    for(i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }

    dfs(1, 0);
    prec();

    while(m--) {

        int sz;
        cin >> sz;

        vector <int> nodes;
        for(i = 0; i < sz; i++) {
            int nod;
            cin >> nod;
            nodes.push_back(nod);
        }

        sort(nodes.begin(), nodes.end(), [&](const int &x, const int &y) {

                    return idl[x] < idl[y];

             });

        for(i = 0; i + 1 < sz; i++) {
            nodes.push_back(get_lca(nodes[i], nodes[i + 1]));
        }

        sort(nodes.begin(), nodes.end(), [&](const int &x, const int &y) {

                    return idl[x] < idl[y];

             });

        nodes.resize(unique(nodes.begin(), nodes.end()) - nodes.begin());

        stack <int> stk;
        for(auto nod : nodes) {
            while(stk.size() && !in(stk.top(), nod)) {
                stk.pop();
            }
            if(stk.size()) {
                sp[nod]++;
                sp[stk.top()]--;
            }
            stk.push(nod);
        }
    }

    dfs_sp(1, 0, k);

    cout << sol.size() << "\n";

    sort(sol.begin(), sol.end());
    for(auto it : sol) {
        cout << it << " ";
    }

    return 0;
}
#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...