HDU 3047 Zjnu Stadium(带权并查集,难想到)

M - Zjnu Stadium

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).

Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.

Input

There are many test cases:

For every case:

The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.

Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

Output

For every case:

Output R, represents the number of incorrect request.

Sample Input

10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100

Sample Output

2

Hint

 Hint: (PS: the 5th and 10th requests are incorrect) 

一个类似的题:https://www.cnblogs.com/yinbiao/p/9460772.html

code:
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<string.h>
#include<memory>
using namespace std;
#define max_v 50005
#define INF 9999999
int pa[max_v];
int sum[max_v];
int n,m;
int ans;
void init()
{
    for(int i=0;i<=n;i++)
    {
        pa[i]=i;
        sum[i]=0;
    }
}
int find_set(int x)
{
    if(pa[x]!=x)
    {
        int t=pa[x];
        pa[x]=find_set(pa[x]);
        sum[x]+=sum[t];//!!!
    }
    return pa[x];
}
void union_set(int a,int b,int v)
{
    int x=find_set(a);
    int y=find_set(b);
    if(x==y)
    {
        if(sum[a]-sum[b]!=v)//!!!
            ans++;
    }else
    {
        pa[x]=y;
        sum[x]=sum[b]-sum[a]+v;//!!!
    }
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        int x,y,w;
        ans=0;
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&x,&y,&w);
            union_set(x,y,w);
        }
        printf("%d\n",ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/yinbiao/p/9460838.html

时间: 2024-10-28 10:01:42

HDU 3047 Zjnu Stadium(带权并查集,难想到)的相关文章

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

HDU3047 Zjnu Stadium 带权并查集

转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <stack> #include <cstdlib> #include <algorithm> #includ

HDU 3172 Virtual Friends 带权并查集 -秩

ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集合的秩而不是0... #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <map&

hdu 2818 Building Block(带权并查集)

题目: 链接:点击打开链接 题意: 有N个积木,1到N编号.进行一些操作P次,M:X Y把X积木放到Y的上面,如果X和Y相等请忽略.C:X 计算X积木下的积木数目. 思路: 带权并查集的题目,定义数组sum[i]表示i积木下面积木的数目.遇到操作M,就把X和Y合并到同一个集合中.我们视每个结点为1个 Pile,其中rank[i]就表示每个Pile处的积木的个数,Initially, there are N piles, and each pile contains one block.所以,ra

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

hdu3047Zjnu Stadium 带权并查集

//n列个座位,排数为无穷 //m个询问 //a,b,x ,a在b前面x列 //问这m个询问与其前面询问冲突的有多少个 //带权并查集存下每个点到这个集合中最前的距离 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 50010 ; int F[maxn] ; int v[maxn] ; int n , m ; int find(int

HDU 3635 Dragon Balls(带权并查集)

题目地址:HDU 3635 加权并查集水题. 用num数组维护该城市有多少龙珠,用times数组维护每个龙珠运输了多少次.num数组在合并的时候维护.times数组由于每个都不一样,所以要在找根的时候递归来全部维护. 最终,x龙珠所在的城市就是x节点所在的根,x结点所在的跟的num数组值是该城市的龙珠数.times[x]为该龙珠运输了多少次. 代码如下: #include <iostream> #include <cstdio> #include <string> #i

hdu 5441 travel 离线+带权并查集

Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem 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 bidir

hdu 2818 Building Block (带权并查集,很优美的题目)

Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds