【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的更多相关文章

  1. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  4. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【Uva 11584】Partitioning by Palindromes

    [Link]:https://cn.vjudge.net/contest/170078#problem/G [Description] 给你若干个只由小写字母组成的字符串; 问你,这个字符串,最少能由 ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. 绿色便携版Lazarus的制作教程

    本文来源: www.fpccn.com 原作者:逍遥派掌门人 http://msdn.microsoft.com/zh-cn/library/windows/apps/hh452791.aspx 本教 ...

  2. 分享:FIFO 同步、异步以及Verilog代码实现

    FIFO 很重要,之前参加的各类电子公司的逻辑设计的笔试几乎都会考到. FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,他与普通存储器的区别是没有外部读写地址线 ...

  3. centos7 nginx搭建及其反向代理

    摘要:nginx反向代理的原理:外部通过ip加端口访问nginx,nginx接收到外部请求,通过ip解析访问内部服务器,内部服务器再将数据传回Nginx服务器,而Nginx再把数据传回给外部客户机. ...

  4. 118.类包装器与lambda函数包装器(伪函数实现)

    #include <iostream> #include <list> using namespace std; //函数包装器,左边参数右边函数 template<cl ...

  5. 解决plsql遇到乱码的问题

    遇到问题: PLSQL中汉字显示乱码,并且SQL语句中如果有汉字,则执行时报错.其实并不是SQL语句写的有问题,而是系统不能识别汉字. 解决方法. 首先执行语句 select * from V$NLS ...

  6. c#(asp.net) 如何计算两个日期之间相隔天数

    1.DateTime t1 = Convert.ToDateTime("2006-1-6"); DateTime t2 = Convert.ToDateTime("200 ...

  7. 实现人脸识别性别之路---opencv

    import cv2from make_imge import get_file_namefrom train_ph import Modelimport os IMAGE_SIZE = 128if ...

  8. HDU-5307 He is Flying (FFT)

    Problem DescriptionJRY wants to drag racing along a long road. There are n sections on the road, the ...

  9. 压缩感知——SP(subspace pursuit)重构算法前言翻译

    压缩感知是一种採样方法,它和变换编码类似,后者被广泛用于涉及到大规模数据採样的现代通信系统中.变换编码将高维空间中的输入信号.转换成很低的低维空间中的信号.变换编码器的样例有著名的小波变换和普遍存在的 ...

  10. JNI之——Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform错误的解决

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46605003 在JNI开发中,Java程序须要调用操作系统动态链接库时,报错信息: ...