转: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. Ubuntu 14.04为浏览器添加Flash插件

    在刚安装好到Ubuntu操作系统中默认是没有flash支持到,因此,当我们使用浏览器查看很多视频网页到时候,会导致网页上到视频无法播放.然而,这个问题我们也不能够通过“软件中心”来解决,这时候需要我们 ...

  2. 【转】怎样将DataGridView中绑定的表的列名改成中文

    在DataGridView设置数据源绑定后,设置DataGridView的属性HeaderText就可以了.代码参考: dataGridView.Columns[filedName].HeaderTe ...

  3. How to fix “Duplicate sources.list entry …” issue

    The correct format of repository source line is <type of repository>  <location>  <di ...

  4. Cakephp 创建无模型的Controller

    控制器(Controller)如果没有特定的表/模型关联的话,哪怕建测试都会出错,但你可以加一行到控制器(Controller)里就好了public $uses=array(); 或者 public ...

  5. Python解析生成XML-ElementTree VS minidom

    OS:Windows 7 关键字:Python3.4,XML,ElementTree,minidom 本文介绍用Python解析生成以下XML: <Persons> <Person& ...

  6. Python的传值和传址与copy和deepcopy

    1.传值和传址 传值就是传入一个参数的值,传址就是传入一个参数的地址,也就是内存的地址(相当于指针).他们的区别是如果函数里面对传入的参数重新赋值,函数外的全局变量是否相应改变,用传值传入的参数是不会 ...

  7. AWR报告导出的过程报ORA-06550异常

    刚才在笔记本上(win 7)想要导出一套RAC的AWR报告(linux),执行awrgrpt.sql的脚本的过程中报错了,报错的异常代码是:ORA-06550.经过检查,发现是用户的问题,换成sys用 ...

  8. navigationController Pop回指定页面

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...

  9. AJax跨域请求百度音乐接口数据展示页面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. CSS3 animation的steps方式过渡

    animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果 是连贯性的.除了ease,linear.cubic-bezier之类的过渡函数都会为其插入补间. 但有些效果不 ...