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 |
Tags
- 우선순위큐
- java
- COSPROJAVA1급
- 백준
- 이젠 골드구현도 어렵네..
- 재귀함수
- 네트워크플로우
- spring
- 알고리즘
- PS
- DFS
- GatherTown
- YBMCOS
- BFS
- 완전탐색
- COSPRO
- 시뮬레이션
- 자바PS
- deque
- 취득후기
- 01BFS
- 백준코딩테스트
- 엘라스틱서치
- 세그먼트트리
- dp
- 구현
- QUICKSTARTGUIDE
- 게더타운시작
- 다익스트라
- 다이나믹프로그래밍
Archives
- Today
- Total
공부공간
BOJ - 2188 ) 축사배정 본문
https://www.acmicpc.net/problem/2188
각 소가 원하는 축사가 존재한다 ( 존재하지 않을수도 있음 )
일단은 배정후 변경하는 이분매칭알고리즘을 사용하자.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
class Main {
public static int m,n,result[];
public static boolean check[];
public static ArrayList<Integer> node[];
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
node = new ArrayList[n+1];
// 축사가 어느 소에 배정이 되어있는지 정보 RESULT배열 -> 0은 없는것 꼭 1부터 시작
result = new int[m+1];
for(int i = 1 ; i <= n ; i++){node[i] = new ArrayList<>();}
for(int i = 0 ; i < n ; i ++){
st = new StringTokenizer(br.readLine());
int cnt = Integer.parseInt(st.nextToken());
while(cnt-->0){
int wantHoust = Integer.parseInt(st.nextToken());
node[i+1].add(wantHoust);
}
}
int answer =0;
for(int i = 1 ; i <= n ; i++){
check = new boolean[m+1];
if(bipartiteMatching(i)){
answer++;
}
}
System.out.print(answer);
}
public static boolean bipartiteMatching(int x){
for(int i = 0 ; i < node[x].size() ; i++){
// 원하는 집
int want = node[x].get(i);
if(!check[want]){
// 방문안했다면 방문처리 해주어 중복방지
check[want] = true;
// 내가 들어가고싶은 축사가 비어있으면 배정 or 앞에있는 축사의 배정이 바뀔수 있다면 재귀
if(result[want]==0 || bipartiteMatching(result[want])){
result[want] = x;
return true;
}
}
}
return false;
}
}
'알고리즘 > 이분 매칭' 카테고리의 다른 글
BOJ - 11375 ) 열혈강호 (0) | 2020.09.01 |
---|---|
BOJ - 1298 ) 노트북의 주인을 찾아서 (0) | 2020.08.30 |
Comments