• 并查集的区间合并


    1、vj链接:  Restructuring Company  CodeForces - 566D 

    题意:1操作:合并x和y,2操作:合并x,x+1,x+2...y 操作3:查询x和y是否已经连在同一个父节点上

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int n,q;
     7 int f[200005],next[200005];
     8 int getf(int x)
     9 {
    10     return f[x]==x?x:f[x]=getf(f[x]);
    11 } 
    12 void merge(int x,int y)
    13 {
    14     int tx=getf(x);
    15     int ty=getf(y);
    16     if(tx!=ty)
    17     f[tx]=ty;
    18 }
    19 int a,b,c;
    20 int main()
    21 {
    22     scanf("%d%d",&n,&q);
    23     for(int i=1;i<=n;i++)
    24         f[i]=i,next[i]=i+1;
    25     while(q--)
    26     {
    27         scanf("%d%d%d",&a,&b,&c);
    28         if(a==1)
    29         {
    30             merge(b,c);
    31         }
    32         else if(a==2)
    33         {
    34             int tc=getf(c);
    35             for(int j=b;j<=c;)
    36             {
    37                 int t=j;
    38                 j=next[j];
    39                 f[getf(t)]=tc;
    40                 next[t]=next[c];
    41             }
    42         }
    43         else if(a==3)
    44         {
    45             int tx=getf(b);
    46             int ty=getf(c);
    47             if(tx==ty)
    48             printf("YES
    ");
    49             else
    50             printf("NO
    ");
    51         }
    52     }
    53 return 0;
    54 } 

    2、vj链接:Knight Tournament CodeForces - 357C

    Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.

    As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:

    • There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.
    • The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.
    • After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.
    • The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament.

    You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.

    Write the code that calculates for each knight, the name of the knight that beat him.

    Input

    The first line contains two integers nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ nli ≤ xi ≤ ri) — the description of the i-th fight.

    It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.

    Output

    Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.

    Examples

    Input
    4 3
    1 2 1
    1 3 3
    1 4 4
    Output
    3 1 4 0 
    Input
    8 4
    3 5 4
    3 7 6
    2 8 8
    1 8 1
    Output
    0 8 4 6 4 8 6 1 
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int t;
     6 int n,m;
     7 int f[300010];
     8 int nextt[300010];
     9 void init()
    10 {
    11     for(int i=1;i<=n;i++)
    12     f[i]=i,nextt[i]=i+1;
    13 }
    14 int getf(int x)
    15 {
    16     return f[x]==x?x:f[x]=getf(f[x]);
    17 }
    18 void merge(int x,int y)
    19 {
    20     int tx=getf(x);
    21     int ty=getf(y);
    22     if(tx!=ty)
    23     {
    24         f[tx]=ty;
    25     }
    26 } 
    27 struct node{
    28     int x;
    29     int y;
    30     int z;
    31 }ans[300010];
    32 int p=0;
    33 int a,b,c;
    34 int main()
    35 {
    36     cin>>n>>m;
    37     init();
    38     while(m--)
    39     {
    40         scanf("%d%d%d",&a,&b,&c);
    41         for(int j=a;j<=b;)
    42         {
    43             if(j==f[j])
    44             {
    45                 //printf("%d %d
    ",j,f[j]);
    46                 f[j]=c;
    47             }
    48             int t=j;
    49                 j=nextt[j];
    50                 if(t<c)
    51                     nextt[t]=c;
    52                 else
    53                     nextt[t]=nextt[b];
    54         }
    55     }
    56     for(int i=1;i<n;i++)
    57     {
    58         if(f[i]!=i)
    59             printf("%d ",f[i]);
    60         else -
    61             printf("%d ",0);
    62     }
    63     
    64     if(f[n]==n)
    65     printf("%d
    ",0);
    66     else
    67     printf("%d
    ",f[n]);
    68     
    69 return 0;
    70 }
    
    
  • 相关阅读:
    选择器高级,from提交方式
    java基础(5)
    java基础(4)
    java基础(3)
    java基础(2)
    java基础(1)
    数据库基础与ADO.NET和C#中的异常处理
    C#中字符串的常用方法
    Linux基本命令格式(3)
    面向对象第二章
  • 原文地址:https://www.cnblogs.com/1013star/p/9351255.html
Copyright © 2020-2023  润新知