【带权并查集】HDU 3047 Zjnu Stadium
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的更多相关文章
- 带权并查集 HDU - 3047
题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- 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 ...
- HDU3047 Zjnu Stadium 【带权并查集】
HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...
- 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 ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
随机推荐
- JavaScript整理
JavaScript是脚本语言 常用对话框: alert()——警告对话框,作用是弹出一个警告对话框 confirm()——带确定和取消按钮,返回True或false prompt()——弹出一个可以 ...
- java课程设计全程实录——第0天
本次课设计划在5月25日完成.目前还剩18天. 第0天主要完成事项如下: 搭建开发环境 制定开发进度规划表 阅读前人的课设 详细: 1.IDE的下载,安装,以及配合使用的阿里巴巴编程规约插件的安装与测 ...
- 架包Error inflating class错误
当引用架包后,出现Error inflating class错误时通常要检测架包是否正确引用: 1.首先将你所需要的架包拷贝到工程目录下: 2.右击工程,选择Build Path-->confi ...
- 洛谷 P1216 [USACO1.5]数字三角形 Number Triangles(水题日常)
题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...
- codevs 1043 方格取数 2000年NOIP全国联赛提高组
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 设有N*N的方格图(N<=10,我们将其中的某些方格中填入正整数,而 ...
- dnskeygen - 针对DNS安全性所生成的公共,私有和共享的密钥
SYNOPSIS(总览) dnskeygen [- [DHR ] size ] [-F ] -[zhu ] [-a ] [-c ] [-p num ] [-s num ] -n name DESCRI ...
- core 中使用 nlog
引包 代码 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) ...
- vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
- python之路——函数进阶
阅读目录 楔子 命名空间和作用域 函数嵌套及作用域链 函数名的本质 闭包 本章小结 楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x> ...
- QT +菜单栏和工具栏
#include "mainwindow.h" #include <QMenuBar>//菜单栏需要的头文件 #include <QMenu>//菜单 #i ...