/*
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;
}HDU 3729 I'm Telling the Truth -- 二分图最大匹配 输出方案
最新推荐文章于 2026-06-19 14:37:57 发布
本文介绍了一种解决二分图最大匹配问题的算法实现,通过深度优先搜索(DFS)来寻找增广路径并更新匹配结果。代码示例中详细展示了如何构建图结构、添加边以及进行最大匹配的过程。

386

被折叠的 条评论
为什么被折叠?



