http://acm.hdu.edu.cn/showproblem.php?pid=3047

【题意】

http://blog.csdn.net/hj1107402232/article/details/9921311

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std; int n,m;
const int maxn=5e4+;
const int inf=0x3f3f3f3f;
int fa[maxn];
int rk[maxn];
void init()
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} int find(int x)
{
if(x==fa[x]) return fa[x];
int fx=find(fa[x]);
rk[x]+=rk[fa[x]];
return fa[x]=fx;
} void mix(int x,int y,int d)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
rk[fy]=rk[x]-rk[y]+d;
fa[fy]=fx;
}
} int query(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
{
return rk[y]-rk[x];
}
else
{
return inf;
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
memset(rk,,sizeof(rk));
int A,B,x;
int cnt=;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&A,&B,&x);
int d=query(A,B);
if(d==inf)
{
mix(A,B,x);
}
else if(d!=x)
{
cnt++;
}
}
printf("%d\n",cnt);
}
return ;
}

带权并查集

【带权并查集】HDU 3047 Zjnu Stadium的更多相关文章

  1. 带权并查集 HDU - 3047

    题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...

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

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

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

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

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

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

  5. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  6. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

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

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

  8. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  9. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

随机推荐

  1. 项目错误提示Multiple markers at this line

    新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line         - The type java.lang.Obje ...

  2. mybatis generator 整合lombok

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  3. JAVA Map的使用

    学JAVA那么多天了,所以就不写那啥了,哈哈 Map 是一个很实用的东西,它查询的速度也是飞快的.还有很多好的地方, 至于好在哪里,我也说不清. 还是用代码来说吧: import java.util. ...

  4. elasticsearch 2.4在head删除数据(使用Delete By Query插件)

    之所以说明是2.4版,是因为不同版本删除的语法不一样(例如跟5.x就不同) 首先安装Delete By Query插件,方式跟安装head插件差不多,安装命令:plugin install delet ...

  5. c++正则表达式模板库GRETA的使用

    GRETA是微软研究院的一位前员工开发并开源的一个C++正则表达式库,兼容perl正则语法 官方介绍:“A fast, flexible, perl-compliant regular express ...

  6. How exception works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=28 February 18, 2013 How exception work ...

  7. react基础语法(五) state和props区别和使用

    props的验证: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  8. html文本溢出显示省略字符的两种常用方法

    方法一:使用CSS溢出省略的方式解决 解决效果如下: css代码: display: -webkit-box; display: -moz-box; white-space: pre-wrap; wo ...

  9. 使用python书写的小说爬虫

    1.写了一个简单的网络爬虫 初期1 (后期将会继续完善) #小说的爬取 import requests import random from bs4 import BeautifulSoup base ...

  10. python中*号和**号的用法

    1.乘法符号 2.可变长参数 当我们使用函数时,需要传入不定个数的位置参数时,就可以使用*号表示,即*args,以元组形式传入:需要传入不定个数的关键字参数时,使用**表示,即**kwargs,以字典 ...