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. Centos系统安装 phpredis 扩展

    Git地址:https://github.com/nicolasff/phpredis 一.安装: phpize ./configure make && make install 其中 ...

  2. qconbeijing2018

    https://2018.qconbeijing.com/schedule 会议 · 第一天 (2018/04/20 周五) 时间 日程 上午 主题演讲 大数据下的软件质量建设实践 黄闻欣 出品 人工 ...

  3. oracle 权限、规则

    Oracle中关于权限与规则简单总结: --1,管理员登录 conn sys/orcl@orcl as sysdba; --2,创建用户方案---必须管理员身份才能操作 create user use ...

  4. 1. UI Tests简介

    (1) User Interface Testing UI Testing库主要提供了与App中的UI元素进行查找和交互的能力,这使得我们可以通过验证UI元素的状态来测试App是否正常运行.     ...

  5. ios MD5大小写加密

    #import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...

  6. IOS 根据身份证号码获取 年龄 生日 性别

    /** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...

  7. (C#)Xamarin.ios 发布到 App Store

    项目做到尾声了,IOS要发布,程序猿力Max来了. 不过就公司开发者账号就弄了一个月多,期间因为申请过D-U-N-S客服联系要公司资料时我们中途说取消了,后来再申请不知多少次了都没再回复... 给美国 ...

  8. MFC技术积累——基于MFC对话框类的那些事儿5

    4. 菜单 4.1 弹出菜单 本节主要讲解如何在主对话框的指定区域内通过鼠标右击来弹出一个菜单选项.最终效果图如图4.1. 如图4.1鼠标只能在指定区域(图中深色区域)内右击时弹出菜单,在指定区域外点 ...

  9. Docker下redis的主从配置

    1.拉取redis镜像docker pull redis2.启动3个redis容器服务,分别使用到6379.6380.6381端口docker run --name redis-6379 -p 637 ...

  10. 1.1 NLP基础技能,字符串的处理

    #!/usr/bin/env python # coding: utf-8 # # 字符串操作 # ### 去空格和特殊字符 # In[8]: s = " hello world! &quo ...