USACO 2018 Gold US Open P2. Milking Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<vector<int>> inputV;
vector<int> ans;

bool check(int x) {
    vector<vector<int>> adj(N + 1);
    vector<int> inDegree(N + 1, 0);

    for(int i = 0; i < x; i++) {
        for(int j = 1; j < inputV[i].size(); j++) {
            adj[inputV[i][j - 1]].push_back(inputV[i][j]);
            inDegree[inputV[i][j]]++;
        }
    }

    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 1; i < N + 1; i++) {
        if (! inDegree[i]) {
            pq.push(i);
        }
    }

    if (pq.empty()) {
        return false;
    }

    int turns = 0;
    while (! pq.empty()) {
        turns++;
        int node = pq.top(); pq.pop();
        ans[turns] = node;

        for (int child : adj[node]) {
            inDegree[child]--;

            if (inDegree[child] == 0) {
                pq.push(child);
            }
        }
    }

    if (turns != N) {
        return false;
    }
    return true;
}

int binary_search() {
    int l = 0;
    int r = M - 1;
    while (l != r) {
        int mid = (l + r + 1)/2;
        if (check(mid)) {
            l = mid;
        }
        else {
            r = mid - 1;
        }
    }
    // make sure to double check l!
    check(l);
    return l;
}

int main() {
    ifstream cin("milkorder.in");
    ofstream cout("milkorder.out");

    cin >> N >> M;

    ans.resize(N + 1);
    inputV.resize(M);

    // read in input
    for (int i = 0; i < M; i++) {
        int v;
        cin >> v;
        for (int j = 0; j < v; j++) {
            int val;
            cin >> val;
            inputV[i].push_back(val);
        }
    }

    // run binary search
    binary_search();

    // print answer
    for (int i = 1; i < N; i++) {
        cout << ans[i] << " ";
    }
    cout << ans[N];
    cout << endl;
}