首页 > ACM题库 > HDU-杭电 > HDU 3047-Zjnu Stadium-并查集-[解题报告]HOJ
2014
02-27

HDU 3047-Zjnu Stadium-并查集-[解题报告]HOJ

Zjnu Stadium

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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

带权的并查集

/*
 带权值的并查集
 ps:对于两个不在同一个集合里面的两个元素,把父节点接在一起即可
 */
 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
 #include<algorithm>
 #include<iostream>
 #include<queue>
 #include<map>
 #include<math.h>
 using namespace std;
 typedef long long ll;
 //typedef __int64 int64;
 const int maxn = 50015;
 const int inf = 0x7fffffff;
 const double pi=acos(-1.0);
 const double eps = 1e-8;
 
 int dis[ maxn ],fa[ maxn ];
 
 void init( int n ){
     for( int i=0;i<=n;i++ ){
         dis[ i ] = 0;
         fa[ i ] = i;
     }
     return ;
 }
 int find( int x ){
     if( fa[x]==x ) return x;
     int tmp = find( fa[x] );
     dis[ x ] = dis[ x ]+dis[ fa[x] ];
     dis[ x ] = dis[ x ]%300;
     fa[ x ] = tmp;
     return fa[ x ];
 }
 bool unionab( int a,int b,int c ){
     int x = find(a);
     int y = find(b);
     //printf("fa[%d]=%d,fa[%d]=%d,dis[%d]=%d,dis[%d]=%d\n",a,x,b,y,a,dis[a],b,dis[b]);
     if( x==y ){
         if( ((dis[b]-dis[a]+300)%300)!=c ){//b在a的后面,保证是dis[b]-dis[a]
             //printf("test:%d %d\n",a,b);
             return false;
         }
     }
     else{
         dis[ y ] = dis[ a ]+c-dis[ b ]+300;
         dis[y] %= 300;
         fa[ y ] = x;
     }
     return true;
 }
 int main(){
     int n,m;
     while( scanf("%d%d",&n,&m)==2 ){
         int a,b,c;
         init( n );
         int res = 0;
         while( m-- ){
             scanf("%d%d%d",&a,&b,&c);
             bool f = unionab( a,b,c );
             if( f==false ) res++;
         }
         printf("%d\n",res);
     }
     return 0;
 }

 

参考:http://www.cnblogs.com/justforgl/archive/2013/05/08/3066895.html


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }