10.17模拟赛

· · 个人记录

分数

估分:6+0+3+0=9
得分:6+0+0+0=6 原因:T1set不会使用,二分+vector没调出来;T3没开long long

T1

思路:

考虑将原题抽象为一个图,仍可以选择的糖就是边,每次选择度数为1的点即可,使用拓扑排序

代码:
#include<bits/stdc++.h>
using namespace std;

int n, x, d[200001], st[200001];
vector<int> g1[200001], g2[200001], ans;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> d[i];
        for(int j = 1; j <= d[i]; j++){
            cin >> x;
            g1[i].push_back(x);
            g2[x].push_back(i);
        }
    }
    queue<int> q;
    for(int i = 1; i <= n; i++){
        if(d[i] == 1){
            q.push(i);
        }
    }
    while(q.size()){
        int t = q.front();
        q.pop();
        for(auto x : g1[t]){
            if(!st[x]){
                ans.push_back(t);
                st[x] = 1;
                for(auto y : g2[x]){
                    if(--d[y] == 1){
                        q.push(y);
                    }
                }
                break;
            }
        }
    }
    if(ans.size() != n){
        cout << "-1";
        return 0;
    }
    for(auto x : ans){
        cout << x << ' ';
    }
    return 0;
}