题意:给你一颗树,一个点可以占领跟它连的所有的边。问最少需要多少个这样的点?
思路:一眼就是染色之后二分图最小点覆盖。
坑点:T得我不能自理,发现是边数组开小了,我加的双向边,也是爽。
http://acm.hdu.edu.cn/showproblem.php?pid=1054
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std ;
const int MOD = 1e9 +7 ;
const int INF = 0x3f3f3f3f ;
const int MAXN = 2000 ;
const int MAXE = 4000 ;
typedef long long LL;
typedef unsigned long long ULL;
struct Edge {
int to;
Edge * next;
}E[MAXE],E1[MAXE],*EE;
struct Gragh {
Edge * first;
}G[MAXN],G1[MAXN];
int N,M;
bool visit[MAXN];
int match[MAXN];
int C[MAXN];
int X[MAXN];
int Y[MAXN];
void addedge(int u,int v) {
EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;
}
void addedge1(int u,int v) {
EE->to = v ; EE -> next = G1[u].first ; G1[u].first = EE ++;
EE->to = u ; EE -> next = G1[v].first ; G1[v].first = EE ++;
}
int T,n,m,k;
void init() {
EE = E1; N = M = 0 ;
cls(G,0 );
cls(G1,0 );
}
bool find_path(int u) {
int v;
repE(p,u) {
v = p->to;
if (!visit[v]) {
visit[v] = 1 ;
if (match[v] == -1 || find_path(match[v])) {
match[v] = u;
return true ;
}
}
}
return false ;
}
int Max_match() {
cls(match,-1 );
int cnt = 0 ;
rep(i,1 ,N) {
cls(visit,0 );
if (find_path(i)) cnt ++;
}
return cnt;
}
void dfs(int u,int color,int fa = 0 ) {
C[u] = color % 2 ;
if (C[u] == 0 ) X[u] = ++N;
else Y[u] = ++M;
for (Edge * p = G1[u].first;p;p=p->next) {
int v = p->to;
if (v != fa) dfs(v,color+1 ,u);
}
}
void create_G(int u,int fa = 0 ) {
for (Edge * p = G1[u].first;p;p=p->next) {
int v = p->to;
if (v != fa)
{
if (C[u] == 0 ) addedge(X[u],Y[v]+N);
else addedge(X[v],Y[u]+N);
create_G(v,u);
}
}
}
void input() {
int fa , num , son;
rep(i,1 ,n) {
scanf ("%d:(%d)" ,&fa,&num);
rep(i,1 ,num) {
scanf ("%d" ,&son);
addedge1(fa,son);
}
}
}
void solve() {
dfs(0 ,0 );
EE = E;
create_G(0 );
printf ("%d\n" ,Max_match());
}
int main(void ) {
while (~scanf ("%d" ,&n)) {
init();
input();
solve();
}
return 0 ;
}