寒假Day28:并查集及其变形

 Zjnu Stadium

 HDU - 3047 

题面:

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)
View Code

关键部分:

int getf(int x)
{
    if(f[x]==x)
        return x;
    int t=f[x];//找它的父节点
    f[x]=getf(f[x]);// find root
    d[x]=d[x]+d[t];//d[x](为x到根节点的距离)更新为它到它父节点的距离+父节点到根节点的距离
    return f[x];
}

AC代码:

#include<stdio.h>
#include<map>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
const int N=50050;
int f[N],d[N],n;


int getf(int x)
{
    if(f[x]==x)
        return x;
    int t=f[x];//找它的父节点
    f[x]=getf(f[x]);// find root
    d[x]=d[x]+d[t];//d[x](为x到根节点的距离)更新为它到它父节点的距离+父节点到根节点的距离
    return f[x];
}

void merge(int x,int y)
{
    int t1=getf(x);
    int t2=getf(y);
        f[t2]=t1;
}

int main()
{
    int m;
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1;i<=n;i++)
            f[i]=i,d[i]=0;
        int ans=0;
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            int t1=getf(x),t2=getf(y);
            if(t1==t2)
            {
                if(d[x]+z!=d[y])
                    ans++;
            }
            else
            {
                merge(x,y);
                d[t2]=d[x]+z-d[y];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
View Code

How Many Answers Are Wrong

 HDU - 3038 

题面:

 1 TT and FF are ... friends. Uh... very very good friends -________-b
 2 
 3 FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
 4 
 5 Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
 6 
 7 Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
 8 
 9 The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
10 
11 However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
12 
13 What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
14 
15 But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
16 Input
17 Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.
18 
19 Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.
20 
21 You can assume that any sum of subsequence is fit in 32-bit integer.
22 
23 Output
24 A single line with a integer denotes how many answers are wrong.
25 
26 Sample Input
27 10 5
28 1 10 100
29 7 10 28
30 1 3 32
31 4 6 41
32 6 6 1
33 Sample Output
34 1
View Code

题意:

判断每个区间是否和之前的区间冲突,是的话ans++

注意:

多组输入,

dis需要清空

记录权值

int getf(int x)
{
    if(f[x]==x)
        return x;
    int t=getf(f[x]);
    dis[x]+=dis[f[x]];
    //当前点到父节点距离加上父节点到根节点距离就是当前点到根节点距离 
    return f[x]=t;

//    dis[x]+=dis[f[x]];
//    return f[x]=getf(f[x]);   WA
}

AC代码:

 1 #include<string.h>
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 #include<map>
 8 #include<cmath>
 9 using namespace std;
10 #define inf 0x3f3f3f3f
11 const int N =200050;
12 const int mod=1e9+7;
13 typedef long long ll;
14 
15 int f[N],dis[N];
16 
17 int getf(int x)
18 {
19     if(f[x]==x)
20         return x;
21     int t=getf(f[x]);
22     dis[x]+=dis[f[x]];
23     //当前点到父节点距离加上父节点到根节点距离就是当前点到根节点距离 
24     return f[x]=t;
25 
26 //    dis[x]+=dis[f[x]];
27 //    return f[x]=getf(f[x]);   WA
28 }
29 
30 int main()
31 {
32     int n,m;
33     while(~scanf("%d%d",&n,&m))
34     {
35         memset(dis,0,sizeof(dis));
36         for(int i=1; i<=n; i++)
37             f[i]=i;
38         int ans=0;
39         for(int i=1; i<=m; i++)
40         {
41             int a,b,c;
42             scanf("%d%d%d",&a,&b,&c);
43             a--;
44             int t1=getf(a);
45             int t2=getf(b);
46             if(t1!=t2)
47             {
48                 f[t2]=t1;
49                 dis[t2]=dis[a]+c-dis[b];
50                 // 更新b的祖先t2到a的祖先t1(也就是根)的距离 d(t2->t1)
51             }
52             else
53             {
54                 if(dis[b]-dis[a]!=c)
55                     ans++;
56             }
57         }
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
View Code

待解决:

针对上述题目:

a--不执行的话会wa

我的理解,避免端点重合不起来,左开右闭

但是对于这组数据又解释不通(a--后,我觉得是1,但是答案是0,不存在冲突)

10 3
1 10 10
1 4 2
4 8 10

POJ1611-The Suspects

题面:

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
View Code

题意:

求和0有关的数字有多少个?(包括0)

思路:

求解同一祖先下有多少个节点。(包括祖先)

解法一:

关键代码:

void merge(int x,int y)
{
    int t1,t2;
    t1=getf(x);
    t2=getf(y);
    if(t1!=t2)
    {
        f[t2]=t1;
        num[t1]=num[t1]+num[t2];
    }
    return ;
}

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#define N 31000
using namespace std;

int f[N],num[N],dis[N],m,n;
void init()
{
    for(int i=0; i<n; i++)
    {
        f[i]=i;
        num[i]=1;
    }
    return ;
}

int getf(int x)
{
    if(f[x]==x)
        return x;
    return f[x]=getf(f[x]);
}

void merge(int x,int y)
{
    int t1,t2;
    t1=getf(x);
    t2=getf(y);
    if(t1!=t2)
    {
        f[t2]=t1;
        num[t1]=num[t1]+num[t2];
    }
    return ;
}

//int dis[510][N];

int main()
{
    while(~scanf("%d %d",&n,&m)&&(m+n))
    {
        //??? ,n||m
        init();
        if(m==0)
        {
            printf("1\n");
            continue;
        }
        int i,j,k,a=0,b;
        while(m--)
            // for(j=0;i<m;j++)
        {
            //    memset(dis,0,sizeof(dis));
            scanf("%d",&b);
            for(i=0; i<b; i++)
                //  scanf("%d",&dis[a++][i]);
                scanf("%d",&dis[i]);
            for(i=0; i<b-1; i++) //注意一下
                merge(dis[i],dis[i+1]);
        }

//        int p,q=getf(0),sum=0;
//        for(i=0; i<n; i++)
//        {
//            p=getf(i);
//            if(p==q)
//                sum++;
//        }
         printf("%d\n",num[getf(0)]);
     //   printf("%d\n",sum);
    }
    return 0;
}
View Code

解法二:

AC代码:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 using namespace std;
 5 #define inf 0x3f3f3f3f
 6 
 7 int f[30020],a[30020],book[30020];
 8 
 9 int getf(int x)
10 {
11     if(f[x]==x)
12         return x;
13     return f[x]=getf(f[x]);
14 }
15 
16 void merge(int x,int y)
17 {
18     int t1=getf(x);
19     int t2=getf(y);
20     if(t1!=t2)
21         f[t2]=t1;
22 }
23 
24 int main()
25 {
26     int n,m;
27     while(cin>>n>>m)
28     {
29         if(n==0&&m==0)
30             break;
31         if(m==0)
32         {
33             cout<<1<<endl;
34             continue;
35         }
36         memset(book,0,sizeof(book));
37         for(int i=0; i<n; i++)
38             f[i]=i;
39         for(int i=1; i<=m; i++)
40         {
41             int k;
42             cin>>k;
43             for(int j=1; j<=k; j++)
44                 cin>>a[j],book[a[j]]=1;
45             for(int j=2; j<=k; j++)
46                 merge(a[1],a[j]);
47         }
48         int ans=0;
49         book[0]=1;
50         for(int i=0; i<n; i++)
51         {
52             if(book[i])
53             {
54                 int t1=getf(i);
55                 int t2=getf(0);
56                 if(t1==t2)
57                     ans++;
58             }
59         }
60         cout<<ans<<endl;
61     }
62     return 0;
63 }
View Code

POJ1182-食物链

 题目链接:POJ - 1182

思路:

把A看成1-N,B看成N+1~2N,C看成2N+1~3N

AC代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define inf 0x3f3f3f3f

int f[150020];

int getf(int x)
{
    if(f[x]==x)
        return x;
    else
        return f[x]=getf(f[x]);
}

void merge(int x,int y)
{
    int t1=getf(x);
    int t2=getf(y);
    if(t1!=t2)
        f[t2]=t1;
}

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int ans=0;
    for(int i=1; i<=3*n; i++)
        f[i]=i;
    for(int i=1; i<=k; i++)
    {
        int d,x,y;
        scanf("%d %d %d",&d,&x,&y);
        if(x>n||x<1||y<1||y>n)
        {
            ans++;
            continue;
        }
        int t1=getf(x);
        int t2=getf(y);
        int t3=getf(x+n);
        int t4=getf(y+n);
        int t5=getf(x+2*n);
        int t6=getf(y+2*n);
        if(d==1)
        {
//                if(t1==t4||t3==t6||t5==t2)
            if(t1==t4||t1==t6)
                ans++;
            else
            {
                merge(x,y);
                merge(x+n,y+n);
                merge(x+2*n,y+2*n);
            }
        }
        else 
        {
//                if(t1==t2||t3==t4||t5==t6)
            if(t1==t2||t1==t6)
                ans++;
            else
            {
                merge(x,y+n);
                merge(x+n,y+2*n);
                merge(x+2*n,y);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
View Code

Cube Stacking

 POJ - 1988 

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.

Write a program that can verify the results of the game.
Input
* Line 1: A single integer, P

* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.

Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Print the output from each of the count operations in the same order as the input file.

Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output
1
0
2
View Code
#include<stdio.h>
#include<map>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
const int N=300200;

int f[N],d[N],ji[N],n;


int getf(int x)
{
    if(f[x]==x)
        return x;
    int t=f[x];//找它的父节点
    f[x]=getf(f[x]);// find root
    d[x]=d[x]+d[t];//d[x](为x到根节点的距离)更新为它到它父节点的距离+父节点到根节点的距离
    return f[x];
}

void merge(int x,int y)
{
    int t1=getf(x);
    int t2=getf(y);
    f[t2]=t1;
}

int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        f[i]=i,d[i]=0,ji[i]=1;
    char a[2];
    for(int i=1; i<=n; i++)
    {
        scanf("%s",a);
        if(a[0]=='M')
        {
            int x,y;
            cin>>x>>y;
            //merge(x,y);
            int t1=getf(x); 1 6
            int t2=getf(y);
            if(t1!=t2)
            {
                f[t1]=t2;//define父节点
                d[t1]=ji[t2];//几个元素
                ji[t2]=ji[t2]+ji[t1];//对t2的集合元素个数更新
            }
        }
        else
        {
            int x;
            cin>>x;
            getf(x);
            cout<<d[x]<<endl;
        }
    }
    return 0;
}
View Code

待解决:

这边的数组大小开到题目给定的N的大小不知道为什么会RT

M那边不知道为什么要把t2当作父节点,换一下就WA

Thinking

所有的代码都会在原有基础上越写越简单,越写越精炼,越写越快,

三月份所有考试去小区,四月考位锁定,五月换题,只能安排在五月~

不知道接下来还会发生什么事情~

加油!!!

原文地址:https://www.cnblogs.com/OFSHK/p/12309673.html