제출 #1014374

#제출 시각아이디문제언어결과실행 시간메모리
1014374Error404Railway (BOI17_railway)C++17
100 / 100
156 ms41928 KiB
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define f first
#define s second
#define pi pair<ll,ll>
#define vi vector<ll>
#define vd vector<double>
#define vpi vector<pi>
#define pb push_back
#define INF 1e18
#define endl '\n'
//#define int ll
#define pii pair<pi,ll>



const int mod = 1e9+7;
const int MAX = 1e5+1;
const int LOG = 30;
vi g[MAX];
int tin[MAX], val[MAX], tout[MAX];
int up[LOG+1][MAX];
map<pi,int>mp;
vector<pi>order;
int timer = 0;


void dfs(int from,int pr){

    tin[from]= ++timer;
    up[0][from] = pr;
    for(int i=1 ; i <= LOG; i++){    
        up[i][from]  = up[i-1][up[i-1][from]];
    }


    for(int to :g[from]){
        if(to==pr) continue;
        dfs(to,from);
    }
    tout[from]=++timer;
}

bool anc(int u,int v){
    return tin[u]<=tin[v] && tout[u]>=tout[v];   
}
ll dfs1(int from,int pr){
    ll sum = val[from];
    ll hk;
    for(int to :g[from]){
        if(to==pr) continue;
        hk = dfs1(to,from);
        sum+=hk;
        int a = min(from,to);
        int b = max(from,to);
        mp[{a,b}]+=hk;
    }
    return sum;
}


int lca(int u, int v){
    if(anc(u,v)) return u;
    if(anc(v,u)) return v;
    for(int i = LOG; i >= 0; i--){
        if(anc(up[i][u], v)) continue;
        u= up[i][u];
    }
    return up[0][u];
}


signed main(){
    int n,m,k,a,b;
    cin >>n >> m >> k;
    for(int i =1; i<n; i++){
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
        order.pb({a,b});
    }
    int q;

    dfs(1,1);

    for(int i =0; i < m; i++){
        cin >>q;
        vector<pi>v;
        for(int j = 0; j <q; j++){
            cin >> a;
            v.pb({tin[a],a});
        }
        sort(v.begin(),v.end());

        for(int j = 0; j < q; j++){
            a = v[j].s;
            b = v[(j+1)% q].s;
            int common = lca(a,b);
            val[a]++;
            val[b]++;
            val[common]-=2;
        }

    }
    dfs1(1,0);
    vi ans;
    int cnt = 1;
    for(pi j: order){

        a = min(j.f,j.s);
        b = max(j.f,j.s);
     //   cout <<a<<" " << b << " "<<mp[{a,b}]<< endl; 
        if(mp[{a,b}]>=k*2) ans.pb(cnt);
        cnt++;
    }

    cout << ans.size() << endl;
    for(int i :ans){
        cout << i <<" ";
    }
    cout << endl;

}
/*



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