HDU 3729 I'm Telling the Truth -- 二分图最大匹配 输出方案

/*
	http://acm.hdu.edu.cn/showproblem.php?pid=3729  I'm Telling the Truth
	二分图最大匹配
*/
#include <iostream>
#include <string>
#include <cstring>
#include <map>
using namespace std;
#define CLR(c,v) memset(c,v,sizeof(c))

const int N = 1e4 + 5;
const int M = 6e5 + 5;
struct Vertex{	int head;	}V[N];
struct Edge{	int v,next;	}E[M];
int top,match[N];
bool vis[N];

void add(int u,int v){
	E[top].v = v;
	E[top].next = V[u].head;
	V[u].head = top++;
}

bool dfs(int u){
	for(int i = V[u].head ; ~i ; i = E[i].next){
		int v = E[i].v;
		if(!vis[v]){
			vis[v] = true;
			if(!match[v] || dfs(match[v]) ){
				match[v] = u;
				return true;
			}
		}
	}
	return false;
}

void maxMatch(int n){ /// 点数
	CLR(match,0);
	int ans = 0;
	int tmp[100] , cnt = 0;
	for(int i = n ; i >= 1 ; i--){
		CLR(vis,0);
		if(dfs(i)){
			++ans;
			tmp[++cnt] = i;
		}		
	}
	cout << ans << endl;
	for(int i = cnt ; i >= 2 ; i--)
		cout << tmp[i] << " ";
	cout << tmp[1] << endl;
}

int main(){
	//freopen("in.txt","r",stdin);
	int T;cin >> T;while(T--){
		int n;cin >> n;
		CLR(V,-1);
		top = 0;
		for(int i = 1 ; i <= n ; i++){
			int a, b;
			cin >> a >> b;
			for(int j = a ; j <= b ; j++)
				add(i,j);
		}
		maxMatch(n);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值