페이지

2252번: 줄 세우기

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


#include<stdio.h>
#include<vector>
using namespace std;
const int MAX_N = 32000;
int n, m, ind[MAX_N + 1];
vector<int> adj[MAX_N + 1];
void dfs(int h) {
    if (ind[h]) return;
    ind[h] = -1;
    printf("%d ", h);
    for (auto t : adj[h])
        --ind[t], dfs(t);
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].push_back(b);
        ind[b]++;
    }
    for (int i = 1; i <= n; i++) dfs(i);
    return 0;
}

댓글 없음 :

댓글 쓰기