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 ...
随机推荐
- 1017. Queueing at Bank (25)
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- 从零开始学ios开发(十九):Application Settings and User Defaults(上)
在iphone和ipad中,有一个东西大家一定很熟悉,那个东西就是Settings. 这次要学习的东西说白了很简单,就是学习如何在Settings中对一个app的某些属性进行设置,反过来,在app中更 ...
- 【采集】php str_replace
<?php function my_str_replace($xmlHttp,$order='asc'){ if($order=='asc'){ return str_replace(array ...
- 在Visual Studio 2010 中创建类库(dll)
创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...
- error: format not a string literal and no format arguments [-Werror=format-security]
You can put this in your Application.mk to disable treating those warnings as errors: APP_CFLAGS += ...
- Text selection in div(contenteditable) when double click
背景: 在最近项目中,碰到一个问题:有一个可编辑的div需要双击时可编辑,blur或者回车时将编辑结果保存.你可能注意到双击时,文字会被选中,可编辑区域不会focus到光标位置.考虑到兼容性问题,写了 ...
- How does java technology relate to cloud computing?
Java Paas shootout (@IBM developer) Cloud computing is always a hot topic around IT field today.Ho ...
- Pentaho Data Integration笔记 (一):安装
介绍 Pentaho Data Integration (PDI) is an extract, transform, and load (ETL) solution that uses an inn ...
- c++ 信号量
最近写了个c++项目,发现 ctrl + c 退出程序可以出发析构,但kill pid,则不会触发析构,导致现场数据丢失. 解决方案:同时捕捉以下信号. signal(SIGINT, &完成命 ...
- BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树
题目链接: 题目 2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB 问题描述 a180285非常喜欢滑雪.他来到一座雪山, ...