728x90
Code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
vector<vector<int>> v(101);
bool visited[101];
void dfs(vector<int> except, int index){
visited[index] = true;
for(int i=0;i<v[index].size();i++){
int next = v[index][i];
if(visited[next])
continue;
if(index==except[0] && next==except[1])
continue;
if(index==except[1] && next==except[0])
continue;
dfs(except,next);
}
}
int solution(int n, vector<vector<int>> wires) {
int answer = n;
for(int i=0;i<wires.size();i++){
v[wires[i][0]].push_back(wires[i][1]);
v[wires[i][1]].push_back(wires[i][0]);
}
for(int i=0;i<wires.size();i++){
memset(visited,false,sizeof(visited));
//1번부터 순회
dfs(wires[i],1);
//visited true인 곳 세기
int true_cnt=0;
for(int i=1;i<=n;i++){
if(visited[i])
true_cnt++;
}
answer = min(answer,abs(true_cnt-(n-true_cnt)));
}
return answer;
}
|
cs |
728x90
'Algorithm > 완전탐색' 카테고리의 다른 글
[프로그래머스] 피로도 c++ (0) | 2023.02.21 |
---|---|
[프로그래머스] 소수찾기 c++ (1) | 2022.09.30 |
백준 11403 경로찾기 (0) | 2022.02.05 |
댓글