博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU3047:Zjnu Stadium(并查集)
阅读量:6087 次
发布时间:2019-06-20

本文共 2584 字,大约阅读时间需要 8 分钟。

Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3256    Accepted Submission(s): 1257


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
Hint: (PS: the 5th and 10th requests are incorrect)
 

Source
题意:略。

思路:普通带权并查集。

# include 
# include
const int MAXN = 50000;int pre[MAXN+1], sta[MAXN+1];int find(int x){ if(x == pre[x]) return x; else { int t = pre[x]; pre[x] = find(pre[x]); sta[x] = sta[x] + sta[t]; return pre[x]; }}int main(){ int n, m, a, b, x, ans; while(~scanf("%d%d",&n,&m)) { ans = 0; for(int i=1; i<=n; ++i) pre[i] = i; memset(sta, 0, sizeof(sta)); while(m--) { scanf("%d%d%d",&a,&b,&x); int px = find(a); int py = find(b); if(px != py) { pre[py] = px; sta[py] = sta[a] + x - sta[b]; } else if(sta[b] - sta[a] != x) ++ans; } printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6729978.html

你可能感兴趣的文章
Django为数据库的ORM写测试例(TestCase)
查看>>
web.xml中的contextConfigLocation在spring中的作用
查看>>
NYOJ-107 A Famous ICPC Team
查看>>
与众不同 windows phone (44) - 8.0 位置和地图
查看>>
Visual Studio Code 使用 ESLint 增强代码风格检查
查看>>
iOS设备中的推送(二):证书
查看>>
敏捷 - #3 原则:经常提供工作软件 ( #3 Agile - Principle)
查看>>
数据结构与算法:二分查找
查看>>
使用思科模拟器Packet Tracer与GNS3配置IPv6隧道
查看>>
Linux设备驱动之Ioctl控制【转】
查看>>
iOS开发-NSPredicate
查看>>
《Clojure编程乐趣》—— 第1章,第1.2节为何(又一种)Lisp
查看>>
如何快速部署Node.js项目
查看>>
《移动App测试的22条军规》—App测试综合案例分析23.3节测试微信App的多任务和意外情况处理...
查看>>
《贝叶斯思维:统计建模的Python学习法》一1.6 M&M豆问题
查看>>
从代码层读懂HashMap的实现原理
查看>>
趋势在此汇集!2016杭州・云栖大会技术大咖专访系列合集
查看>>
Android应用框架之PackageManagerService
查看>>
Myexclipse创建Junit测试
查看>>
【Spark Summit East 2017】EasyMapReduce:利用Spark与Docker以MapReduce方式赋能大规模科学工具...
查看>>