• ZOJ 3261 Connections in Galaxy War (并查集)


    Connections in Galaxy War

    Time Limit: 3 Seconds      Memory Limit: 32768 KB

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.

    In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes star A couldn't find such star for help.

    Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.

    Input

    There are no more than 20 cases. Process to the end of file.

    For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers ab (0 <= ab <= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.

    In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.

    "destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star b was available before the monsters' attack.

    "query a" - star a wanted to know which star it should turn to for help

    There is a blank line between consecutive cases.

    Output

    For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.

    Print a blank line between consecutive cases.

    Sample Input

    2
    10 20
    1
    0 1
    5
    query 0
    query 1
    destroy 0 1
    query 0
    query 1
    

    Sample Output

    1
    -1
    -1
    -1



    题意:有N星球编号0~N-1,每个星球有一个战斗力,在星球大战中,弱小的星球可以向其他比它更强的星球求助,当然这些星球要求直接或者间接和它相连,给出M组两个星球之间的联系,两个星球之间的通信可能会被怪兽损坏。之后给出Q组询问,query a表示编号为a的星球向其他更强的星球求助,找到与a直接或者间接相连的战斗力最大的星球,如果有多个,输出编号最小的那个,destroy a b表示a和b之间的通信被损坏。

    分析:并查集,把所有数据都保存下来,然后倒着处理。对于提出的问题:及时回答,保存答案。对于删去的树枝:做并查集的合并处理。

    如果按照顺序处理,先合并有联系的点,当后面有destory操作的时候,不好处理。所以就倒着处理,离线输入数据,从最后的状态开始,如果是query的时候就做查询操作,如果是destory就做合并操作。合并的时候比较当前节点的值和根节点的值的大小,更新最大值。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=10000+5;
    const int HASH=10000;
    int n,m,Q;
    int ans[50000+5];
    int p[MAXN];
    int val[MAXN];
    map<int,int> mat;
    struct EDGE
    {
        int u,v;
    }a[20000+5];
    struct NODE
    {
        char str[10];
        int u,v;
    }b[50000+5];
    
    
    void init()
    {
        for(int i=0;i<n;i++) p[i]=i;
        memset(ans,0,sizeof(ans));
        memset(val,0,sizeof(val));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        mat.clear();
    }
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    void Union(int u,int v)
    {
        int x=findfa(u);
        int y=findfa(v);
        if(x!=y)
        {
            if(val[x]>val[y]) p[y]=x;
            else if(val[x]<val[y]) p[x]=y;
            else
            {
                if(x<y) p[y]=x;
                else p[x]=y;
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        bool flag=false;
        while(scanf("%d",&n)!=EOF)
        {
            init();
            for(int i=0;i<n;i++) scanf("%d",&val[i]);
            scanf("%d",&m);
            for(int i=0;i<m;i++)
            {
                scanf("%d %d",&a[i].u,&a[i].v);
                if(a[i].u>a[i].v) swap(a[i].u,a[i].v);
            }
            scanf("%d",&Q);
            for(int i=0;i<Q;i++)
            {
                scanf("%s",&b[i].str);
                if(b[i].str[0]=='q') scanf("%d",&b[i].u);
                else
                {
                    scanf("%d %d",&b[i].u,&b[i].v);
                    if(b[i].u>b[i].v) swap(b[i].u,b[i].v);
                    mat[b[i].u*HASH+b[i].v]=1;
                }
            }
            for(int i=0;i<m;i++)
                if(mat[a[i].u*HASH+a[i].v]!=1)
                    Union(a[i].u,a[i].v);
    
            int cnt=0;
            for(int i=Q-1;i>=0;i--)
            {
                if(b[i].str[0]=='q')
                {
                    int x=findfa(b[i].u);
                    if(val[x]>val[b[i].u]) ans[cnt++]=x;
                    else ans[cnt++]=-1;
                }
                else Union(b[i].u,b[i].v);
            }
    
            if(flag) printf("
    ");
            else flag=true;
    
            for(int i=cnt-1;i>=0;i--)
                printf("%d
    ",ans[i]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    软件工程——理论、方法与实践 第三章
    软件工程——理论、方法与实践 第二章
    软件工程——理论、方法与实践 第一章
    使用@RunWith(SpringJUnit4ClassRunner.class)进行单元测试时 报错 和 java.lang.NoSuchMethodError的解决方法
    springMVC 校验时,CustomValidationMessages.properties中的错误提示信息的中文乱码 问题
    通过scrapy,从模拟登录开始爬取知乎的问答数据
    利用AJAX JAVA 通过Echarts实现豆瓣电影TOP250的数据可视化
    Scrapy爬取伯乐在线的所有文章
    搭建第一个scrapy项目的常见问题
    利用Ajax实现数据的同步传输,从mysql中提取数据,通过echarts可视化
  • 原文地址:https://www.cnblogs.com/clliff/p/4701258.html
Copyright © 2020-2023  润新知