图论算法 住宿安排

 

 

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 stub

		Scanner 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 stub

		for (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 stub
		if (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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值