【图论】【宽搜】【染色】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& ...
随机推荐
- ORACLE解锁数据库用户
the account is locked解决办法: 1.进入sqlplus sqlplus "/as sysdba" 2.解锁: alter user hpmng account ...
- iOS开动画效果之──实现 pushViewController 默认动画效果
在开发中,视图切换会常常遇到,有时我们不是基于导航控制器的切换,但实际开发中,有时需要做成push效果,下面将如何实现push和pop 默认动画效果代码实例: 一.push默认动画效果 CATrans ...
- java.lang.reflection打印一个类的全部信息
package com.ljy.chapter5; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...
- (java)从零开始之--装饰者设计模式
装饰者设计模式:简单定义:增强一个类的功能,而且还可以让这些装饰类互相装饰. 应用场景:当要在某个功能的基础上扩充功能,并且扩充的功能具有大量排列组合,通过继承关系会衍生出大量子类,这时候用装饰者模式 ...
- SGU 106.Index of super-prime
时间限制:0.25s 空间限制:4M 题目大意: 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...
- WHU 1579 Big data (DP)
题意: f[0]=0,f[i]=f[i-1]+a or b. 求满足L<=∑f[n]<=R的序列的种数 n<100. |a|,|b|<=10000. |L|,|R|< ...
- 【BZOJ3211】【并查集+树状数组】花神游历各国
Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...
- Mvvm绑定datagrid或listview的selectItems的方法[转]
单选,很简单,将SelectedItem与ViewModel的属性进行双向绑定就OK了 多选,由于ListView的SelectedItems不能进行绑定,需要将ListView的SelectionC ...
- php笔记之GD库图片创建/简单验证码
燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.in ...
- magento前台访问错误
前台访问出现错误 General error: 145 Table './dbname/tablename' ismarked as crashed and should be repaired 解决 ...