【Uva 11080】Place the Guards
【Link】:
【Description】
一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫.
【Solution】
二分图;
如果某个连通块不能形成二分图->不行!
如果能形成二分图.
则看看是在染成颜色0的位置放守卫还是在1的位置放守卫;
如果连通块只有1个的话,答案是1而不是0;
因为有说所有的点也要覆盖到的。
这题和最小点覆盖不同,最小点覆盖的话,两个取的点之间可以有边相连
最小点覆盖也没有涉及到单个的点的处理。
【NumberOf WA】
0
【Reviw】
【Code】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 200;
int n,m,color[N+20],cnt[2],ans;
vector <int> G[N+20];
int dfs(int x,int c){
color[x] = c;
cnt[c]++;
int len = G[x].size(),bo = 1;
rep1(i,0,len-1){
int y = G[x][i];
if (color[y]==-1)
bo &= dfs(y,1-c);
else
if (color[y]==color[x])
return 0;
}
return bo;
}
int main(){
//Open();
//Close();
int T;
ri(T);
while (T--){
rep1(i,1,N) G[i].clear();
ri(n),ri(m);
rep1(i,1,m){
int x,y;
ri(x),ri(y);
x++,y++;
G[x].pb(y),G[y].pb(x);
}
ans = 0;
int ok = 1;
ms(color,255);
rep1(i,1,n)
if (color[i]==-1){
cnt[0] = 0,cnt[1] = 0;
ok &= dfs(i,0);
if (cnt[0] + cnt[1] == 1){
ans+=1;
}else
ans+=min(cnt[0],cnt[1]);
}
if (!ok)
puts("-1");
else
oi(ans),puts("");
}
return 0;
}
【Uva 11080】Place the Guards的更多相关文章
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【Uva 11584】Partitioning by Palindromes
[Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...
- 【Uva 11400】Lighting System Design
[Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...
随机推荐
- vue20 父子组件数据交互
子组件使用父组件数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- php实现简单算法1
php实现简单算法1 <? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) functio ...
- zzulioj--1801--xue姐的小动物(水题)
1801: xue姐的小动物 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 594 Solved: 168 SubmitStatusWeb Boar ...
- 7. java操作MongoDB,采用_id查询
转自:https://www.2cto.com/database/201704/633262.html mongodb命令行_id查询方法 直接用ObjectId() db.getCollection ...
- JS对浏览器Cookie的操作,查询、设置以及删除
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...
- mysql 5.7 双主+主从配置
mysql5.7安装及赋权 wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm rpm -ivh mysql57 ...
- hiho week 38 P1 : 二分·二分答案
P1 : 二分·二分答案 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回和上上回里我们知道Nettle在玩&l ...
- android ActionBar的使用
Action Bar主要功能包括: 1. 显示选项菜单 2. 提供标签页的切换方式的导航功能,能够切换多个fragment. 3. 提供下拉的导航条目. 4. 提供交互式活动视图取 ...
- jquery08
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- jquery05 继承
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...