Vertex Cover
frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm))(v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm)).
She would like to color some vertices so that each edge has at least one colored vertex.
Find the minimum number of colored vertices.
Input
The input consists of multiple tests. For each test:
The first line contains 22 integers n,mn,m (2≤n≤500,1≤m≤n(n−1)22≤n≤500,1≤m≤n(n−1)2). Each of the following mm lines contains 22 integers ai,biai,bi (1≤ai,bi≤n,ai≠bi,min{ai,bi}≤301≤ai,bi≤n,ai≠bi,min{ai,bi}≤30)
Output
For each test, write 11 integer which denotes the minimum number of colored vertices.
Sample Input
3 2
1 2
1 3
6 5
1 2
1 3
1 4
2 5
2 6
Sample Output
1
2
题解:
最小顶点覆盖==最大匹配
#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
vector<int>G[maxn];
int match[maxn];
bool vis[maxn];
bool dfs(int v)
{
vis[v]=1;
for(int i=0;i<G[v].size();i++)
{
int u=G[v][i],w=match[u];
if(w<0||(!vis[w]&&dfs(w)))
{
match[v]=u;
match[u]=v;
return 1;
}
}
return 0;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<maxn;i++)G[i].clear();
for(int i=1;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
int res=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
{
if(match[i]<0)
{
memset(vis,0,sizeof(vis));
if(dfs(i))res++;
}
}
printf("%d\n",res);
}
return 0;
}