Algorithm/BOJ

효율적인 해킹 - 1325

jhg0406 2020. 2. 11. 12:47
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
//1325 - 효율적인 해킹
 
#include <iostream>
#include <vector>
using namespace std;
 
int N, M;
vector<vector<int>> adj;
vector<bool> visited;
vector<int> hacked;
 
void init()
{
    cin >> N >> M;
    adj = vector<vector<int>>(N+1);
    hacked = vector<int>(N+1);
    int x, y;
    for(int i = 0; i<M; ++i)
    {
        cin >> x >> y;
        adj[y].push_back(x);
    }
}
 
int dfs(int here)
{
    visited[here] = true;
    int ret = 1;
    for(int i = 0; i<adj[here].size(); ++i)
    {
        int there = adj[here][i];
        if(!visited[there])
            ret += dfs(there);
    }
    return ret;
}
 
void dfsAll()
{
    int big = 0;
    for(int i = 1; i<=N; ++i)
    {
        visited = vector<bool>(N+1false);
        int t = dfs(i);
        big = max(big, t);
        hacked[i] = t;
    }
    for(int i = 1; i<=N; ++i)
        if(big == hacked[i])
            cout << i << " ";
}
 
int main()
{
    init();
    dfsAll();
}
cs

 

 

 

 

 

 

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

 

1325번: 효율적인 해킹

첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한다"를 의미한다. 컴퓨터는 1번부터 N번까지 번호가 하나씩 매겨져 있다.

www.acmicpc.net

 

 

 

 

 

 

효율적인 해킹

모든 정점에 대해 DFS를 돌리면 되는 문제였습니다..

DFS시간복잡도 * 전체 정점 개수를 하면 10억이기에 5초안에 안풀릴줄 알았는데 풀리네용~