페이지

1432번: 그래프 수정

https://www.acmicpc.net/problem/1432


#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
int n, ind[300], res[300];
vector<int> adj[300];
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0, x; j < n; j++) {
            scanf("%1d", &x);
            if (x) adj[j].push_back(i), ind[i]++;
        }
    }
    priority_queue<int> pq;
    for (int i = 0; i < n; i++) if (!ind[i]) pq.push(i);
    int m = n;
    while (!pq.empty()) {
        int h = pq.top();
        pq.pop();
        res[h] = m--;
        for (auto it : adj[h])
            if (!--ind[it]) pq.push(it);
    }
    if (m) puts("-1");
    else for (int i = 0; i < n; i++) printf("%d ", res[i]);
    return 0;
}

댓글 없음 :

댓글 쓰기