思路跟 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. 如何访问Microsoft Azure Storage

    首先先要创建存储账户 http://www.cnblogs.com/SignalTips/p/4119128.html 可以通过以下的几个方式访问 通过Visual Studio 2013 Commu ...

  2. 含有自增序列的表中使用truncate与delete的不同结果

    一个含有自增序列的表,使用delete跟truncate之后会有什么不同结果呢? 大概说一下,使用truncate,表中的记录全部被清除,如果向表中插入数据,那么数据的排序是从1开始的. 如果使用的是 ...

  3. WPF-控件-层级控件-TreeView

    <?xml version="1.0" encoding="utf-8" ?> <Data xmlns=""> &l ...

  4. wrk 网站压力测试

    下载安装 wrk [root@aikaiyuan ~]# git clone https://github.com/wg/wrk.gitInitialized empty Git repository ...

  5. Linux C 文件与目录1 创建目录

    linux C    创建目录 创建目录函数:mkdir 函数原型:int mkdir(char * pathname , mode_t mode); pathname字符指针是表示需要创建的目录路径 ...

  6. js SVG

    Snap.svg Paths.js http://www.sitepoint.com/creating-animated-valentines-day-card-snap-svg/

  7. NPOI读取Excel数据应用

    NPOI 是 POI 项目的 .NET 版本.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它 ...

  8. 3640: JC的小苹果 - BZOJ

    让我们继续JC和DZY的故事.“你是我的小丫小苹果,怎么爱你都不嫌多!”“点亮我生命的火,火火火火火!”话说JC历经艰辛来到了城市B,但是由于他的疏忽DZY偷走了他的小苹果!没有小苹果怎么听歌!他发现 ...

  9. HDU 5486 Difference of Clustering 图论

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...

  10. SQL SERVER其它函数

    本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER常用的其它函数.(其它数据库这里就不罗列了,想看更多的可以关注<程序员的SQL金典>). 具 ...