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 ...
随机推荐
- Microsoft Press Free eBook
微软的免费的电子书, 都是Microsoft Press 出版的 有以下价格方面 Windows Server(大体上都是Windows Server 2012 ) Microsoft Azure(好 ...
- MySQL不同库名相同表结构实现主从配置
数据库版本 5.6.16 在两个服务器上,存在不同名字的数据库,但是两个数据库中的所有表结构相同,实现主从复制. 主库服务器的数据库名为yoon,从库服务器的库名为hank 在从库的my.cnf配置文 ...
- How to create jar for Android Library Project
http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project This works ...
- GNOME与KDE的战争
目录1 序言2 GNOME与KDE交替发展% M" O/ h% R( b f, ~7 W' n9 V, G3 GNOME获得商业公司的支持4 KDE3.5可实现半透明和阴影效果,界面华丽. ...
- Windows 10 响应式设计和设备友好的开发
使用Effective pixels有效像素设计UI 什么是缩放像素和Effective有效像素: 当你的应用程序运行在Windows的设备,系统用一个算法控制的规范,字体,和其他UI元素显示在屏幕上 ...
- Madwifi Mad coding:自底向上分析associated_sta的更新过程 —— RSSI和MACADDR等信息获取的底层原理
Madwifi驱动工作在AP模式下时,可以在/proc/net/madwifi/ath0/associated_sta文件中得到所有接入的用户的MAC地址.实时平均RSSI,和last_rx三个信息. ...
- ios 存储学习笔记
一.主要路径: Library/Caches/此文件用于存储那些需要及可延迟或重创建的临时数据.且这些内容不会被IOS 系统备份,特别地,当设备磁盘空间不足且应用不在运行状态时,IOS 系统可能会移除 ...
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- oracle 插入timestamp
示例: insert into tpurview(IPURVIEWID,CPURVIEWNAME,COPERATENAME,IPARENTID,DADDTIME,DEDITTIME,CADDUSER, ...
- C/C++ 快速排序 quickSort
下面的动画展示了快速排序算法的工作原理. 快速排序图示:可以图中在每次的比较选取的key元素为序列最后的元素. #include <stdio.h> #include <stdlib ...