(并查集 添加关系)How Many Answers Are Wrong --Hdu --3038
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3038
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/D
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std; #define N 200005 int n, m, f[N], r[N]; int Find(int x)
{
int k=f[x];
if(x!=f[x])
{
f[x]=Find(f[x]);
r[x]=r[x]+r[k];
} return f[x];
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
int ans=, i, a, b, t, fa, fb; for(i=; i<=n; i++)
{
f[i]=i;
r[i]=;
} for(i=; i<m; i++)
{
scanf("%d%d%d", &a, &b, &t);
a--;
fa=Find(a), fb=Find(b); if(fa!=fb)
{
f[fa]=fb;
r[fa]=r[b]-r[a]-t;
}
else
{
if(t!=r[b]-r[a])
ans++;
}
} printf("%d\n", ans);
} return ;
}
(并查集 添加关系)How Many Answers Are Wrong --Hdu --3038的更多相关文章
- 食物链--poj1182(并查集含有关系)
http://poj.org/problem?id=1182 题意应该就不用说了 再次回到食物链这道题,自己写了一遍,一直wa...原因竟然是不能用多实例,我也是醉了,但是我真的彻底的理解了,那 ...
- (并查集 带关系)Find them, Catch them -- poj -- 1703
链接: http://poj.org/problem?id=1703 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3676 ...
- D - How Many Answers Are Wrong HDU - 3038【带权并查集】
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- How Many Answers Are Wrong HDU - 3038 带边权并查集
#include<iostream> #include<cstring> using namespace std; ; int d[N],p[N]; int find(int ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- [kuangbin带你飞]专题五 并查集
并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...
- TOJ3955: NKU ACM足球赛(并查集+map+细节题)
时间限制(普通/Java):5000MS/15000MS 内存限制:65536KByte 描述 NKU ACM最近要举行足球赛,作为此次赛事的负责人,Lee要对报名人员进行分队.分队要遵循如下 ...
- Luogu P1195/P1892 口袋的天空/BOI团伙 【最小生成树/并查集】By cellur925
其实这俩题挺水的,团伙拿下了一血,但是感觉还是写一下博客比较好x. 一.团伙 题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是 ...
- 浅谈并查集 By cellur925【内含题目食物链、银河英雄传说等】
什么是并查集? 合并!查询!集合! 专业点说? 动态维护若干不重叠的和,支持合并查询的数据结构!(lyd老师说的) 数据结构特点:代表元.即为每个集合选择一个固定的元素,作为整个集合的代表,利用树形结 ...
随机推荐
- object-c语法
Objective-C:C的超集 Objective-Objective-C是C语言的严格超集--任何C语言程序不经修改就可以直接通过Objective-C编译器,在Objective-C中使用C语言 ...
- JDK和Tomcat部署时,版本不同的问题解决
问题: 在以Tomcat作为Web容器,启动java Web工程时,遇到下面问题:org.eclipse.jdt.internal.compiler.classfmt.ClassFormatExcep ...
- Haskell语言学习笔记(49)ByteString Text
Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...
- Mysql update 索引
执行mysql update,或者delete的时候会遇到: You can't specify target table for update in FROM clause 相关的原因自不必说:下面 ...
- Access to the path 'C:\inetpub\wwwroot\mysite\images\savehere' is denied.
访问路径被拒绝 我解决了这个设置: IIS>应用程序池> [您的站点]>高级设置...>标识>内置帐户> LocalSystem
- [leetcode]560. Subarray Sum Equals K 和为K的子数组
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- C# Socket网络编程精华篇
我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次模型 当然这里我们只讨论重要的四层 01,应用层(Application):应用层是个很广泛的概念,有一些基本 ...
- 【SQL模板】一.修改/新增存储过程TSQL
---Name: 创建存储过程模板.sql ---Purpose: 用于创建 数据库中 新的存储过程 ---Author: xx ---Time: 2015-12-18 10:26:06 ---Rem ...
- JS验证登录平台
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("Android" ...
- sql ltrim/rtrim 字段中为中文时出现?的问题
字段存储为中文,类型为nvarchar,使用ltrim时结果集中出现的问号,我的解决办法是:将问号replace掉