Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 백준
- HTTP
- notification
- hilt
- DataBinding
- Behavior
- 안드로이드
- Coroutine
- onLayout
- lifecycle
- Algorithm
- BOJ
- View
- LiveData
- CollapsingToolbarLayout
- 코틀린
- onMeasure
- Navigation
- room
- recyclerview
- sqlite
- AppBarLayout
- Android
- 알림
- 알고리즘
- CoordinatorLayout
- CustomView
- ViewModel
- kotlin
- activity
Archives
- Today
- Total
개발일지
Algorithm in A..Z - 백준 3648 아이돌 본문
https://www.acmicpc.net/problem/3648
3648번: 아이돌
각 테스트 케이스에 대해서, 상근이를 포함해, 다음 라운드 진출 목록을 심사위원의 의심 없이 만들 수 있으면 'yes'를, 없으면 'no'를 출력한다.
www.acmicpc.net
접근
Tarjan 알고리즘으로 SCC를 구현하면 특성상 하위의 SCC가 먼저 결정된다. 그렇기 때문에 x < ¬x 를 만족하면 x가 참이된다.
상근이가 합격하려면 ¬x가 False면 합격이기 때문에 그래프를 구현할 때 역방향으로 그래프를 구현하여 ¬x가 False가 될 수 있는지 확인한다. (명제의 대우는 동치이기 때문에 가능하다.)
코드
#include <bits/stdc++.h>
using namespace std;
constexpr int NONE = -1;
int n, m;
vector<vector<int>> graph;
inline int convert(int value) {
return value <= n ? value + n : value - n;
}
int order;
stack<int> st;
vector<int> sccNumber, isVisited;
vector<vector<int>> scc;
int dfs(int index) {
st.push(index);
isVisited[index] = order++;
int value = isVisited[index];
for (auto &next : graph[index]) {
if (isVisited[next] == NONE) {
value = min(value, dfs(next));
} else if (sccNumber[next] == NONE) {
value = min(value, isVisited[next]);
}
}
if (value == isVisited[index]) {
vector<int> cycle;
while (!st.empty()) {
int i = st.top(); st.pop();
cycle.push_back(i);
sccNumber[i] = (int)scc.size();
if (i == index) {
break;
}
}
scc.push_back(cycle);
}
return value;
}
inline void init() {
graph = vector<vector<int>>(2*n + 1);
order = 0;
st = stack<int>();
sccNumber = isVisited = vector<int>(2*n + 1, NONE);
scc = vector<vector<int>>();
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
while (cin >> n >> m) {
init();
while (m--) {
int s, e;
cin >> s >> e;
s = s < 0 ? convert(-s) : s;
e = e < 0 ? convert(-e) : e;
graph[convert(s)].push_back(e);
graph[convert(e)].push_back(s);
}
for (int i = 1;i <= 2*n;++i) {
if (isVisited[i] == NONE) {
dfs(i);
}
}
bool flag = true;
for (int i = 1;i <= n;++i) {
if (sccNumber[i] == sccNumber[convert(i)]) {
flag = false;
break;
}
}
if (sccNumber[convert(1)] < sccNumber[1]) {
flag = false;
}
cout << (flag ? "yes" : "no") << "\n";
}
}
'Problem Solving' 카테고리의 다른 글
Algorithm in A..Z - Programmers 트리 트리오 중간값 (0) | 2021.09.08 |
---|---|
Algorithm in A..Z - Programmers 몸짱 트레이너 라이언의 고민 (0) | 2021.09.06 |
Algorithm in A..Z - 백준 4013 ATM (0) | 2021.07.09 |
Algorithm in A..Z - 백준 2449 전구 (0) | 2021.05.09 |
Algorithm in A..Z - 백준 3830 교수님은 기다리지 않는다 (0) | 2021.04.20 |
Comments