[POJ1463] Strategic game
题目链接:##
题目大意:##
Bob非常享受玩电脑游戏的过程,尤其是策略游戏,但是在有些时候,他因为不能在第一时间找到最佳的策略而十分伤心。 现在,他遇到了一个问题。他必须保卫一个中世纪的城市,有很多道路将整个城市连起来,整体上看上去像一棵树。Bob需要放置尽可能少的士兵,保卫树上所有的边。士兵只能放在节点上,但是却可以保卫所有与这个节点相邻的边。
题目分析:##
由于树本身具有二分图的性质,所以这里我把树手动染色转成二分图,然后跑最小点覆盖
(当然也可以树形dp)
代码:##
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N (1500 + 5)
using namespace std;
inline int read(){
int cnt = 0, f = 1; char c;
c = getchar();
while (!isdigit(c)) {
if(c == '-') f = -f;
c = getchar();
}
while (isdigit(c)) {
cnt = cnt * 10 + c - '0';
c = getchar();
}
return cnt * f;
}
int n, nxt[2*N], to[2*N], first[2*N], tot, x, t, y, Float[N], cnt, black, white, ans, match[N];
bool vis[N];
struct node{
int x;int y;
}edge[N];
void add(int x, int y) {
nxt[++tot] = first[x];
first[x] = tot;
to[tot] = y;
}
void Dfs(int u){
for (register int i = first[u]; i; i = nxt[i]) {
int v = to[i];
if(!Float[v]) {
Float[v] = Float[u] + 1;
Dfs(v);
}
}
}
void build_map() {
memset (first, 0, sizeof(first));
memset (nxt, 0, sizeof(nxt));
memset (to, 0, sizeof(to));
tot = 0;
for (register int i = 1; i <= cnt; i++) {
int X = edge[i].x; int Y = edge[i].y;
if (Float[X] % 2 == 1) {
add(X, Y);
if (!vis[X]) {
vis[X] = true;
black++;
}
if (!vis[Y]) {
vis[Y] = true;
white++;
}
} else {
add(Y, X);
if (!vis[X]) {
vis[X] = true;
white++;
}
if (!vis[Y]) {
vis[Y] = true;
black++;
}
}
}
}
int find(int u) {
// cout<<u<<"##"<<endl;
for (register int i = first[u]; i; i = nxt[i]) {
int v = to[i];
// cout<<vis[v]<<" "<<v<<endl;
if (vis[v]) {
continue;
} else {
vis[v] = true;
if (match[v] == -1||find(match[v])) {
match[v] = u;
return 1;
}
}
}
return 0;
}
int hungary() {
for (register int i = 1; i <= n; i++) match[i] = -1;
for (register int i = 1; i <= n; i++) {
for (register int j = 1; j <= n; j++) vis[j] = 0;
ans += find(i);
}
return ans;
}
int main(){
while(scanf("%d", &n) != EOF) {
memset(first, 0, sizeof(first));
memset(nxt, 0, sizeof(nxt));
memset(to, 0, sizeof(to));
memset(Float, 0, sizeof(Float));
memset(vis, 0, sizeof(vis));
tot=0; cnt=0; ans=0;
for (register int i = 1; i <= n; i++) {
x = read();
t = read();
for (register int j = 1; j <= t; j++) {
y = read();
edge[++cnt].x = x+1;
edge[cnt].y = y+1;
add(x+1,y+1);
add(y+1,x+1);
}
}
Float[1] = 1;
Dfs(1);
build_map();
// cout<<black<<" "<<white<<endl;
// for(register int i = 1; i <= n; i++) cout<<Float[i];
int res = hungary();
printf("%d\n", res);
}
return 0;
}
[POJ1463] Strategic game的更多相关文章
- poj1463 Strategic game (树状dp)
Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 5498 Accepted: 2484 De ...
- poj1463 Strategic game【树形DP】
Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 9582 Accepted: 4516 De ...
- POJ1463 Strategic game (最小点覆盖 or 树dp)
题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆 ...
- poj1463 Strategic game[树形DP]
求一棵树每条边都被选上的点覆盖掉的最少选点数. 一条边被覆盖掉,必须他父亲和儿子中选一个..这不就是比NOIP2018D2T3还裸的暴力么.水掉. lyd给的练习题都什么**玩意儿.. code不挂了 ...
- 树形DP入门题目推荐以及解析
关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...
- POJ1463:Strategic game(树形DP)
Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...
- Strategic game树形DP解法(Poj1463,Uva1292)
已经写过本题用二分图的做法,见这儿. 本题的图是一棵树,求最小点覆盖也可以用树形DP的做法. 定义状态f[0/1][u]表示以u为根的子树,u选取/不选最少需要选取多少点来覆盖. 显然 f[0][u] ...
- Strategic game(无向?)二分图最小点覆盖(Poj1463,Uva1292)
原题链接 此题求二分图的最小点覆盖,数值上等于该二分图的最大匹配.得知此结论可以将图染色,建有向图,然后跑匈牙利/网络流,如下.然而... #include<iostream> #incl ...
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
随机推荐
- HDU4686 Arc of Dream —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4686 Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memo ...
- BZOJ 2002 Bounce 弹飞绵羊 —— 分块算法
题目链接:https://vjudge.net/problem/HYSBZ-2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Li ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...
- 人生苦短之Python类的一二三
在Python中,类也是以class开头定义的.我们定义一个动物类,它有名字和年龄,在java变量有实例变量和局部变量,方法内的变量是局部变量,类里面的变量是实例变量.那么在Python中的类及其属性 ...
- Oracle数据查看被锁住的用户
//lock_date是被锁住时间,如果为空证明这个用户没有被锁住 select username,lock_date from dba_users where username='GFMIS'; ...
- Java版本更新历史(ing)
历史版本特性 JDK Version 1.0 开发代号为Oak(橡树),于1996-01-23发行. JDK Version 1.1 于1997-02-19发行. 引入的新特性包括: 引入JDBC(J ...
- mysql 入门 1
连接mysql服务器 mysql -h localhost -u username -ppasswd 1.查看服务器存在的库 show databases; 2.创建数据库 create databa ...
- JS自动让手机调出软键盘,进行输入
$('.search').click(function(){ $('input[type=text]').focus(); //让input框自动聚焦就可以让手机自动调出软键盘 });
- having,groub by 结合聚合函数的用法解析
聚合函数有:sum , count, avg, max等等: where无法与聚合函数一起使用,所以在sql语句中加上having子句来筛选查询结果: 上面的sql语句是错的,正确如下: SELECT ...
- SpringMVC笔记- 不配置HandlerMapping
使用SpringMVC框架时发现有的配置了HandlerMapping,而有的没有,那么它们有什么区别呢?不配置能不能正常使用框架呢? 下面我们看一看不配置任何HandlerMapping时,框架会使 ...