-
단절점 - 11266Algorithm/BOJ 2020. 1. 10. 15:15
무향그래프에서 dfs 스패닝트리를 만들고 특정 정점을 루트 노드로 하는 subtree에서 선조 노드로 갈수 있는 역방향 간선이 있는지 확인하는 방법을 사용해 풀었습니다.
https://www.acmicpc.net/problem/11266
11266번: 단절점
첫째 줄에 두 정수 V(1≤V≤10,000), E(1≤E≤100,000)가 주어진다. 이는 그래프가 V개의 정점과 E개의 간선으로 이루어져 있다는 의미이다. 다음 E개의 줄에는 간선에 대한 정보를 나타내는 두 정수 A, B가 주어진다. 이는 A번 정점과 B번 정점이 연결되어 있다는 의미이며, 방향은 양방향이다. 입력으로 주어지는 그래프는 연결 그래프가 아닐 수도 있다.
www.acmicpc.net
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465//단절점 - 백준11266#include <iostream>#include <vector>using namespace std;int V, E;vector<vector<int>> adj;vector<int> discovered;vector<bool> bucket;int a_num = 0;int count = 0;int dfs(int here, bool isRoot){int ret = discovered[here] = count++;int there;int c_num = 0;for(int i = 0; i<adj[here].size(); i++){there = adj[here][i];if(discovered[there] == -1){++c_num;int subtree = dfs(there, false);if(!isRoot && discovered[here] <= subtree)bucket[here] = true;ret = min(ret, subtree);}elseret = min(ret, discovered[there]);}if(isRoot && c_num > 1)bucket[here] = true;if(bucket[here])++a_num;return ret;}void dfsAll(){for (int i = 1; i <= V; i++)if (discovered[i] == -1)dfs(i, true);}int main(){cin >> V >> E;adj = vector<vector<int>>(V + 1, vector<int>());discovered = vector<int>(V + 1, -1);bucket = vector<bool>(V + 1, false);int x, y;for (int i = 0; i < E; i++){cin >> x >> y;adj[x].push_back(y);adj[y].push_back(x);}dfsAll();cout << a_num << endl;for (int i = 1; i <= V; i++)if (bucket[i])cout << i << " ";}cs 'Algorithm > BOJ' 카테고리의 다른 글
효율적인 해킹 - 1325 (0) 2020.02.11 최소 스패닝 트리 - 1197 (0) 2020.01.30 최대공약수와 최소공배수 - 2609 (0) 2020.01.20 Strongly Connected Component - 2150 (0) 2020.01.20 단절선 - 11400 (1) 2020.01.10