BOJ 3665 최종 순위

1 개요[ | ]

BOJ 3665 최종 순위

2 C++[ | ]

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

int n, m;
int t[501];
int indeg[501];
bool adj[501][501];

void solve() {
    queue<int> q;
    for (int i=1; i<=n; i++) {
        if (indeg[i] == 0) q.push(i);
    }
    vector<int> v;
    while (!q.empty()) {
        if (q.size() > 1) {
            cout << "?\n";
            return;
        }
        int cur = q.front();
        q.pop();
        v.push_back(t[cur]);
        if (v.size() == n) {
            for (auto& el: v) {
               cout << el << ' ';
            }
            cout << '\n';
            return;
        }
        for (int i=1; i<=n; i++) {
            if (!adj[cur][i]) continue;
            adj[cur][i] = false;
            indeg[i]--;
            if (indeg[i] == 0) q.push(i);
        }
    }
    cout << "IMPOSSIBLE\n";
}

void sub() {
    cin >> n;
    memset(t, 0, sizeof(t));
    memset(adj, 0, sizeof(adj));
    memset(indeg, 0, sizeof(indeg));
    int node[501] = {};
    for (int i=1; i<=n; i++) {
        cin >> t[i];
        node[t[i]] = i;
    }
    for (int i=1; i<n; i++) {
        for (int j=i+1; j<=n; j++) {
            adj[i][j] = true;
            indeg[j]++;
        }
    }
    cin >> m;
    int temp1, temp2;
    for (int i=0; i<m; i++) {
        cin >> temp1 >> temp2;
        int a = node[temp1];
        int b = node[temp2];
        if(a > b) swap(a, b);
        adj[a][b] = false;
        indeg[b]--;
        adj[b][a] = true;
        indeg[a]++;
    }
    solve();
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--) {
        sub();
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}