제출 #567392

#제출 시각아이디문제언어결과실행 시간메모리
567392SavicSRailway (BOI17_railway)C++17
100 / 100
234 ms48956 KiB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ldb;
 
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;

#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
 
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 100005; 

int n, q, k;

vector<pii> g[mxN];


int timer = 0;
int in[mxN];
int out[mxN];
int deda[mxN][20];
void dfs1(int v, int p){
    in[v] = ++timer;
    deda[v][0] = p; ff(i,1,19)deda[v][i] = deda[deda[v][i - 1]][i - 1];
    for(auto c : g[v]){
        if(c.fi != p){
            dfs1(c.fi, v);
        }
    }
    out[v] = timer;
}

bool predak(int x, int y){
    return in[x] <= in[y] && in[y] <= out[x];
}

int LCA(int x, int y){
    if(predak(x, y))return x;
    if(predak(y, x))return y;
    fb(i,19,0)if(deda[x][i] != 0 && !predak(deda[x][i], y))x = deda[x][i];
    return deda[x][0]; 
}

int A[mxN];
bool cmp(int s1, int s2){
    return in[s1] < in[s2]; 
}

vector<int> add[mxN];
vector<int> rem[mxN];

map<int,int> dt[mxN];
void comb(int x, int y){
    if(sz(dt[x]) < sz(dt[y]))swap(dt[x], dt[y]);
    for(auto c : dt[y]){
        dt[x][c.fi] += c.se;
    }
}

int kol[mxN];
void dfs2(int v, int p){
    
    for(auto c : g[v]){
        int u = c.fi;
        int i = c.se;
        if(u != p){
            dfs2(u, v);
            kol[i] = sz(dt[u]);
            comb(v, u);
        }
    }

    for(auto c : add[v]){
        dt[v][c] += 1;
    }

    for(auto c : rem[v]){
        dt[v][c] -= 1;
        if(dt[v][c] == 0)dt[v].erase(c);
    }

}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> q >> k;
    ff(i,1,n - 1){
        int u, v;
        cin >> u >> v;
        g[u].pb({v, i});
        g[v].pb({u, i});
    }

    dfs1(1, 0);

    ff(i,1,q){
        int len;
        cin >> len;
        ff(j,1,len)cin >> A[j];
        sort(A + 1, A + len + 1, cmp);

        ff(j,2,len){
            int X = A[j - 1];
            int Y = A[j];
            int Z = LCA(X, Y);

            if(Z != Y){
                add[Y].pb(i);
                rem[Z].pb(i);
            }

            if(Z != X){
                add[X].pb(i);
                rem[Z].pb(i);
            }

        }

    }

    dfs2(1, 0);

    vector<int> res;
    ff(i,1,n - 1)if(kol[i] >= k)res.pb(i);

    cout << sz(res) << '\n';
    for(auto c : res)cout << c << " ";
    cout << '\n';

    return 0;
}
/*

6 3 2
1 3
2 3
3 4
6 4
4 5
4 1 3 2 5
2 6 3
2 3 2

// probati bojenje sahovski
*/
 
 
 
 
 
#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...