luoguP1197 [JSOI2008] Star Wars X

Source: Internet
Author: User

P1197 [JSOI2008] Star Wars title description

Long ago, in a distant galaxy, a dark empire was leaning against its super-weapons rulers throughout the galaxy. One day, with an occasional chance, a rebel army destroyed the empire's super weapons and attacked almost all the planets in the galaxy. These planets are connected to each other directly or indirectly through special etheric tunnels.

But it was not long, and soon the Empire re-created his super weapon. With the power of this super weapon, the Empire began to plan to destroy the rebel-occupied planet. As the planet continues to be destroyed, communication channels between the two planets are beginning to be unreliable. Now, the rebel leader gives you a mission: to give the connection of the etheric tunnels between the original two planets and the order of the planets in the Empire's attack, to find out the number of connected fast planets that the rebels occupy after each strike as quickly as possible. (If two planets can be connected directly or indirectly through existing etheric channels, the two planets are in the same connected block).

Input/output format

Input format:

The first line of the input file contains two integers, n (1 <= n <= 2M) and M (1 <= m <= 200,000), respectively, representing the number of planets and the number of etheric tunnels. Planets are numbered with 0~n-1 integers.

The next M-line, each line consists of two integers x, y, where (0<=x<>y<n) indicates an ether tunnel between planet X and Planet Y. Note that all the Ethernet tunnels are bidirectional.

The next line is an integer k, representing the number of planets the empire plans to hit.

The next K-line is an integer X for each line, satisfying 0<=x<n, which represents the planet number that the Empire plans to strike. The empire always destroys the planets in the order in which they are entered.

Output format:

The first line of the output file is the number of connected blocks at the beginning of the planet.

The next K-line, an integer per line, indicates the number of connected blocks of the existing planet after the strike.

Input and Output Sample input example # #:
8 130 11 66 55 00 61 22 33 44 57 17 27 63 6516357
Sample # # of output:
111233
Description

[JSOI2008]

Ideas:

The inverse use of Unicom block + and check set

At first assume that all the planets are destroyed and then processed offline (listen to the high-end look!)

Pit point:

Be sure to remember to initialize the value of the head array!!! Because we're going to use element number No. 0.

On the code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intM = 4e5 +5;intHeads[m];intn,m,k;intnow=0, x, y;intDad[m],hit[m],answer[m];BOOLV[m];structa{intNext,to;} H[M];voidAddintPreintTo ) {h[now].to=to ; H[now].next=Heads[pre]; Heads[pre]=now++;}intGetdad (intMs) {    returnDAD[MS] = = Ms? ms:dad[ms]=Getdad (Dad[ms]);}voidUnions (intDadsintsons) {Dad[getdad (DADS)]=Getdad (sons);}intMain () {scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) dad[i]=i,heads[i]=-1;  for(intI=0; i<m;i++) {scanf ("%d%d",&x,&y);    Add (x, y), add (y,x); } scanf ("%d",&k);  for(intI=0; i<k;i++) {scanf ("%d",&Hit[i]); V[hit[i]]=true; ///because it's in reverse order, the current planet has been hit.    }     for(intI=0; i<n;i++)    {        if(!V[i]) {             for(intj=heads[i];j!=-1; j=H[j].next) {                intVc=h[j].to; if(!V[VC]) unions (I,VC); }        }    }     for(intI=0; i<n;i++)        if(!v[i] && getdad (i) = =i) answer[k]++;///find the remaining connected blocks of the last picture     for(inti=k-1; i>=0; i--)///because the stored time is the positive sequence of storage, the calculation should be reversed when the operation, so reverse order    {        intx=0; ///x records a request that is connected to a planet that is currently being hit and not being hit .v[hit[i]]=false;///clears the tag, because it is in reverse order.         for(intj=heads[hit[i]];j!=-1; j=H[j].next) {            if(!V[h[j].to]) {                intA=Getdad (Hit[i]); intb=Getdad (h[j].to); if(a!=b) {Unions (a B); X++; }}} Answer[i]=answer[i+1]-x+1; }     for(intI=0; i<=k;i++) printf ("%d\n", Answer[i]); return 0;}
View Code

luoguP1197 [JSOI2008] Star Wars X

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.