题目链接:

  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. 11.3 afternoon

    迭代40 #include<cstdio> #include<cstring> using namespace std; int n,sum,falg; ],c[]; void ...

  2. 处理移动端click事件300ms延迟的好方法—FastClick

    下载地址:https://github.com/ftlabs/fastclick 1.click事件为什么有延迟? “...mobile browsers will wait approximatel ...

  3. 【转】纯 CSS 实现高度与宽度成比例的效果

    先来演示页面:Demo; 转的内容: 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 C ...

  4. 45种Javascript技巧大全

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  5. 传统的log4j实战

    /** * */ package log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator ...

  6. sql数值显示成千分位分隔符的形式

    ), )--带小数点 ), ),'.00','')--不带小数点

  7. OUTPUT、Merge语句的使用

    新版本的数据库中增加了OUTPUT子句,这个很好用,详细的使用方式大家可以参考SQL的联机帮助文档.这里仅记录下常用的场景:在对数据库进行增删改的时候我们有时候需要记录日志(这里指将日志记录在DB中而 ...

  8. Gridview分頁保存選項

    #region //'Revision: 1.00 Created Date: 2013/08/02 Created ID: Una [#1300071]增加多選框 /// <summary&g ...

  9. VC++读取资源中文件

    //查找目标资源 HRSRC hResource = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MAINPROG), TEXT(& ...

  10. [Client]动检参数讨论与ONVIF

    [问题]客户端访问ONVIF设备动检 客户端要访问ONVIF设备(IPC)的动检,一是事件,二是设置: 此处就是讨论如何设置动检区域的. 通过Video Analytics/Cell Motion D ...