题目:给你一棵树。找到最小的顶点集合,使得全部的边至少有一个顶点在这个集合中。

分析:树形dp,图论,最小顶点覆盖。

方案1:树形dp。分别记录每一个节点取和不取的最优解f(k。0)与f(k,1);

每一个节点的状态取决于子树,子树的根都不选,则他必选;否则取最小;

f(k。0)= sum(f(i,1))。

f(k。1)= sum(min(f(i,0)。f(i。1))){ 当中 i 是k的子树根节点 };

方案2:最小顶点覆盖 = N - 最大独立点 = 最大匹配。

说明:(2011-09-19 09:46)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define min(x,y) ((x)<(y)?(x):(y)) typedef struct node
{
int Count;
int Value;
int Next[ 10 ];
}node;
node Node[ 1510 ];
int Root; typedef struct answ
{
int sum0;
int sum1;
}Answ;
Answ Save; Answ dp( int Root )
{
Answ an;
an.sum0 = 0;
an.sum1 = 1;
if ( Node[ Root ].Count ) {
for ( int i = 0 ; i < Node[ Root ].Count ; ++ i ) {
Save = dp( Node[ Root ].Next[ i ] );
an.sum0 += Save.sum1;
an.sum1 += min( Save.sum0, Save.sum1 );
}
}
return an;
} int main()
{
int n,a,m,b;
while ( scanf("%d",&n) != EOF ) {
Root = -1;
memset( Node, 0, sizeof( Node ) );
for ( int i = 0 ; i < n ; ++ i ) {
scanf("%d:(%d)",&a,&m);
if ( Root == -1 ) Root = a;
Node[ a ].Count = m;
Node[ a ].Value = a;
for ( int j = 0 ; j < m ; ++ j ) {
scanf("%d",&b);
Node[ a ].Next[ j ] = b;
}
}
Answ answer = dp( Root );
printf("%d\n",min( answer.sum0, answer.sum1 ));
}
return 0;
}

图论解法:

#include <stdio.h>
#include <stdlib.h> typedef struct node
{
int Point;
node* Next;
}node;
node Node[ 3001 ];
node *Head[ 1501 ];
bool Used[ 1501 ];
int Result[ 1501 ]; bool find( int a, int n )
{
for ( node *P = Head[ a ] ; P ; P = P->Next )
if ( !Used[ P->Point ] ) {
Used[ P->Point ] = true;
if ( Result[ P->Point ] == -1 || find( Result[ P->Point ] , n ) ) {
Result[ P->Point ] = a;
return true;
}
}
return false;
} int argument( int n )
{
for ( int i = 0 ; i < n ; ++ i )
Result[ i ] = -1;
int Count = 0;
for ( int i = 0 ; i < n ; ++ i ) {
for ( int j = 0 ; j < n ; ++ j )
Used[ j ] = false;
if ( find( i, n ) )
++ Count;
}
return Count;
} int main()
{
int n,a,m,b;
while ( scanf("%d",&n) != EOF ) {
for ( int i = 0 ; i < n ; ++ i )
Head[ i ] = NULL;
int Count = 0;
for ( int i = 0 ; i < n ; ++ i ) {
scanf("%d:(%d)",&a,&m);
for ( int j = 0 ; j < m ; ++ j ) {
scanf("%d",&b);
Node[ Count ].Next = Head[ a ];
Node[ Count ].Point = b;
Head[ a ] = &Node[ Count ++ ];
Node[ Count ].Next = Head[ b ];
Node[ Count ].Point = a;
Head[ b ] = &Node[ Count ++ ];
}
}
printf("%d\n",argument( n )/2);
}
return 0;
}

zoj 1134 - Strategic Game的更多相关文章

  1. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  2. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  3. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  4. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  5. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  6. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  7. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  8. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  9. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

随机推荐

  1. cocos2dx进阶学习之屏幕适配

    背景 在学习cocos2dx时,我们在main函数中发现一句代码, #include "main.h" #include "AppDelegate.h" #in ...

  2. layer 的常用属性

    layer的各种属性代码示例: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading ...

  3. PHP+Apache+Mysql 配置流程【配置之后才能正常使用】

    你需要的软件: 1.服务器:apache_2.2.4-win32-x86-no_ssl.msi http://pan.baidu.com/share/link?shareid=3837123167&a ...

  4. mysql select简单用法

    1.select语句可以用回车分隔 $sql="select * from article where id=1" 和 $sql="select * from artic ...

  5. PHP - 继承 - 子类使用父类方法

    <?php class ShopProduct { private $FirstName; private $LastName; private $Title; private $Price; ...

  6. ThinkPHP - CURD增删改查 - 实例

    目录结构:

  7. select()函数详解

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是 习惯写诸如connect. accept.recv或recvfrom这样的阻塞程 ...

  8. python模块 mysql-python安装(在ubuntu系统下)

    直接运行如下命令 sudo pip install MySQL-python 报如下错误 xxx@ubuntu:~$ sudo pip install MySQL-python Downloading ...

  9. ThinkPHP 3.1.2 URL<1>

    # # ThinkPHP 3.1.2 URL 本节课大纲: 一.URL规则 1.默认是区分大小写的 2.如果我们不想区分大小写可以改配置文件 'URL_CASE_INSENSITIVE'=>tr ...

  10. 上证A股股指跌破1900

    上证A股股指跌破1900 有钱的同学赶紧买哦,机会难得哈哈!