ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1325번 C++ 풀이
    백준 2018. 11. 18. 17:08

    최적화를 안 해서 메모리와 시간사용량이 대단합니다.


    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <climits>
    using namespace std;
    vector <int> buffer[10001];
    bool isVisited[10001];
    int maxHackable[10001];
    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (auto i=0; i<m; i++){
    int a, b;
    cin >> a >> b;
    // a가 b를 신뢰한다 -> b 해킹시 a
    buffer[b].push_back(a);
    }
    priority_queue<pair<int, int>> pq;
    for (auto i=1; i<=n; i++){
    for (auto j=1; j<=n; j++){
    isVisited[j] = false;
    }
    queue<int> q;
    isVisited[i] = true;
    q.push(i);
    while (!q.empty()){
    int size = q.size();
    for (auto j=0; j<size; j++){
    int top = q.front();
    q.pop();
    for ( int next : buffer[top]){
    if (!isVisited[next]){
    isVisited[next] = true;
    q.push(next);
    }
    }
    }

    }
    int hacked = 0;
    for (auto i=1; i<=n; i++){
    if (isVisited[i]) hacked++;
    }
    maxHackable[i] = hacked;
    }
    int maxHackableNumeber = *max_element(maxHackable+1, maxHackable+n+1);
    for (auto i=1; i<=n; i++){
    if (maxHackableNumeber == maxHackable[i]){
    cout << i << ' ';
    }
    }
    return 0;

    }


    '백준' 카테고리의 다른 글

    백준 16935번 C++ 풀이  (0) 2018.11.18
    백준 16936번 C++ 풀이  (0) 2018.11.18
    백준 14890번 C++ 풀이  (0) 2018.11.17
    백준 C++ 2161번 풀이  (0) 2018.11.16
    백준 11725번 C++ 풀이  (0) 2018.10.09
Designed by Tistory.