【图论】【宽搜】【染色】NCPC 2014 A Ades
题目链接:
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的更多相关文章
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- POJ1426 Find The Multiple (宽搜思想)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24768 Accepted: 102 ...
- Colorado Potato Beetle(CF的某道) & 鬼畜宽搜
题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...
- BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子 ...
- 【宽搜】ECNA 2015 D Rings (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: 给你一张N*N(N<=100)的图表示一个树桩,'T'为年轮,'.'为空,求每个'T'属于哪一圈年轮,空 ...
- 【宽搜】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) ...
- 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ...
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
随机推荐
- atoi、stoi、strtoi区别
首先atoi和strtol都是c里面的函数,他们都可以将字符串转为int,它们的参数都是const char*,因此在用string时,必须调c_str()方法将其转为char*的字符串.或者atof ...
- RSA 加解密 签名 示例
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStrea ...
- PL/SQL 记录集合IS TABLE OF的使用
在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...
- heibernate增删改查总结一下自己的不足
难点也就我不熟悉的是数据库语句的书写,要加强复杂查询语句的书写 /* 简单的在共享类中已经可以用的了 * 总结: * * --------------------------------------查 ...
- retain two decimal digits.
package kju.o; import static kju.print.Printer.*; import java.text.*; class MathDemo { public static ...
- servlet 项目 ,,启动没问题,,但是,一请求也面就报错误。。。。求解决。。。。。。。。。。。。。各种百度,都没解决了啊。。。。。急急急急急急急急急急急急急急急急急急
信息: Server startup in 1674 mslog4j:WARN No appenders could be found for logger (com.mchange.v2.log.M ...
- visualSVN server库迁移(转)
转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...
- git config找到下载的路径URL
有时想在别的机器上下载自己的上传的源代码,发现git不像svn那么方便一眼就能看到下载地址,这两天在网上查了一下,终于找到方法了. 一般本地的github目录下总会有一个.git目录,点击进入,可 ...
- contos 安装jdk1.8
JDK安装配置 查看centos系统32位还是64位, 使用命令uname -a;x86是386,586系列的统称,主要是指指令集合.X64才是cpu对64位计算的支持版本. 1. 下载jdk,本例使 ...
- iframe中在父窗口打开子页面
我们在做页面框架的时候,通常会采用一个iframe来显示子页面,但有这么种情况,就是session失效时,登录页面就会显示在iframe中,这不符合常理,一般应该显示在顶部才对. 下面的js代码可以解 ...