转:http://blog.csdn.net/shuangde800/article/details/7983965

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int N=2e5+;
int n,m,fa[N],sum[N];
int find(int x)
{
if(x==fa[x])return fa[x];
int t=fa[x];
fa[x]=find(fa[x]);
sum[x]+=sum[t];
return fa[x];
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; ++i)fa[i]=i,sum[i]=;
int ans=;
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
int x=find(u),y=find(v);
if(x==y)
{
if(sum[v]-sum[u]!=w)++ans;
}
else if(x>y)
{
sum[x]=sum[v]-sum[u]-w;
fa[x]=y;
}
else
{
sum[y]=sum[u]-sum[v]+w;
fa[y]=x;
}
}
printf("%d\n",ans);
}
return ;
}

HDU3047 Zjnu Stadium 带权并查集的更多相关文章

  1. Hdu 2047 Zjnu Stadium(带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  3. HDU 3047 Zjnu Stadium(带权并查集)

    题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...

  4. HDU3047 Zjnu Stadium 【带权并查集】

    HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...

  5. Zjnu Stadium(hdu3047带权并查集)

    题意:一个300列的无限行的循环场地,a b d代表a,b顺时针相距d的距离,现在给你一些距离,判断是否有冲突,如果有冲突计算冲突的次数 思路:带权并查集 a,b的距离等于b到根节点的距离 - a到根 ...

  6. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  7. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  8. 【HDOJ3047】Zjnu Stadium(带权并查集)

    题意:浙江省第十二届大学生运动会在浙江师范大学举行,为此在浙师大建造了一座能容纳近万人的新体育场. 观众席每一行构成一个圆形,每个圆形由300个座位组成,对300个座位按照顺时针编号1到300,且可以 ...

  9. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

随机推荐

  1. RX学习笔记:Bootstrap

    Bootstrap https://getbootstrap.com 2016-07-01 在学习FreeCodeCamp课程中了解到Bootstrap,并于课程第一个实战题卡在响应式部分,于是先对B ...

  2. jquery 在页面中按回车 响应 事件

    为了用户方便我们往往会在用户回车之后做一些事,比如登陆的时候,填完表单过后,我们习惯性的会直接按回车,当然要处理这个,jquery是很简单的,我们来看看怎么做吧. $(document).ready( ...

  3. DataGridView绘制序号

    1.找到RowPostPaint事件 2.写入事件 /// <summary> /// 绘制序号 /// </summary> private void dgvStatemen ...

  4. "严格模式" use strict 详解

    一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...

  5. ASP.NET MVC 搭建简单网站 --1.前端页面布局和基本样式实现

    学技术这件事儿本来就是学习现有的东西,然后变成自己的,本文当然也是借鉴的别人的东西,写出来作为一个对知识的巩固.  1.网站用的是MVC模式,新建一个MVC项目,建立一个APP1Controller, ...

  6. thinkphp表单上传文件并将文件路径保存到数据库中

    上传单个文件,此文以上传图片为例,上传效果如图所示 创建数据库upload_img,用于保存上传路径 CREATE TABLE `seminar_upload_img` (  `id` int(11) ...

  7. 运行phpize失败排查

    ==相关参考== rpm包 http://rpmfind.net/linux/rpm2html/ phpize学习 ==问题及排查过程== 1.phpize失败 2.yum install php-d ...

  8. VirtualBox 安装虚拟机时出现错误 VT-x features locked or unavailable in MSR.

    修改安装好的虚拟机的cup的个数重新启动的时候报了上述错误. 参考博文:http://blog.csdn.net/zklth/article/details/7019990 错误:VT-x featu ...

  9. sizeof()函数求各类型变量所占空间的方法

    #include "stdafx.h" #include <iostream> using namespace std; ]) { cout<<sizeof ...

  10. 解决在构造函数中使用Session,Session为null的问题

    问题描述: public abstract class PageBase : System.Web.UI.Page 在PageBase中如何使用Session??? 我直接用 Session[&quo ...