-
단절선 - 11400Algorithm/BOJ 2020. 1. 10. 15:20
무향그래프에서 dfs 스패닝트리를 만들고 특정 정점을 루트 노드로 하는 subtree에서 선조 노드로 갈수 있는 역방향 간선이 있는지 확인하는 방법을 사용해 풀었습니다.
https://www.acmicpc.net/problem/11400
11400번: 단절선
첫째 줄에 두 정수 V(1≤V≤100,000), E(1≤E≤1,000,000)가 주어진다. 이는 그래프가 V개의 정점과 E개의 간선으로 이루어져 있다는 의미이다. 다음 E개의 줄에는 간선에 대한 정보를 나타내는 두 정수 A, B가 주어진다. 이는 A번 정점과 B번 정점이 연결되어 있다는 의미이며, 방향은 양방향이다. 그래프는 항상 연결되어 있으며, 같은 간선이 두 번 이상 들어오는 경우는 없다. 또, A와 B가 같은 경우도 없다. 그래프의 정점은 1부터
www.acmicpc.net
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172//단절선 찾기 - 백준11400#include <iostream>#include <vector>#include <algorithm>using namespace std;int V, E;vector<vector<int>> adj;vector<pair<int, int>> ans;vector<int> discovered;int counter = 0;int a_num = 0;bool compare(const pair<int, int>&p1, const pair<int, int>&p2){if(p1.first == p2.first)return p1.second < p2.second;return p1.first < p2.first;}int dfs(int here, int before){int ret = discovered[here] = counter++;int there;for (int i = 0; i < adj[here].size(); i++){there = adj[here][i];if (discovered[there] == -1)ret = min(ret, dfs(there, here));else if(there != before)ret = min(ret, discovered[there]);}if (before != -1 && ret >= discovered[here]){++a_num;if (here < before)ans.push_back(make_pair(here, before));elseans.push_back(make_pair(before, here));}return ret;}void dfsAll(){for (int i = 1; i <= V; i++)if (discovered[i] == -1)dfs(i, -1);}int main(){ios_base::sync_with_stdio(0); cin.tie(0);cin >> V >> E;adj = vector<vector<int>>(V + 1);ans = vector<pair<int, int>>();discovered = vector<int>(V + 1, -1);int x, y;for (int i = 0; i < E; i++){cin >> x >> y;adj[x].push_back(y);adj[y].push_back(x);}dfsAll();sort(ans.begin(), ans.end(), compare);cout << a_num << endl;for(int i = 0; i<ans.size(); i++)cout << ans[i].first << " " << ans[i].second << "\n";}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 단절점 - 11266 (0) 2020.01.10