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个恶魔,天使只说 ...
随机推荐
- W: GPG error: http://ppa.launchpad.net trusty InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8CF63AD3F06FC659
报错信息: W: GPG error: http://ppa.launchpad.net trusty InRelease: The following signatures couldn't be ...
- 二十八、CentOS系统光盘安装、anaconda概述
常见问题你会感觉 tftp timeout: 防火墙 time out script: 网关没有指定,在dhcpd.conf中 不能下载:vmlinuz和initrd程序和安装的系统版本不一致 内存必 ...
- 八十年代的经典 NFS和AFS
NFS:(C/S模式) 大体架构: 1985年Sun公司基于UDP开发了网络共享文件系统(Network FileSystem) NFS由一系列NFS命令和进程组成的客户机/服务器模式,在第三版中加入 ...
- 在testrpc以太坊测试环境部署智能合约
2018年03月13日 09:20:54 思无邪-machengyu 阅读数 2683 版权声明:本文为博主原创文章,转载请务必注明出处,否则追究法律责任 https://blog.csdn.ne ...
- linux内核中的wait_event_interruptible_timeout接口解析
1. 原型 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \ ({ \ long __ret = time ...
- MySQL SSL配置(mysql5.7和mysql5.6)
专题一:mysql5.7上开启并配置ssl [root@mysqlmaster01 bin]# ./mysql_ssl_rsa_setup --datadir=/data/mysql_data1/ - ...
- 一百三十:CMS系统之七牛js和python的SDK使用示例
1.安装: pip install qiniu 2.编写获取uptoken的接口 @app.route('/uptoken/')def uptoken(): access_key = '' secre ...
- CentOS7 升级 python3 过程及注意
• 从官网下载python3的压缩包,解压(以3.5.1版本为例)• 创建安装目录(自定义)sudo mkdir /usr/local/python3• cd 进入解压目录sudo ./configu ...
- 图解 HTTP 笔记(五)——Web 服务器
该章的主要内容是讲解与 HTTP 协作的 Web 服务器 一.用单台虚拟主机实现多个域名 基于虚拟主机的功能,可以只使用一台物理机实现多个域名的网站部署. 在互联网上,域名通过 DNS 域名解析系统可 ...
- REPLACE 语法
转自:https://www.cnblogs.com/jiangzhengjun/p/4292994.html#_Toc411766043 REPLACE REPLACE [{FIRST OCCURR ...