题目链接:

  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1787

题目大意:

  N个点M条无向边(N,M<=200000),一个节点只能有一个标记。每条边有一个值{0,1或2}表示这条边连接的两个节点拥有的标记之和。问只要要多少个标记才能满足,无解impossible。

题目思路:

  【图论】【宽搜】

  因为每个点只能有或没有标记。所以可以枚举每个联通块的其中一个点有还是没有标记,用这个点去拓展这个点的联通块并01染色(这个点所能到达的所有点)

  初始点标记为0需要的标记数为sumz,初始点为1的标记数为sumo,选取能够满足的加到答案,如果能够满足图的要求就取min。

  每个点只会被走过一次,所以是O(N)的。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 200004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int q[N],last[N];
int mark[N][];
bool u[N];
struct xxx
{
int next,to,d;
}a[N+N];
void add(int x,int y,int z)
{
a[++lll].to=y;
a[lll].d=z;
a[lll].next=last[x];
last[x]=lll;
}
bool spfa(int s)
{
int i,l=,r=,now,to,sumz=,sumo=;
bool z=,o=;
q[]=s;mark[s][]=,mark[s][]=;u[s]=;
while(l!=r)
{
now=q[l=(l+)%N];
sumz+=mark[now][];sumo+=mark[now][];
for(i=last[now];i;i=a[i].next)
{
to=a[i].to;
if(mark[to][]==-)mark[to][]=a[i].d-mark[now][];
if(mark[to][]==-)mark[to][]=a[i].d-mark[now][];
if(mark[now][]+mark[to][]!=a[i].d || mark[to][]< || mark[to][]>)z=;
if(mark[now][]+mark[to][]!=a[i].d || mark[to][]< || mark[to][]>)o=;
if(!u[to])
{
u[to]=;
q[r=(r+)%N]=to;
}
}
if(z && o)break;
}
if(z && o)return ;
if(!z && o)ans+=sumz;
else if(z && !o)ans+=sumo;
else if(!z && !o)ans+=min(sumz,sumo);
return ;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
mem(u,);mem(mark,-);
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
ans=;
for(i=;i<=n;i++)
{
if(u[i])continue;
if(!spfa(i))break;
}
if(i<=n)puts("impossible");
else printf("%d\n",ans);
}
return ;
}
/*
// //
*/

【图论】【宽搜】【染色】NCPC 2014 A Ades的更多相关文章

  1. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  2. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  3. POJ1426 Find The Multiple (宽搜思想)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24768   Accepted: 102 ...

  4. Colorado Potato Beetle(CF的某道) & 鬼畜宽搜

    题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...

  5. BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子 ...

  6. 【宽搜】ECNA 2015 D Rings (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: 给你一张N*N(N<=100)的图表示一个树桩,'T'为年轮,'.'为空,求每个'T'属于哪一圈年轮,空 ...

  7. 【宽搜】ECNA 2015 E Squawk Virus (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: N个点M条无向边,(N<=100,M<=N(N-1)/2),起始感染源S,时间T(T<10) ...

  8. 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ...

  9. 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

随机推荐

  1. oo面向对象原则

    1.单一职责原则 一个类,最好只做一件事,只有一个引起他变化的原因否则就应该考虑重构. 2.开放封闭原则 软件实体应该是可扩展的,而不是可修改的.也就是说对扩展开放,对修改封闭.主要体现在两个方面: ...

  2. ORACLE每组只保留一条记录

    删除同一组内其他记录 DELETE from memactivities a where exists(select 1 FROM (select Uuid,ci_no,lst_upd_ts,ROW_ ...

  3. SDK Manager.exe 无法启动,一闪而过的解决办法

    删掉 C:\Windows\system32\ 下的 java.exe.javaw.exe.javaws.exe 即可解决.(转载)

  4. 安装SQL Server2008时 检测时有“重启计算机”失败

    第一种解决方案: 在学校的时候 遇到这种问题的解决办法是: 卸载VS,先安装SQL Server 2008 再安装VS 就行了: 第二种解决方案: 如果已经安装过VS,在安装SQL Server200 ...

  5. Struts2默认拦截器配置

    http://blog.csdn.net/axin66ok/article/details/7321430

  6. WampServer修改端口及菜单Localhost

    一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: 3.将 80 改成 8080(当然自己也可以设定别的不使用的 ...

  7. 【POJ2185】【KMP + HASH】Milking Grid

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  8. WPF之核心面板(容器)控件简单介绍

    一.Canvas 1.官方表述:定义一个区域,在该区域中可以使用相对于该区域的坐标显式定位子元素. 2.对于canvas 的元素的位置,是靠控件的大小及Canvas.Top.Canvas.Left.C ...

  9. Magento 2.0 安装

    环境: 直接升到最新版PHP5.6.x 刚才开MAC OS PHP 5.5  CENTOS PHP 5.5  composer install  依懒包错误.反复安装组件.还是不行.后来决定重新编释最 ...

  10. java 多线程sleep和wait的区别

    对于sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监 ...