HDU 3047 带权并查集 入门题
Zjnu Stadium
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3047
Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.
Output
For every case:
Output R, represents the number of incorrect request.
Sample Input
10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100
Sample Output
2
Hint
(PS: the 5th and 10th requests are incorrect)
题意
1.n个观众标号1~n,坐在环形的观众席上,一个位置可以坐好几个人。
2.每条语句x y z,即y在x顺时针z距离处。
2.如果某条语句与之前的矛盾,则该条语句非法。
3.求非法语句个数。
题解
这道题是带权并查集的入门,因为我之前也没接触过,所以感觉做完这题,对并查集的理解深刻了不少,我也将自己的心得记录与此。
并查集的实质是维护森林关系的一种结构,我因为经常用路径压缩,所以忘记了它的本来样子。
最原始的并查集:
如果不进行路径压缩,合并完成之后会变成一个森林。每次合并时,我们是将两棵数合并成一棵,而我们只需将两个根合并即可。
那我问一个问题: 为什么要进行路径压缩呢?
1.我们需要查找两个点是否再同一集合,就需要判断根是否相同,而每次查找需要x次递归(x为其深度),这样时间复杂度就得不到保证了。
2.我们一开始很多题目不关心每个点的深度(深度也就是合并次数),也不关心边权。
3.综合以上两点,我们就通过改变数的结构,使得数的深度趋于两层。
题解从这开始,以上都是废话:
这题增加了距离,并查集讲究的就是相对性,我们只需要知道到根的距离,做差就是两点的距离了。
但是我一开始就觉得路径压缩了,怎么记录到根的距离呢?其实仔细想想还是可以想出来的。
x->fa[x]->fx,我们设fx为x的根,fa[x]为x现在的父节点。
那么dist[x->fx]=dist[x->fa[x]]+dist[fa[x]->fx],这样是不是路径压缩时,顺便把它到根的距离也就算出来了。
其余的应该都很好想了,其实有的时候不是想不出来,而是觉得想不出来,你在耐心想想,说不定就想出来了呢。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
const int mo=300;
int n,m,fa[N],val[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int find(int x)
{
if (x==fa[x])return x;
int y=fa[x];
fa[x]=find(fa[x]);
val[x]=(val[x]+val[y])%mo;//val[x]即x到当前根的距离,根变了,val跟着一起变
return fa[x];
}
bool merge (int x,int y,int z)
{
int fx=find(x),fy=find(y); if(fx==fy)return 0;
fa[fx]=fy;
val[fx]=(val[y]-val[x]+z)%mo;//这里好好想想哦,画个图就明白了
return 1;
}
void work()
{
int ans=0;
read(n); read(m);
for(int i=1;i<=n;i++)fa[i]=i;
memset(val,0,sizeof(int)*(n+1));
for(int i=1;i<=m;i++)
{
int x,y,z;
read(x); read(y); read(z);
if (merge(x,y,z))continue;
if((val[x]-val[y]+mo)%mo!=z)ans++;
}
printf("%d\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}
HDU 3047 带权并查集 入门题的更多相关文章
- Zjnu Stadium HDU - 3047 带权并查集板子题
#include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...
- 【poj 1962】Corporative Network(图论--带权并查集 模版题)
P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...
- HDOJ 3047 带权并查集
解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...
- HDU - 3038 带权并查集
这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...
- 并查集——poj2492(带权并查集入门)
一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...
- 并查集——poj1703(带权并查集入门)
传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...
- hdu 3038带权并查集
#include<stdio.h> #include<string.h> #define N 200100 struct node { int x,count; }pre[N ...
- hdu 1829 带权并查集的运用类似于食物链但是更简单些
#include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- poj1417(带权并查集+背包DP+路径回溯)
题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...
随机推荐
- Selenium执行cdp命令,driver.execute_cdp_cmd用法
Chrome自带的开发者工具DevTools功能非常强大.有时候我们在使用Selenium操作浏览器时需要通过调用一下DevTools的方法来完成一些设置,如模拟移动设备,弱网模拟等等. Seleni ...
- JAVA基础知识|HTTP协议-两个特性
一.无连接 无连接:服务器与浏览器之间的一次连接只处理一个http请求,请求处理结束后,连接断开.下一次请求再重新建立连接. 然而随着互联网的发展,一台服务器同一时间处理的请求越来越多,如果依然采用原 ...
- 【Golang】基于录制,自动生成go test接口自动化用例
背景 之前写过一篇博客,介绍怎么用Python通过解析抓包数据,完成自动化用例的编写.最近这段时间在使用go test,所以就在想能不能也使用代码来生成自动化用例,快速提升测试用例覆盖率.说干就干. ...
- iOS App的几种安全防范
虽然公司的项目目前还不算健壮,安全问题对于大部分小公司来说似乎并没什么必要,不过要攻击的话,我有十足的把握,我们是无法承受冲击的.嘿嘿嘿~不过带着一颗入坑iOS的心思,搜集了一下资料后,还是做了一些尝 ...
- zookeeper源码 — 三、集群启动—leader、follower同步
zookeeper集群启动的时候,首先读取配置,接着开始选举,选举完成以后,每个server根据选举的结果设置自己的角色,角色设置完成后leader需要和所有的follower同步.上面一篇介绍了le ...
- php 的生命周期
1.PHP的运行模式: PHP两种运行模式是WEB模式.CLI模式.无论哪种模式,PHP工作原理都是一样的,作为一种SAPI运行. 1.当我们在终端敲入php这个命令的时候,它使用的是CLI. 它就像 ...
- JNI调用C和C++存在的区别
JNI调用C和C++存在的区别 JNI是由C语言定义接口的,JNI通过函数名找函数入口,执行函数里的内容.这和函数用什么语言生成的并没有关系.只要保证函数名称符合JNI的协议.而使用C++要注意的 ...
- js和微信小程序本地获取东八北京时间
changeCount(){ // 目标时区,东8区 const targetTimezone = -8; // 当前时区与中时区时差,以min为维度 const dif = new Date().g ...
- kotlin中值范围
值范围表达式用rangeTo函数,该函数的操作符形式是二个点(..)另外还有in 和!in 相关操作符,任何可比较的大小的数据类型都可以定义值范围 值范围应用 fun main(arg: Array& ...
- 04Flutter仿京东商城项目 首页商品列表布局
Home.dart import 'package:flutter/material.dart'; import 'package:flutter_swiper/flutter_swiper.dart ...