Connections in Galaxy War-- reverse disjoint-set

Topic Link

Meaning of the questions:

First, a given number n and n represents give you an energy magnitude of each star p [i] is then given a relationship between the input connection and m and m stars

To give a fixed line q q queries if the Query x represents x number of star connected and the star most energy disruption of the connection between the numbers x and y sun destroy xy represents if the sun

answer:

The traditional approach is to first input point is added disjoint-set build relationships, and then began to ask judge, will destroy the relationship between encountered two points off, but it is clear that such a relationship is very difficult to connect it disconnect

Zhengnanzefan

We not destroy the first star connected, so that we can begin to determine under and record the results from the last one to ask, when it comes to destroy and then establish the relationship between these two points, so destroy the front of the inquiry can properly judge the

How to not destroy the star connected?

First connection relationships of all the planet save up, and then destroy the stars mark up is not marked not destroy, and then join () what you can

 

Note the star number is zero-based input but also to ensure the number u, v to ensure that u <v namely (to order) may appear otherwise, bordered border erase time is 1, 2, but when is 2 , 1 case

 

Code:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e4+5;
const int Hash=1000;
int n,m;
int p[maxn],f[maxn],ans[maxn];
int pos[maxn],maxx[maxn];
struct node
{
    int x,y;
}a[maxn];
struct edge
{
    int op,x,y;
}e[maxn];
map<int,int>vis;
char s[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        f[fx]=fy;
        if(maxx[fx]>maxx[fy])
        {
            maxx[fy]=maxx[fx];
            pos[fy]=pos[fx];
        }
        else if(maxx[fx]==maxx[fy] && pos[fx]<pos[fy])
        {
            pos[fy]=pos[fx];
        }
    }
}
int main()
{
    int flag=0;
    while(~scanf("%d",&n))
    {

        if(flag)printf("\n");
        else flag=1;

        for(int i=0;i<n;i++)
        {
            scanf("%d",&p[i]);
            maxx[i]=p[i];
            pos[i]=i;
            f[i]=i;
        }

        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            if(a[i].x>a[i].y)swap(a[i].x,a[i].y);
        }
        int q;
        scanf("%d",&q);
        vis.clear();
        for(int i=0;i<q;i++)
        {
            scanf("%s",s);
            if(s[0]=='q')
            {
                e[i].op=0;
                scanf("%d",&e[i].x);
            }
            else
            {
                e[i].op=1;
                scanf("%d%d",&e[i].x,&e[i].y);
                if(e[i].x>e[i].y)swap(e[i].x,e[i].y);
                vis[e[i].x*Hash+e[i].y]=1;
            }
        }
        for(int i=0;i<m;i++)
        {
            if(!vis[a[i].x*Hash+a[i].y])join(a[i].x,a[i].y);
        }
        int cnt=0;

        for(int i=q-1;i>=0;i--)
        {
            if(e[i].op==0)
            {
                int x=e[i].x;
                int fx=Find(x);
                if(maxx[fx]>p[x])ans[cnt++]=pos[fx];
                else ans[cnt++]=-1;
            }
            else
            {
                join(e[i].x,e[i].y);
            }
        }
        for(int i=cnt-1;i>=0;i--)printf("%d\n",ans[i]);

    }
    return 0;
}
View Code

  

 

Guess you like

Origin www.cnblogs.com/j666/p/11609892.html