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. poj1857 To Europe! To Europe!

    思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...

  2. Excel数据导入SQL Server

    基本有2种方案,都是无需安装Office的方案 Ole DB读取 + BulkCopy 获取Excel各个SheetName //连接串 string strConn = "Provider ...

  3. 洛谷 P1618 三连击(升级版)

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数的比例是A:B:C,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. //感谢黄小U饮品完善题意 输入输出格 ...

  4. 下拉列表事件 Dropdown iview

    <Dropdown @on-click="export"> <Button icon='md-log-out'> 000l <Icon type=&q ...

  5. PyTorch如何构建深度学习模型?

    简介 每过一段时间,就会有一个深度学习库被开发,这些深度学习库往往可以改变深度学习领域的景观.Pytorch就是这样一个库. 在过去的一段时间里,我研究了Pytorch,我惊叹于它的操作简易.Pyto ...

  6. 理解 Word2Vec 之 Skip-Gram 模型

    理解 Word2Vec 之 Skip-Gram 模型 天雨粟 模型师傅 / 果粉 https://zhuanlan.zhihu.com/p/27234078 508 人赞同了该文章 注明:我发现知乎有 ...

  7. http请求响应格式

    当浏览器向Web服务器发出请求时,它向服务器传递了一个数据块,也就是请求信息,HTTP请求信息由3部分组成:l   请求方法URI协议/版本l   请求头(Request Header)l   请求正 ...

  8. Manjaro/Arch linux 安装输入法

    输入命令: sudo pacman -Syu fcitx fcitx-googlepinyin fcitx-im fcitx-configtool 编辑 ~/.xprofile:# xfcitxexp ...

  9. 洛谷——P4932 浏览器

    P4932 浏览器 __stdcall给了你n个点,第i个点有权值x[i],对于两个点u和v,如果x[u] xor x[v]的结果在二进制表示下有奇数个1,那么在u和v之间连接一个Edge,现在__s ...

  10. 洛谷 P1518 两只塔姆沃斯牛

    P1518 两只塔姆沃斯牛 The Tamworth Two 简单的模拟题,代码量不大. 他们走的路线取决于障碍物,可以把边界也看成障碍物,遇到就转,枚举次,因为100 * 100 * 4,只有4个可 ...