图论算法 住宿安排
来源:互联网 发布:g71车内孔编程实例 编辑:程序博客网 时间:2024/06/10 00:09
package com.bluecup.org;import java.util.ArrayList;import java.util.Arrays;import java.util.Scanner;public class CheckIn {/** * @param args */static boolean check[][] = new boolean[20][2];static int v[][] = new int[20][2];static int f[][] = new int[20][20];static boolean used[] = new boolean[20];static int linkey[] = new int[20];static int n, m;static ArrayList<Integer> a = new ArrayList<Integer>();static ArrayList<Integer> b = new ArrayList<Integer>();public static void main(String[] args) {// TODO Auto-generated method stubScanner scan = new Scanner(System.in);n = scan.nextInt();m = scan.nextInt();for (int i = 0; i < m; i++) {v[i][0] = scan.nextInt();v[i][1] = scan.nextInt();f[v[i][0]][v[i][1]] = f[v[i][1]][v[i][0]] = 1;}if (divide(0)) {for (int i = 0; i <= n; i++) {if (check[i][0]) {a.add(i);}if (check[i][1]) {b.add(i);}}int t = 0;for (int i = 0; i < a.size(); i++) {Arrays.fill(used, false);if (hungary(i))t++;}System.out.println(t);for (int i = 0; i < b.size(); i++) {System.out.println(b.get(i) + " " + a.get(linkey[i]));}}else{System.out.println("no");}}private static boolean hungary(int i) {// TODO Auto-generated method stubfor (int j = 0; j < b.size(); j++) {if (used[j] == false && f[a.get(i)][b.get(j)] == 1) {used[j] = true;if (linkey[j] == 0 || hungary(linkey[j])) {linkey[j] = i;return true;}}}return false;}private static boolean divide(int x) {// TODO Auto-generated method stubif (x == m)return true;if (check[v[x][0]][0] && check[v[x][1]][0]|| (check[v[x][0]][1] && check[v[x][1]][1]))return false;if (check[v[x][0]][0]) {check[v[x][1]][1] = true;if (divide(x + 1))return true;check[v[x][1]][1] = false;} else if (check[v[x][0]][1]) {check[v[x][1]][0] = true;if (divide(x + 1))return true;check[v[x][1]][0] = false;} else if (check[v[x][1]][0]) {check[v[x][0]][1] = true;if (divide(x + 1))return true;check[v[x][0]][1] = false;} else if (check[v[x][1]][1]) {check[v[x][0]][0] = true;if (divide(x + 1))return true;check[v[x][0]][0] = false;} else {check[v[x][0]][0] = true;check[v[x][1]][1] = true;if (divide(x + 1))return true;check[v[x][0]][0] = false;check[v[x][1]][1] = false;check[v[x][0]][1] = true;check[v[x][1]][0] = true;if (divide(x + 1))return true;check[v[x][0]][1] = false;check[v[x][1]][0] = false;}return false;}}
0 0
- 图论算法 住宿安排
- 【图论-二分图】学生的住宿
- 贪心算法-节目安排
- 关于活动安排算法
- 会议安排最优算法
- 算法训练 比赛安排
- 贪心算法-会场安排
- 算法 会场安排问题
- 算法训练 比赛安排
- 算法训练 比赛安排
- 【算法题】工作安排
- 贪心算法--活动安排
- 贪心算法☞会场安排问题/图着色
- 住宿环境
- 弱鸡的暑假图论安排
- first,单循环比赛安排算法
- 活动安排 贪心算法实现
- 贪心算法_活动安排
- Oracle监听的作用
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
- C# 伪随机数 随机数顺序排列 附带双色球机选程序
- PeopleReady 是什么系统?其中文为全员就绪
- html + javascript
- 图论算法 住宿安排
- 介绍一个轻量级iOS安全框架:SSKeyChain
- Response的一些用法
- Hello world!
- 制作电子书遇到的知识点整理
- ef 事务 的一种处理方法
- ckeditor KindEditor eWebEditor WQeditor FreeTextbox Tinymce 几款在线编辑器的比较(附各版本demo下载地址)
- C语言简单函数递归调用问题
- CentOS下JDK7和tomcat的安装配置