HDU - 3038 / 3048 (带权并查集) (待补)

题目链接:点我点我

题意:

题解:

两题代码差不多,放个3047的。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int N=200010;
 7 int Father[N],value[N];
 8
 9 int find(int x){
10     if(x==Father[x]) return x;
11     int tmp=Father[x];
12     Father[x]=find(Father[x]);
13     value[x]+=value[tmp];
14     return Father[x];
15 }
16
17 int Union(int x,int y,int len){
18     int fx=find(x),fy=find(y);
19     if(fx==fy){
20         if(value[x]+len!=value[y]) return 0;
21         return 1;
22     }
23     Father[fy]=fx;
24     value[fy]=value[x]-value[y]+len;
25     return 1;
26 }
27
28 int main(){
29     int n,m;
30     int a,b,c;
31     while(scanf("%d %d",&n,&m)!=EOF){
32         int ans=0;
33         for(int i=0;i<=n;i++) value[i]=0,Father[i]=i;
34         for(int i=1;i<=m;i++){
35             scanf("%d %d %d",&a,&b,&c);
36             if(!Union(a,b,c)) ans++;
37         }
38         printf("%d\n",ans);
39     }
40     return 0;
41 }
时间: 2024-11-06 16:44:40

HDU - 3038 / 3048 (带权并查集) (待补)的相关文章

HDU - 3038 How Many Answers Are Wrong (带权并查集)

题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用,[a, b]和为s,所以a-1与b就可以确定一次关系,通过计算与根的距离可以判断出询问的正确性 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int f[MAXN],a

hdu 3038 How Many Answers Are Wrong【带权并查集】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出两个数N和M代表有N个数个M组数据 N个数是未知的,然后M组数据每组数据形如:l r x 代表位置l和位置r之间的数的和为x,最后求出M组数据中有 几组是与上面冲突的. 分析:这道题我直接就知道是带权并查集的题,原因是我直接按带权并查 集找的题,所以在这不能为诸君讲述有此类题如何联系到并查集的过程. 直接说方法吧,首先定义数据level[maxn]存相权值初始化为0,level[x]

HDU 3038 How Many Answers Are Wrong (带权并查集+区间判断)

题意:给你长度为n的区间,m个询问:a,b,c,问这m个问题有多少个是错误的(矛盾). 10 5 1 10 100 7 10 28 1 3 32 4 6 41 6 6 1 由6->6=1,  4->6=41 知4->5=40; 同理 由1->10=100,7->10=28 知1->7=72; 又由1->3=32,4-6=41 知1->7=73,与上面矛盾: 所以答案为1: #include<cstdio> #include<stdlib.h

HDU 3038 How Many Answers Are Wrong 带权并查集

分析:这一题和HDU3047一样,都是带权并查集,求后输入和先输入的冲突个数 然后其实就是用并查集维护一棵树,小的作为大的祖先,然后这棵树每个节点到根的路径权值是相对根节点的距离 这样就可以维护距离限制,判断冲突 #include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <stack> #include &l

HDU 3047 Zjnu Stadium 带权并查集

题目来源:HDU 3047 Zjnu Stadium 题意:给你一些人 然后每次输入a b c 表示b在距离a的右边c处 求有多少个矛盾的情况 思路:用sum[a] 代表a点距离根的距离 每次合并时如果根一样 判断sum数组是否符合情况 根不一样 合并两棵树 这里就是带权并查集的精髓 sum[y] = sum[a]-sum[b]+x 这里y的没有合并前b的根 #include <cstdio> #include <cstring> using namespace std; cons

HDU 5176 The Experience of Love (带权并查集 + 贪心)

The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 275    Accepted Submission(s): 111 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arrived

【带权并查集】HDU 3047 Zjnu Stadium

http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/details/9921311 [Accepted] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #incl

HDU 3172 Virtual Friends(带权并查集)

题目地址:HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #inclu

hdu 5441 Travel 离线带权并查集

Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m

Valentine&#39;s Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 91 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arriv