제출 #1172443

#제출 시각아이디문제언어결과실행 시간메모리
1172443DanielPr8Railway (BOI17_railway)C++20
0 / 100
176 ms30916 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
vector<map<ll,ll>> wh;
vll am, col;

void uni(ll a, ll b){
    if(wh[a].size()<wh[b].size()){
        uni(b,a);
        swap(wh[a],wh[b]);
        swap(am[a],am[b]);
        return;
    }
    for(auto [c,d]:wh[b]){
        if(d==0)continue;
        if(wh[a][c]==0)am[a]++;
        wh[a][c]+=d;
        if(wh[a][c]==col[c])am[a]--;
    }
}




int main(){
    ios_base::sync_with_stdio(0);cin.tie(NULL);
    ll n, m, k, a ,b, s;
    cin >> n >> m >> k;
    vector<set<pll>> g(n);
    for(ll i = 0; i < n-1; ++i){
        cin >> a >> b;
        a--;b--;
        g[a].insert({b,i});
        g[b].insert({a,i});
    }
    queue<ll> pq;
    wh = vector<map<ll,ll>>(n);
    am = vll(n);
    col = vll(m);
    for(ll i = 0; i < m; ++i){
        cin >> s;
        col[i]=s;
        while(s--){
            cin >> a;a--;
            if(wh[a][i]==0)am[a]++;
            wh[a][i]++;
            if(wh[a][i]==col[i])am[a]--;
        }
    }
    for(ll i = 0; i < n; ++i){
        if(g[i].size()==1)pq.push(i);
    }
    vll ans;
    while(!pq.empty()){
        ll cr = pq.front();
        pq.pop();
        if(g[cr].size()!=1)continue;
        pll pr = *(g[cr].begin());
        g[cr].clear();
        g[pr.f].erase({cr, pr.s});
        if(am[cr]>=k){
            ans.pb(pr.s);
        }
        uni(pr.f, cr);
        if(g[pr.f].size()==1)pq.push(pr.f);
    }
    cout << ans.size() << "\n";
    for(ll i:ans)cout << i+1 << " ";
}
#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...