본문 바로가기
Computer Science

1766 문제집 / indgree 위상정렬

by OKOK 2018. 12. 7.

1. 벡터

2. 큐

3. 벡터, 벡터 vt

4. pq 우선순위 큐


vt 를 만들어서, 패런트와 차일드 관계를 만듬

그리고 pq 우선순위 큐를 사용함

in을 사용함 그래서 이어져 있는 것을 찾음 


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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define MAX_N 32000
using namespace std;
int n, m, a, b, in[MAX_N + 1];
vector<vector<int>> vt;
priority_queue<int> pq;
int main() {
    freopen("input.txt""r", stdin);
    scanf("%d%d"&n, &m);
    vt.resize(n + 1);
    for (int i = 0; i < m; i++) {
        scanf("%d%d"&a, &b);
        vt[a].push_back(b);
        in[b]++;
    }
    for (int i = 1; i <= n; i++) {
        if (!in[i])
            pq.push(-i);
    }
    while (pq.size()) {
        int here = -pq.top();
        pq.pop();
        printf("%d ", here);
        for (int there : vt[here]) {
            in[there]--;
            if (!in[there])
                pq.push(-there);
        }
    }
    return 0;
}
 
cs


댓글