首页 技术 正文
技术 2022年11月15日
0 收藏 720 点赞 2,550 浏览 1381 个字

传送门

思路:

  依题意可知,在图中的每一条边有且只有一个点被选中(阻止老曹刷街),那么就可以对其采取二分图染色,一条边中:一个点为黑色,另一个点为白色;如果一条边中的两个端点的颜色相同,则说明无解,输出:“ Ipossible “;如果有解,就把白点的数目和黑点的数目取 min ,即为答案。

标程:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<deque>
using namespace std;
#define maxn 100100
int n,m,cnt,ans;
int head[maxn],col[maxn],tot[maxn],vis[maxn];
struct hh
{
int to,nex;
}t[maxn<<];
inline int read()
{
int kr=,xs=;
char ls;
ls=getchar();
while(!isdigit(ls))
{
if(!(ls^))
kr=-;
ls=getchar();
}
while(isdigit(ls))
{
xs=(xs<<)+(xs<<)+(ls^);
ls=getchar();
}
return xs*kr;
}
inline void add(int nex,int to)
{
t[++cnt].nex=head[nex];
t[cnt].to=to;
head[nex]=cnt;
}
inline bool dfs(int now,int c)//c为1,染上白点;c为0,染上黑点;now要被染色的点
{
vis[now]=true;col[now]=c;tot[c]++;//vis记录该点被染色过,col记录这个点染什么颜色
for(int i=head[now];i;i=t[i].nex)//跑遍这个连通块的所有点
{
int v=t[i].to;
if(vis[v]&&col[v]==col[now]) return false;
else if(!vis[v])
{
bool sign=dfs(v,(c+)&);//另一个端点染的颜色相反(黑→白,白→黑)
if(!sign) return false;
}
}
return true;
}
int main()
{
int x,y;
n=read();m=read();
for(int i=;i<=m;i++)
{
x=read();y=read();
add(x,y);
add(y,x);//无向图加边
}
for(int i=;i<=n;i++)
{
if(!vis[i])//该连通块没被染过色
{
tot[]=tot[]=;//tot[0]为黑点,tot[1]为白点
bool sign=dfs(i,);//不仅能够判断有无解,还能染完总这个点起的连通块
if(!sign)//判断无解
{
printf("Impossible\n");
exit();//直接结束
}
else ans+=min(tot[],tot[]);//因为原图不连通,要加上所有连通块的答案。
}
}
printf("%d\n",ans);//输出
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922