ZOJ3261-Connections in Galaxy War- (+ reverse disjoint-set offline processing)

Meaning of the questions:

1. There are n planets, each planet having a number (1-n) and an energy value.

2. a start communicating certain planet.

3. After the war have a number of operations, query a planet can turn to for help or destroy the communication path between the two planets, it can not be connected. If communication can help each other, help target large energy requirements and maximum energy in communicating than their own planet, the planet's largest energy if there are more, then find a small number.

Problem solving:

1. disjoint-set communication with the planet, the large energy as the root node, if the energy the same as the root node put small numbers, help facilitate the query object.

2. disjoint-set operation is not disconnected, the final state as a start state backstepping. Save the start of the communication path after the war and the destruction of the communication path, ending the state of war as the starting state, communicates with the communication path is not destroyed.

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<math.h>
  6 #include<string>
  7 #include<map>
  8 #include<queue>
  9 #include<stack>
 10 #include<set>
 11 #define ll long long
 12 #define inf 0x3f3f3f3f
 13 using namespace std;
 14 
 15 int n,m,x,y,q;
 16 int par[10005 ]; /// father 
. 17 Map < int , BOOL > MP;
 18 is  int ANS [ 50086 ];
 . 19  struct Star
 20 is  {
 21 is      int ID; /// number 
22 is      int Val; /// energy value 
23 is }; Star S [ 10005 ];
 24  
25  struct Edge
 26 is  {
 27      int X;
 28      int Y;
 29 }; Edge E [ 20086 ];
 30 
 31 struct query
 32 {
 33     char s[10];
 34     int x,y;
 35 
 36 };query que[50086];
 37 
 38 
 39 
 40 int find(int a)
 41 {
 42     if( par[a]==a )
 43         return a;
 44     return par[a]=find(par[a]);
 45 }
 46 
 47 void unit(int a,int b)
48  {
 49      int AA = Find (A);
 50      int BB = Find (B);
 51 is      IF (! = AA BB)
 52 is      {
 53 is          IF (S [AA] .val == S [BB] .val) // / same energy value 
54 is          {
 55              IF (S [AA] .id <S [BB] .id) /// small number do root 
56 is                  PAR [BB] = AA;
 57 is              the else 
58                  PAR [AA] = BB;
 59          }
 60          the else  IF (S [AA] .val <S [BB] .val) /// large energy value as the root node
61 is              PAR [AA] = BB;
 62 is          the else 
63 is              PAR [BB] = AA;
 64      }
 65  }
 66  
67  int main ()
 68  {
 69      BOOL First = to true ;
 70      the while (Scanf ( " % D " !, & N-) = the EOF) /// number of planets 
71 is      {
 72          IF (first) /// first come becomes false, to be printed after each blank line 
73 is              first = to false ;
 74          the else the printf ( "\n");
 75         mp.clear();
 76         memset(ans,inf,sizeof(ans));
 77         for(int i=0;i<n;i++)///能量值
 78         {
 79             scanf("%d",&s[i].val);
 80             s[i].id=i;
 81             par[i]=i;
 82         }
 83         scanf("%d",&m);///连通边
 84         for(int I = 0 ; I <m; I ++ )
 85          {
 86              Scanf ( " % D% D " , & X, & Y);
 87              IF (X> Y) /// ensure X <Y
 88                  the swap (X, Y) ; 
89              E [I] .x = X;
 90              E [I] .y = Y; /// kept unconnected edge 
91 is              MP [X * 10000 + Y] = to false ; /// not destroyed 
92          }
 93          Scanf ( " % D " , & Q); ///查询
 94         for(int i=0;i<q;i++)
 95         {
 96             getchar();
 97             scanf("%s",que[i].s);
 98             if(que[i].s[0]=='q')
 99                 scanf("%d",&que[i].x);
100             else
101             {
102                 scanf("%d %d",&x,&y);
103                 IF (X> Y) /// ensure X <Y
 104                      the swap (X, Y); 
105                  que [I] .x = X;
 106                  que [I] .y = Y;
 107                  MP [X * 10000 + Y] = to true ; /// destroyed 
108              }
 109          }
 110          // offline processing 
111          for ( int I = 0 ; I <m; I ++) // the side not being destroyed final state as the starting state , backstepping 
112          {
 113              X = E [I] .x;
 114             = Y E [I] .y;
 115              IF (MP [X *! 10000 + Y])
 1 16                  Unit (X, Y);
 117          }
 1 18          /// backstepping, the corresponding answer stored 
119          for ( int I = Q - . 1 ; I> = 0 ; i-- )
 120          {
 121              IF (que [I] .s [ 0 ] == ' Q ' )
 122              {
 123                  X = Find (que [I] .x); /// planet communication planet maximum energy root 
124                  IF (S [X] .val>S [que [I] .x] .val)
 125                      ANS [I] = S [X] .id;
 126                  the else 
127                      ANS [I] = - . 1 ;
 128              }
 129              the else 
130.                  Unit (que [I] .x, que [I] .y);
 131 is          }
 132          /// sequentially output answer 
133          for ( int I = 0 ; I <Q; I ++ )
 134          {
 135              IF ! (ANS [I] = INF)
 136                  the printf ( " % D \ n- " , ANS [I]);
 137         }
138     }
139     return 0;
140 }

 

Guess you like

Origin www.cnblogs.com/shoulinniao/p/11206735.html