思路跟 LA 6187 完全一样。

我是乍一看没反应过来这是个并查集,知道之后就好做了。

d[i]代表节点 i 到根节点的距离,即每次的sum。

 #include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ; int N, Q;
int p[MAXN];
int d[MAXN]; int FindSet( int x )
{
if ( p[x] == x ) return x;
int root = FindSet( p[x] );
d[x] += d[ p[x] ];
return p[x] = root;
} int main()
{
while ( ~scanf( "%d%d", &N, &Q ) )
{
for ( int i = ; i <= N; ++i )
{
d[i] = ;
p[i] = i;
} int cnt = ;
while ( Q-- )
{
int a, b, sum;
scanf( "%d%d%d", &a, &b, &sum );
--a;
int u = FindSet( a );
int v = FindSet( b );
if ( u == v )
{
if ( sum != d[b] - d[a] )
++cnt;
}
else
{
p[v] = u;
d[v] = d[a] - d[b] + sum;
}
} printf( "%d\n", cnt );
}
return ;
}

HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩的更多相关文章

  1. HDU 3038 How Many Answers Are Wrong (并查集)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. HDU 3038 How Many Answers Are Wrong (并查集)---并查集看不出来系列-1

    Problem Description TT and FF are ... friends. Uh... very very good friends -________-bFF is a bad b ...

  3. hdu 3038 How Many Answers Are Wrong

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...

  4. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  5. HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...

  6. hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  8. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

随机推荐

  1. zendframework 事件管理(一)

    zend里的事件管理器主要是为了实现: 1.观察者模式 2.面向切面设计 3.事件驱动构架 事件管理最基本的功能是将监听器与事件连接或断开.不论时连接还是断开都是通过shared collection ...

  2. 用Redis bitmap统计活跃用户、留存

    Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...

  3. 转载---linux运维相关

    前段时间,我在准备面试的时搜到的一套Linux运维工程师面试题,感觉比较全面,一直保存在草稿,刚在整理后台时翻了出来,干脆就发出来好了,以备不时之需. 1.linux如何挂在windows下的共享目录 ...

  4. Android系统SVC命令教程

    svc命令,位置在/system/bin目录下,用来管理电源控制,无线数据,WIFI # svc svc Available commands: help Show information about ...

  5. 【nodejs】jade模板入门

    使用jetbrians webstom创建空项目 1.创建package.json 引用依赖配置 { "name": "demojade", "des ...

  6. 【软件工程-Teamwork 3】团队角色分配和团队贡献分分配规则

    Part 1 团队角色分配 1.人员分配概要: Project Manager:1名 / Developer:4名 / Test: 1名 2.具体人员分配及职责: Project Manager(PM ...

  7. spicy及remote-viewer登录方法

    spicy登录: $sudo spicy remote-viewer登录: $ sudo /usr/local/bin/remote-viewer $ spice://192.168.70.158:4 ...

  8. About the Storage allocation

    It doesn't matter what programming language u use,it's all about the usage of variable---storage man ...

  9. [转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测

    Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大 ...

  10. 在ubuntu16.04 下安装haproxy 1.5.11 做tcp负载均衡

    由于haproxy需要FQ下载,所以从csdn下载了较为新版的haproxy1.5.11,安装过程如下: 1. 解压haproxy-1.5.11.tar.gz : tar xzvf haproxy-1 ...