标签:图论
2 4 1 2 1 3 1 4 2 3 2 4 3 4 4 1 2 1 3 1 4 2 3 2 4 3 4
2 2
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
const int N = 5005;
int head[N], to[N *10], next1[N *10], tot;
int mach[N];
bool vist[N];
void addEdge(const int& u, const int& v) {
to[tot] = v, next1[tot] = head[u], head[u] = tot++;
}
void addUndirEdge(const int& u, const int& v) {
addEdge(u, v), addEdge(v, u);
}
void init(){
tot=0;
memset(mach,-1,sizeof(mach));
memset(head,-1,sizeof(head));
}
bool findroad(int u){
int i=head[u];
while(i!=-1){
int v=to[i]; i=next1[i];
if(!vist[v]){
vist[v]=1;
if(mach[v]==-1||findroad(mach[v])){
mach[v]=u; return true;
}
}
}
return false;
}
int main(){
int n,T,u,v;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
int m=3*n/2;
while(m--){
scanf("%d%d",&u,&v);
addUndirEdge(u,v);
}
int ans=0;
while(n){
if(head[n]!=-1) {
memset(vist,0,sizeof(vist));
ans+=findroad(n);
}
n--;
}
printf("%d\n",ans/2);
}
}
HDU1845 Jimmy’s Assignment(最大匹配)卡时间
标签:图论
原文地址:http://blog.csdn.net/u010372095/article/details/45673537