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 ...
随机推荐
- 爱上python之盲注探测脚本
本文转自:i春秋论坛 前言: 最近在学python,做了个盲注的简单的跑用户的脚本,仅做个记录. sqmap也有不灵的时候,有时需要根据情况自写脚本探测 正文: 本地用大表姐给的sql和p ...
- 第39节:Java当中的IO
Java当中的IO IO的分类 第一种分:输入流和输出流 第二种分:字节流和字符流 第三种分:节点流和处理流 节点流处理数据,处理流是节点流基础上加工的. IO中的重点: InputStream Ou ...
- oracle select in超过1000条报错解决方法
本博客介绍oracle select in超过1000条数据的解决方法,java框架是采用mybatis的,这可以说是一种比较常见的错误:select * from A where id in(... ...
- editormd实现Markdown编辑器写文章功能
想在项目里引入Markdown编辑器实现写文章功能,网上找到一款开源的插件editormd.js 介绍网站:https://pandao.github.io/editor.md/examples/in ...
- 网页的异步请求(Ajax)
JS原生Ajax操作(XMLHttpRequest) GET请求 var xmld=new XMLHttpRequest(); xmld.open("GET","wan. ...
- 学习笔记第六课 VB程序
VB程序的特殊地方在于: 前几课学的破解方法,诸如设置API断点,修改关键CALL的返回值,MESSAGEBOX断点等,这些对于VB程序都是无效的. 这节课是设置VB的API断点,绕过报错弹窗来破解. ...
- Java核心技术及面试指南 流程控制方面的面试题答案
2.2.5.1 switch语句能否作用在byte上,能否作用在long上,能否作用在String上? 1 switch里可以用char,byte,short,int这些基本类型,以及它们的封装类. ...
- VSCode typescript ctrl+shift+b can't be compiled error:TS5007
环境: vscode:1.12.2 node 7.4.0 TypeScript:2.3.2 从svn 更新下来,别的电脑环境编译是没问题的,在我的电脑上编译失败并出现以下错误 error TS5007 ...
- 【python35小工具】b站弹幕保存
后面有时间学习弹框,加个弹框 效果图: 原理: 原理简单不繁琐 1 根据设置的参数去post请求json参数 2 json提取出用户名和时间以及弹幕内容 (其中值得注意的时,页面默认会保存10个弹幕, ...
- 大数据入门基础系列之Hadoop1.X、Hadoop2.X和Hadoop3.X的多维度区别详解(博主推荐)
不多说,直接上干货! 在前面的博文里,我已经介绍了 大数据入门基础系列之Linux操作系统简介与选择 大数据入门基础系列之虚拟机的下载.安装详解 大数据入门基础系列之Linux的安装详解 大数据入门基 ...