-
효율적인 해킹 - 1325Algorithm/BOJ 2020. 2. 11. 12:47123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657//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+1, false);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초안에 안풀릴줄 알았는데 풀리네용~
'Algorithm > BOJ' 카테고리의 다른 글
학교 가지마! - 1420 (0) 2020.02.12 축사 배정 - 2188 (0) 2020.02.11 최소 스패닝 트리 - 1197 (0) 2020.01.30 최대공약수와 최소공배수 - 2609 (0) 2020.01.20 Strongly Connected Component - 2150 (0) 2020.01.20