HDU3038(KB5-D加权并查集)
How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15228 Accepted Submission(s): 5351
Problem Description
FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
Input
Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.
You can assume that any sum of subsequence is fit in 32-bit integer.
Output
Sample Input
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
Sample Output
Source
- #include <bits/stdc++.h>
- using namespace std;
- const int N = ;
- int fa[N], rk[N];
- void init(){
- for(int i = ; i < N; i++){
- fa[i] = i;
- rk[i] = ;
- }
- }
- int getfa(int x){
- if(x == fa[x])return x;
- int pre = fa[x];
- fa[x] = getfa(fa[x]);
- rk[x] += rk[pre];
- return fa[x];
- }
- bool merge_(int a, int b, int val){
- int af = getfa(a);
- int bf = getfa(b);
- if(af != bf){
- fa[af] = bf;
- rk[af] = rk[b]-rk[a]+val;
- return true;
- }else{
- return rk[a]-rk[b] == val;
- }
- }
- int main(){
- int n, m, a, b, val;
- while(cin>>n>>m){
- int ans = ;
- init();
- while(m--){
- cin>>a>>b>>val;
- a--;
- if(!merge_(a, b, val))
- ans++;
- }
- cout<<ans<<endl;
- }
- return ;
- }
HDU3038(KB5-D加权并查集)的更多相关文章
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bug ...
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- P1196 银河英雄传说(加权并查集)
P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在 ...
- Zjnu Stadium(加权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 洛谷 P2024 [NOI2001]食物链(种类并查集,加权并查集)
传送门 解题思路 加权并查集: 什么是加权并查集? 就是记录着每个节点到它的父亲的信息(权值等). 难点:在路径压缩和合并节点时把本节点到父亲的权值转化为到根节点的权值 怎么转化呢? 每道题都不一样Q ...
- UVALive 4487 Exclusive-OR 加权并查集神题
已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...
- 牛客网-Beauty of Trees 【加权并查集】
锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...
随机推荐
- ZJOI2019二试游记
ZJOI2019二试游记 Day -2 今天就要去被虐了!开一篇占个坑.禁赛警告 Day -1 早上zzy,下午zzq,无限懵逼... 过来的时候Sooke,memset0,老K坐我旁边,瑟瑟发抖.. ...
- 第57节:Java中流的操作以及编码解码
我的博客: https://huangguangda.cn/ https://huangguangda.github.io/ 前言: 编码解码:编码时将信息从一种形式变成为另一种形式,成为编码.编码为 ...
- sublimeText3编辑器 + 入门教程 + 使用大全
sublimeText3编辑器 + 入门教程 + 使用大全 Ctrl+D选中光标所占的文本,继续操作则会选中下一个相同的的文本 ctrl+G:输入行号,可快速跳转该行 ctrl+p:输入冒号,在输入行 ...
- FF中flash滚轮失效的解决方案
概述 在FF浏览器中有这样一个bug,就是当鼠标hover在flash区域的时候,滚轮会失效.原因是ff浏览器没有把滚轮事件嵌入到flash里面去.如果这个flash很小的话,比如直播的视频,会很容易 ...
- 应用监控CAT之cat-home源码阅读(三)
上两章从点到点讲了,cat-client 到 cat-consumer 的请求处理过程,但是怎么样让我们监控给人看到呢?那么就需要一个展示的后台了,也就是本章要讲的 cat-home 模块 ! 带 ...
- 引入CSS的三种方式
虽然入职已经大半年,并自诩前端工程师,但是我真的不会……(有一种我有罪我该死的感觉 从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种. 1.内联式 内联式css样式表就 ...
- VueJs(6)---V-on指令
V-on指令 一.概述 v-on是用来绑定事件监听器,用在普通元素上时,只能监听原生 DOM 事件.用在自定义元素组件上时,也可以监听子组件触发的自定义事件. 在监听原生 DOM 事件时,方法以事件为 ...
- 【云服务器部署】---Linux下安装nginx
[云服务器部署]---Linux下安装nginx 之前两篇,分别讲了:Linux下安装MySQL 和 springboot项目部署云服务器 nginx安装也是挺简单的.具体步骤如下: 第一步,下载 ...
- mysql 开发基础系列22 SQL Model
一.概述 与其它数据库不同,mysql 可以运行不同的sql model 下, sql model 定义了mysql应用支持的sql语法,数据校验等,这样更容易在不同的环境中使用mysql. sql ...
- 全网最详细的一款满足多台电脑共用一个鼠标和键盘的工具Synergy(图文详解)
不多说,直接上干货! 前言 如今无论你是在公司做大数据开发还是实验室里搞科研,这个软件确实好用,作为正在通往大数据架构师路上的我们没有几台电脑怎么行?台式机.笔记本,都放在写字台上,笔记本内置键盘鼠标 ...