HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩
思路跟 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 并查集带权路径压缩的更多相关文章
- 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 ...
- 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 ...
- 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 ( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
随机推荐
- oracle中的loop与while循环
Oracle中loop语句会先执行一次循环,然后再判断“exit when”关键字后面的条件表达式的值是true还是false,如果是true,那么将退出循环,否则继续循环. LOOP循环 语法如下l ...
- cocos2dx中的动作
CCAction是cocos2dx中专门用来处理动作相关的类,几乎所有的与动作相关的类都是从它派生而来的.而CCAction继承自CCObject class CCFiniteTimeAction : ...
- JPA学习---第五节:日期和枚举等字段类型的JPA映射
1.在上一节可在数据库中看到创建出来的表和字段,是通过 Entity bean 来创建的,而创建表名和字段名的规则是怎样的? 有类,代码如下: package learn.jpa.bean; impo ...
- LintCode-Hash Function
In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...
- XCODE真机调试设备连接一直忙碌如何处理
只是还没反应过来 等一会就行了
- java性能优化策略
1. 尽量使用局部变量代替成员变量,循环中对成员变量.方法的调用不超过2次 2. ArrayList如果知道大小,初始化时应指明 3. HashMap的遍历,用Entry 4. 如果确定类不可继承尽量 ...
- iframe嵌入其他网站,如何自适应高度
终于有一周时间,工作不那么忙了,腾出手来总结下工作过程中学到的知识. 每天遇到新问题,解决新问题,但是却很少有时间去仔细研究下,或者总结下.攒的多了,就得从头捋一遍. 说下iframe自适应高度: 搜 ...
- WPF 资源
https://wpftoolkit.codeplex.com/documentation http://www.codeproject.com/Articles/563862/Multi-Selec ...
- Unity3D脚本中文系列教程(十六)
Unity3D脚本中文系列教程(十五) ◆ function OnPostprocessAudio (clip:AudioClip):void 描述:◆ function OnPostprocess ...
- linux sort 命令详解(转 )
linux sort 命令详解 sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比 ...