首页 > 技术文章 > 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

wawcac-blog 2017-04-01 00:32 原文

  这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态,这就能套并查集了。中途的询问结果用栈存下来,输出的时候一个个出栈就好

  还有一个共同点,都是科幻风格的,都是星际战争,通信都靠隧道,不知道出题人有没有借鉴的意思

  假装这篇博客是我刚写完星球大战这题时写的(其实是一年零两个月二十多天以后,写了zoj那题以后写的 2019年01月25日16:35:27)

[JSOI2008]星球大战

题目描述

  很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系。

  某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直接或间接地连接。

  但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。

  现在,反抗军首领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每一次打击之后反抗军占据的星球的连通块的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则这两个星球在同一个连通块中)。

输入输出格式

输入格式:

  输入文件第一行包含两个整数,$N (1<=N<=2M)$和$M (1<=M<=200,000)$,分别表示星球的数目和以太隧道的数目。星球用$ 0 ~ N−1 $的整数编号。

  接下来的 $M$ 行,每行包括两个整数 $X$, $Y$,其中($0<=X<>Y$ 表示星球 $x$ 和星球 $y$ 之间有 “以太” 隧道,可以直接通讯。

  接下来的一行为一个整数 $k$ ,表示将遭受攻击的星球的数目。

  接下来的 $k$ 行,每行有一个整数,按照顺序列出了帝国军的攻击目标。这 $k$ 个数互不相同,且都在 $0$ 到$n−1$的范围内。

输出格式: 

  第一行是开始时星球的连通块个数。接下来的 $k$ 行,每行一个整数,表示经过该次打击后现存星球的连通块个数。

 

输入输出样例

输入样例#1: 复制
8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
输出样例#1: 复制
1
1
1
2
3
3

说明

[JSOI2008]

源代码

 1 #include<stdio.h>//当年的一些中文注释的编码坏掉了
 2 
 3 int n,m,k;//n??μ?mì?±?k???Y?ù????
 4 
 5 
 6 int ruin[400000]={0};
 7 bool ru[400010]={0};//ê?·?±??Y?ù
 8 int num=0;
 9 int ans[400010]={0},top=0;
10 
11 
12 struct Edge{
13     int next,to;
14 }e[400010];
15 int head[400010]={0},cnt=1;//40íò??μ?20íòì??T?ò±?
16 void add(int u,int v)
17 {
18     e[cnt].to=v;
19     e[cnt].next=head[u];
20     head[u]=cnt++;
21 }
22 
23 
24 int fa[400010];//?ó0?e£?2??ó1?e
25 int find(int x)
26 {
27     return fa[x]==x?x:(fa[x]=find(fa[x]));
28 }
29 inline bool uni(int x,int y)
30 {
31     x=find(x),y=find(y);
32     if(x==y) return false;
33     fa[x]=y;
34     return true;
35 }
36 
37 
38 int main()
39 {
40     //freopen("test.txt","r",stdin);
41     for(int i=0;i<400010;i++)
42         fa[i]=i;
43 
44     scanf("%d%d",&n,&m);
45     for(int i=0,u,v;i<m;i++)
46     {
47         scanf("%d%d",&u,&v);
48         //printf("%d %d %d\n",cnt,u,v);
49         add(u,v);
50         //printf("%d %d %d\n\n",cnt,v,u);
51         add(v,u);
52     }
53     scanf("%d",&k);
54     for(int i=0;i<k;i++)
55         scanf("%d",ruin+i),ru[ruin[i]]=true;
56  
57     num=0;
58     for(int i=0;i<n;i++)
59     {
60         if(!ru[i])
61         {
62             for(int j=head[i];j;j=e[j].next)
63             {
64                 if(!ru[e[j].to])
65                     uni(i,e[j].to);
66             }
67         }
68     }//í3??è?2??Y?ùoóμ?á?í?·?á?num
69     for(int i=0;i<n;i++)
70         if(find(i)==i&&!ru[i]) num++;
71     ans[top++]=num;
72     //printf("^^^^\n");
73     for(int i=k-1;i>=0;i--)
74     {
75         ru[ruin[i]]=false;
76         int u=ruin[i];
77         int m=0/*áùê±í3??áaí?·?á??eê§*/;
78         for(int j=head[u];j;j=e[j].next)
79         {
80             if(!ru[e[j].to])
81                 if(uni(u,e[j].to))
82                 {
83                     m++;
84                     //printf("%d %d ",u,e[j].to);
85                     //printf("%d\n",j);
86                 }
87         }
88         num-=m-1;
89         ans[top++]=num;
90     }
91     top=k;
92     while(top>=0)
93     {
94         printf("%d\n",ans[top]);
95         top--;
96     }
97     
98     return 0;
99 }
星球大战

 

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

Author: MO, Luyi
Source: ZOJ Monthly, November 2009

源代码

  1 #include<set>
  2 #include<stdio.h>
  3 #include<algorithm>
  4 
  5 int n,m=-1,q;
  6 
  7 struct Opt{//o==d:a b; o==q: a;
  8     char o;
  9     int a;
 10     int b;
 11     int id;
 12     bool operator < (const Opt & x)const{
 13         return id>x.id;
 14     }
 15 }opt[50004];
 16 
 17 struct Edge{
 18     int s;
 19     int t;
 20     bool operator < (const Edge & x)const{
 21         if(s==x.s) return t<x.t;
 22         return s<x.s;
 23     }
 24 };
 25 std::set<Edge> e;
 26 std::set<Edge>::iterator it;
 27 int ans[50004];
 28 
 29 int p[10010],mx[10010],cnt[10010];
 30 int fa[10010];
 31 int find(int x)
 32 {
 33     if(fa[x]==x) return x;
 34     fa[x]=find(fa[x]);
 35     cnt[x]=cnt[fa[x]];
 36     mx[x]=mx[fa[x]];
 37     return fa[x];
 38 }
 39 void uni(int x,int y)
 40 {
 41     x=find(x);
 42     y=find(y);
 43     fa[x]=y;
 44     cnt[y]+=cnt[x];
 45     cnt[x]=cnt[y];
 46     if(p[mx[x]]>p[mx[y]])
 47     {
 48         mx[y]=mx[x];
 49     }
 50     else if(p[mx[x]]==p[mx[y]])
 51     {
 52         mx[y]=mx[x]<mx[y]?mx[x]:mx[y];
 53     }
 54     mx[x]=mx[y];
 55 }
 56 
 57 int main()
 58 {
 59     freopen("test.in","r",stdin);
 60     while(~scanf("%d",&n))
 61     {
 62         if(m>=0) printf("\n");
 63         for(int i=0;i<n;i++) mx[i]=fa[i]=i,cnt[i]=1;
 64         for(int i=0;i<n;i++) scanf("%d",p+i);
 65         scanf("%d",&m);
 66         for(int i=1,a,b;i<=m;i++)
 67         {
 68             scanf("%d%d",&a,&b);
 69             e.insert({a,b});//查询时e.find({a,b})!=e.end()||e.find({b,a})!=e.end()
 70         }
 71         scanf("%d",&q);
 72         for(int i=1;i<=q;i++)
 73         {
 74             int a,b=0;
 75             char str[10];
 76             scanf("%s%d",str,&a);
 77             if(str[0]=='d')
 78             {
 79                 scanf("%d",&b);
 80                 it=e.find({a,b});
 81                 if(it==e.end()) it=e.find({b,a});
 82                 e.erase(it);
 83             }
 84             opt[i]={str[0],a,b,i};
 85         }
 86         std::sort(opt+1,opt+1+q);
 87         for(it=e.begin();it!=e.end();it++)
 88             uni(it->s,it->t);
 89         int num=0;
 90         for(int i=1;i<=q;i++)
 91         {
 92             if(opt[i].o=='d')
 93             {
 94                 uni(opt[i].a,opt[i].b);
 95             }
 96             else//q
 97             {
 98                 find(opt[i].a);
 99                 if(p[mx[opt[i].a]]<=p[opt[i].a]) ans[num++]=-1;
100                 else ans[num++]=mx[opt[i].a];
101             }
102         }
103         
104         while(num>0) printf("%d\n",ans[--num]);
105                 
106         e.clear();
107     }
108     return 0;
109 }
Connections in Galaxy War

 

推荐阅读