【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. 3.React Native在Android中自己定义Component和Module

    React Native终于展示的UI全是Native的UI.将Native的信息封装成React方便的调用. 那么Native是怎样封装成React调用的?Native和React是怎样交互的? V ...

  2. UINavigationBar的系统渲染方式

    昨天想手工实现一下类知乎日报的Navigation Bar的动态颜色改变,但不管怎么设置Navigation Bar的 backgroundColor barTintColor alpha參数都达不到 ...

  3. Git的日常处理流程

    前提 本地有2个分支,一个是master,还有一个是local master 默认追踪origin/master local 通过git branch -u origin/master来映射 开发的时 ...

  4. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  5. 【Codeforces Round #459 (Div. 2) A】Eleven

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这个数列增长很快的. 直接暴力模拟看看是不是它的一项就好了 [代码] #include <bits/stdc++.h> ...

  6. python 服务端判断客户端异常断开

    在进行 python 套接字编程时,服务端程序要判断客户端是否异常断开[由于断电或者其他突发情况导致链接中断],可以通过以下几种方式判断: 1.如果通信协议中,设有心跳包,则可记录上次收到时间,将服务 ...

  7. 发送邮件被退回,提示: Helo command rejected: Invalid name 错误

    我自己配置的 postfix + dovecot server, 配置了outlook 后, 相同的账号. 在有的电脑上能收发成功, 在有的电脑上发送邮件就出现退信.提示 Helo command r ...

  8. EularProject 48: 利用数组求和

    Problem 48 The series, 11+22+33+...+1010=10405071317. Find the last ten digits of the series, 11+22+ ...

  9. Floodlight中 处理packetin消息的顺序(2)

         前面通过阅读代码知道了怎样推断各个模块处理某个消息的先后顺序.那么内部是怎样实现的呢?      每当一个模块表示对一个消息感兴趣的时候,就会调用IFloodlightProviderSer ...

  10. 华为荣耀3C最新版ROM的root,(4.7.1和4.8.1等等通用方法)

    手头一台honor 3c的机器.应该是线刷的时候,把IMEI给刷掉了.导致移动2G卡无法上网. 刷了4.7.1或者4.8.1.尝试了全部方法都root失败了. 正好我手头有6582的代码.我想.既然系 ...