【带权并查集】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.只需要判断有没有, ...
随机推荐
- 牛客网Java刷题知识点之自动拆装箱
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=5 ...
- PowerShell~执行策略的介绍
首先看一下无法加载ps1脚本的解决方法 事实上也是由于策略导致的 解决方法主是开启对应的策略 set-ExecutionPolicy RemoteSigned 执行策略更改 执行策略可以防止您执行不 ...
- Redis java操作客服端——jedis
1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...
- H+后台主题UI框架---整理(一)
本篇文章是对H+这种框架进行整理,顺便了解一下标准的代码规范的写法. 一.表单: 1).下面是一个基本表单: 现在来看这个表单的结构: 1.整个表单的外框结构是一个div,至于padding和marg ...
- array_keys
<?php$array = array(0 => 100, "color" => "red");print_r(array_keys($arr ...
- javaee 第五周作业
一.Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. ...
- laravel homestead comoser install 报错
项目部署的时候composer install报错 说那个依赖包没有安装成功需要回滚删除但是删除不了 解决: 要配置共享文件 注:使用 NFS 的话,需要安装 vagrant-winnfsd 插件.该 ...
- Oracle中的DDL,DML,DCL总结
转自http://blog.csdn.net/w183705952/article/details/7354974 DML(Data Manipulation Language,数据操作语言):用于检 ...
- 牛客网数据库SQL实战 1-11
1. 查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` ) NOT NULL, `birth_date` date NOT NULL, `first_ ...
- 详解Spring面向切面编程(AOP)三种实现
一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善. ...