Submission #1344038

#TimeUsernameProblemLanguageResultExecution timeMemory
1344038kawhietRailway (BOI17_railway)C++20
36 / 100
76 ms22180 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

constexpr int N = 1e5;
constexpr int M = 20;

vector<pair<int, int>> g[N];
int lvl[N], to[N][M], t[N], tin[N], timer = 0;

void dfs_lvl(int u, int p) {
    tin[u] = timer++;
    to[u][0] = p;
    for (int i = 1; i < M; i++) {
        to[u][i] = to[to[u][i - 1]][i - 1];
    }
    for (auto [v, _] : g[u]) {
        if (v == p) continue;
        lvl[v] = lvl[u] + 1;
        dfs_lvl(v, u);
    }
}

int lca(int u, int v) {
    if (lvl[u] < lvl[v]) swap(u, v);
    int diff = lvl[u] - lvl[v];
    for (int i = 0; i < M; i++) {
        if (diff & (1 << i)) {
            u = to[u][i];
        }
    }
    if (u == v) return u;
    for (int i = M - 1; i >= 0; i--) {
        if (to[u][i] != to[v][i]) {
            u = to[u][i];
            v = to[v][i];
        }
    }
    return to[u][0];
}

void update(int u, int v) {
    t[u]++;
    t[v]++;
    t[lca(u, v)] -= 2;
}

void dfs(int u, int p) {
    for (auto [v, _] : g[u]) {
        if (v == p) continue;
        dfs(v, u);
        t[u] += t[v];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back({v, i});
        g[v].push_back({u, i});
    }
    dfs_lvl(0, 0);
    for (int i = 0; i < m; i++) {
        int s;
        cin >> s;
        vector<int> a(s);
        for (int j = 0; j < s; j++) {
            cin >> a[j];
            a[j]--;
        }
        ranges::sort(a, [&](int x, int y) {
            return tin[x] < tin[y];
        });
        for (int j = 0; j < s; j++) {
            int x = (j + 1) % s;
            update(a[j], a[x]);
        }
    }
    dfs(0, 0);
    vector<int> ans;
    for (int i = 0; i < n; i++) {
        if (t[i] >= 2 * k) {
            for (auto [j, pos] : g[i]) {
                if (j == to[i][0]) {
                    ans.push_back(pos);
                }
            }
        }
    }
    cout << ans.size() << '\n';
    for (auto i : ans) {
        cout << i + 1 << ' ';
    }
    cout << '\n';
    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...