Submission #379017

#TimeUsernameProblemLanguageResultExecution timeMemory
379017jeroenodbRailway (BOI17_railway)C++14
100 / 100
123 ms30052 KiB
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;

int pref[mxN], in[mxN];

struct DSU{
    vi sz,parent, highest;
    DSU(int n) {
        sz.assign(n,1);
        parent.resize(n);
        iota(all(parent),0);
        highest.resize(n);
        iota(all(highest),0);
    }
    void link(int a, int b) {
        if(sz[a]<sz[b]) {
            swap(a,b);
        }
        sz[a] = max(sz[a],sz[b]+1);
        parent[b] = a;
        if(in[highest[a]]>in[highest[b]]) {
            highest[a] = highest[b];
        }
    }
    int high(int a){
        return highest[find(a)];
    }
    void unite(int a, int b) {
        int pa = find(a),pb = find(b);
        if(pa!=pb) link(pa,pb);
    }
    int find(int a) {
        return a==parent[a]?a:parent[a] = find(parent[a]);
    }
};

vector<pi> adj[mxN];
int cnt = 0;
void dfs(int at=0, int from=-1) {
    in[at] = cnt++;
    for(auto [to,haha]: adj[at]) {
        if(to==from) continue;
        dfs(to,at);
    }
}

vi query[mxN];
void decrementlca(int a, int b) {
    query[a].push_back(b);
    query[b].push_back(a);
}

bitset<mxN> visited;
int k;
vi ans;
void lcadfs(int at, int from, DSU& dsu) {
    visited[at]=true;
    for(auto other : query[at]) {
        if(visited[other]) {
            // Offline LCA algorithm from: https://usaco.guide/CPH.pdf#page=181
            int lca = dsu.high(other);
            pref[lca]--;
        }
    }
    for(auto [to,id]: adj[at]) {
        if(to==from) continue;
        lcadfs(to,at,dsu);
        if(pref[to]>=k) {
            ans.push_back(id+1);
        }
        pref[at]+=pref[to];
    }
    dsu.unite(at,from);
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m; cin >> n >> m >> k;

    for(int i=0;i<n-1;++i) {
        int a,b; cin >> a >> b;
        --a,--b;
        adj[a].push_back({b,i});
        adj[b].push_back({a,i});
    }
    dfs();

    for(int official=0;official<m;++official) {
        int s; cin >> s;
        vi nes(s);
        for(int& i: nes) {
            cin >>i; --i; 
            pref[i]++;
        }
        sort(all(nes), [&](int a, int b){return in[a]>in[b];});
        for(int i=1;i<s;++i) {
            decrementlca(nes[i-1],nes[i]);
        }
        decrementlca(nes[0],nes.back());
    }

    DSU dsu(n);
    lcadfs(0,0,dsu);
    sort(all(ans));
    cout << ans.size() << '\n';
    cout << ans << '\n';
}

Compilation message (stderr)

railway.cpp: In function 'void dfs(int, int)':
railway.cpp:50:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |     for(auto [to,haha]: adj[at]) {
      |              ^
railway.cpp: In function 'void lcadfs(int, int, DSU&)':
railway.cpp:74:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |     for(auto [to,id]: adj[at]) {
      |              ^
#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...