Baekjoon #1325

Baekjoon #1325

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<int>> G(100005);
bool vis[100005];
int comp_cnt;

void dfs(int cur) {
    vis[cur] = true;
    ++comp_cnt;
    for (const int &nxt : G[cur]) {
        if (!vis[nxt]) dfs(nxt);
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for (int i = 0; i < M; ++i) {
        // u v가 주어지면 v가 해킹됐을 때 u도 해킹된다.
        int u, v;
        cin >> u >> v;
        G[v].push_back(u);
    }
    vector<int> ans;
    int max_cnt = -1;
    for (int i = 1; i <= N; ++i) {
        memset(vis, false, N + 1);
        comp_cnt = 0;
        dfs(i);
        if (max_cnt < comp_cnt) {
            max_cnt = comp_cnt;
            ans.clear();
            ans.push_back(i);
        }
        else if (max_cnt == comp_cnt) ans.push_back(i);
    }
    for (const int &x : ans) cout << x << ' ';
    cout << '\n';
    return 0;
}

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.