Submission #1106836

#TimeUsernameProblemLanguageResultExecution timeMemory
1106836luvnaRailway (BOI17_railway)C++14
7 / 100
60 ms39748 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e5 + 15;
const int lg = 17;

int n, m, k;
vector<pii> g[N];
int par[N][lg+1];
int tin[N], tout[N], timerDFS;
int h[N];
int idx[N];

void dfs(int u){
    tin[u] = ++timerDFS;
    for(auto [v, id] : g[u]) if(v != par[u][0]){
        par[v][0] = u;
        h[v] = h[u] + 1;
        idx[v] = id;
        dfs(v);
        for(int j = 1; j <= lg; j++) par[v][j] = par[par[v][j-1]][j-1];
    }
    tout[u] = timerDFS;
}

bool inside(int u, int v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int LCA(int u, int v){
    if(inside(u,v)) return u;
    if(inside(v,u)) return v;
    for(int j = lg; j >= 0; j--){
        if(h[u] >= (1 << j) && !inside(par[u][j], v)) u = par[u][j];
    }
    return par[u][0];
}

bool cmp(int a, int b){
    return tin[a] < tin[b];
}

stack<int> st;
int cnt[N];

void solve_query(){
    int k; cin >> k;
    vector<int> vertices;
    for(int i = 1; i <= k; i++){
        int x; cin >> x;
        vertices.pub(x);
    }
    sort(all(vertices), cmp);
    for(int i = 0; i < k-1; i++) vertices.pub(LCA(vertices[i], vertices[i+1]));
    sort(all(vertices), cmp);
    compact(vertices);
    st.push(vertices[0]);
    for(int i = 1; i < sz(vertices); i++){
        while(!st.empty() && !inside(st.top(), vertices[i])) st.pop();
        cnt[vertices[i]]++;
        cnt[st.top()]--;
        st.push(vertices[i]);
    }

    while(!st.empty()) st.pop();
}

vector<int> res;

void calc(int u){
    for(auto [v, id] : g[u]) if(v != par[u][0]){
        calc(v);
        cnt[u] += cnt[v];
    }
    if(cnt[u] >= k) res.pub(idx[u]);
}

void solve(){
    cin >> n >> m >> k;

    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].pub(pii(v,i));
        g[v].pub(pii(u,i));
    }

    dfs(1);

    while(m--) solve_query();

    calc(1);

    cout << sz(res) << endl;

    sort(all(res));

    for(int x : res) cout << x << " ";
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

railway.cpp: In function 'void dfs(int)':
railway.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |     for(auto [v, id] : g[u]) if(v != par[u][0]){
      |              ^
railway.cpp: In function 'void calc(int)':
railway.cpp:91:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |     for(auto [v, id] : g[u]) if(v != par[u][0]){
      |              ^
railway.cpp: In function 'int main()':
railway.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(task".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...