hdu2063:
http://acm.hdu.edu.cn/showproblem.php?pid=2063
题意思路:裸求最大匹配数。
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 505;
const int INF = 0x3f3f3f3f;
int head[N], match[N];
bool vis[N];
int n, m, cnt;
struct Edge
{
int to,next;
}edge[N*N];
void add(int u, int v)
{
edge[cnt] = (struct Edge){v, head[u]};
head[u] = cnt++;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
bool Augment(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = true;
if(match[v]==-1 || Augment(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int Hungary(int m)
{
int ans = 0;
memset(match, -1, sizeof(match));
for(int i = 1; i <= m; i++)
{
memset(vis, false, sizeof(vis));
if(Augment(i)) ans++;
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int k, n, m, u, v;
while(~scanf("%d", &k))
{
if(k == 0) break;
scanf("%d%d", &m, &n);
init();
for(int i = 1; i <= k; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
printf("%d\n", Hungary(m));
}
return 0;
}
poj1469:
http://poj.org/problem?id=1469
题意思路:看所有课程是否可以都有匹配对象。
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 505;
const int INF = 0x3f3f3f3f;
int head[N], match[N];
bool vis[N];
int n, m, cnt;
struct Edge
{
int to,next;
}edge[N*N];
void add(int u, int v)
{
edge[cnt] = (struct Edge){v, head[u]};
head[u] = cnt++;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
bool Augment(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = true;
if(match[v]==-1 || Augment(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int Hungary(int p)
{
int ans = 0;
memset(match, -1, sizeof(match));
for(int i = 1; i <= p; i++)
{
memset(vis, false, sizeof(vis));
if(Augment(i)) ans++;
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int t, p, n, c, u, v;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &p, &n);
init();
for(u = 1; u <= p; u++)
{
scanf("%d", &c);
while(c--)
{
scanf("%d", &v);
add(u, v);
}
}
int ans = Hungary(p);
if(ans == p) printf("YES\n");
else printf("NO\n");
}
return 0;
}