Algorithm/BOJ
[C/C++] 백준 2188번 - 축사 배정
poopooreum
2024. 4. 14. 13:44
반응형
✏️문제 링크
https://www.acmicpc.net/problem/2188
2188번: 축사 배정
농부 존은 소 축사를 완성하였다. 축사 환경을 쾌적하게 유지하기 위해서, 존은 축사를 M개의 칸으로 구분하고, 한 칸에는 최대 한 마리의 소만 들어가게 계획했다. 첫 주에는 소를 임의 배정해
www.acmicpc.net
✏️문제 설명
✏️문제 풀이
기본적인 이분 매칭 구현 문제입니다.
이분 매칭 알아보기
https://pooreumjung.tistory.com/338
Algorithm 공부 #26 - 이분 매칭(Binary Matching)
Algorithm 공부 #26 - 이분 매칭 ✏️이분 매칭이란? 이분 그래프에서 주로 사용하는 알고리즘 이분 그래프는 두 개의 정점 그룹이 존재할 때 모든 간선의 용량이 1이면서 양쪽 정점이 서로 다른 그
pooreumjung.tistory.com
✏️문제 코드
#include<iostream>
#include<vector>
using namespace std;
#define MAX 201
int N, M;
vector<int>graph[MAX];
int node[MAX];
bool visit[MAX];
bool dfs(int cow);
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> M;
for (int i = 1; i <= N; i++) { // 그래프 구현
int number;
cin >> number;
while (number--) {
int shed;
cin >> shed;
graph[i].push_back(shed);
}
}
int count = 0;
for (int i = 1; i <= N; i++) {
fill(visit, visit + MAX, false);
if (dfs(i))
count++;
}
cout << count;
}
bool dfs(int cow)
{
for (int i = 0; i < graph[cow].size(); i++) {
int shed = graph[cow][i];
if (visit[shed])
continue;
visit[shed] = true;
if (node[shed] == 0 || dfs(node[shed])) {
node[shed] = cow;
return true;
}
}
return false;
}
✏️다른 이분 매칭 문제 보러가기
https://pooreumjung.tistory.com/339
[C/C++] 백준 열혈강호 문제 모음 / 백준 11375번, 백준 11376번
✏️ 문제 링크 https://www.acmicpc.net/problem/11375 11375번: 열혈강호 강호네 회사에는 직원이 N명이 있고, 해야할 일이 M개가 있다. 직원은 1번부터 N번까지 번호가 매겨져 있고, 일은 1번부터 M번까지 번
pooreumjung.tistory.com
반응형