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 带权并查集 入门题的更多相关文章

  1. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  2. 【poj 1962】Corporative Network(图论--带权并查集 模版题)

    P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...

  3. HDOJ 3047 带权并查集

    解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...

  4. HDU - 3038 带权并查集

    这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...

  5. 并查集——poj2492(带权并查集入门)

    一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...

  6. 并查集——poj1703(带权并查集入门)

    传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...

  7. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  8. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  9. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  10. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

随机推荐

  1. Liunx之MySQL安装与主从复制

    MYSQL安装(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...

  2. RabbitMQ 和 Kafka 的消息可靠性对比

    RabbitMQ和Kafka都提供持久的消息保证.两者都提供至少一次和至多一次的保证,另外,Kafka在某些限定情况下可以提供精确的一次(exactly-once)保证. 让我们首先理解一下上述术语的 ...

  3. allowMultiQueries=true mybatis 要多行sql执行,一定要注意

    allowMultiQueries=true 这个配置已经出现多次问题了,这次由于切换时多数据源,搞配置的同志不知道从哪里copy的配置,只换了我们的链接,我们之前配置了好多配置都丢失了,我的代码中有 ...

  4. Raspberry Pi 4B 使用OpenCV访问摄像头picamera模块

    目录 1.OpenCV安装 (1)安装依赖 (2)下载OpenCV源码 (3)安装pip (4)安装Python虚拟机 (5)编译OpenCV (6)验证安装 2.使用OpenCV和Python控制摄 ...

  5. (六)爬虫之使用selenium

    selenium是使用javascript编写,主要用来进行web应用程序测试,在python爬虫中可以用来进行动态网页爬取,解决爬虫中的javascript渲染(执行js语句).总结记录下,以备后面 ...

  6. 交互式绘图工具gunplot

    Gnuplot是一个命令行的交互式绘图工具(command-driven interactive function plotting program).用户通过输入命令,可以逐步设置或修改绘图环境,并 ...

  7. jmeter-Charles抓包显示的请求方式错误了,难道

    抓包显示的请求方式为get,但是get一直报错见上图 将get修改为post就正确了

  8. JAVA将两个byte数组合并为一个

    /** * 将两个byte数组合并为一个 * @param data1 要合并的数组1 * @param data2 要合并的数组2 * @return 合并后的新数组 */ public stati ...

  9. 004-jdk-数据结构-ArrayList、LinkedList

    一.ArrayList概述 数组集合,无容量限制,非线程安全 ArrayList.Vector是线性表,使用Object数组作为容器去存储数据的,添加了很多方法维护这个数组,使其容量可以动态增长,极大 ...

  10. python用户评论标签匹配的解决方法

    python用户评论标签匹配的解决方法 这篇文章主要为大家详细介绍了python用户评论标签匹配的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 我们观察用户评论发现:属性词往往和情感词伴 ...