bzoj1015 [JSOI2008] Star Wars Starwar

Source: Internet
Author: User

1015: [JSOI2008] Star Wars Starwar time limit:3 Sec Memory limit:162 MB
submit:5134 solved:2328
[Submit] [Status] [Discuss] Description

Long ago, in a distant galaxy, a dark empire was leaning against its super-weapons rulers throughout the galaxy. One day, with an accidental
Opportunity, a rebel army destroyed the empire's super weapons and attacked almost all the planets in the galaxy. These planets are directly connected to each other through special etheric tunnels.
Connected to or indirectly. 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 have plans
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 army's first
I'll give you a mission: provide an Ethernet tunnel connection between the original two planets and the Order of the empire-hit planets to find out every
The number of connected fast planets that the rebels occupy after a strike. (If two planets can be connected directly or indirectly through the existing etheric channels,
The two planets are in the same connected block).

Input

The first line of the input file contains two integers, n (1 < = n < = 2M) and m (1 < = m < = 200,000), respectively, representing the planet's
Number and number of Ethernet tunnels. The planet is numbered with an integer of 0 ~ N-1. The next M-line, each line consists of two integers x, Y, where (0 < = X <>
Y means that there is an "ether" tunnel between Planet X and Planet Y that can communicate directly. The next behavior is an integer k, which indicates that the planet that will be attacked
Number. The next K-line, each line has an integer, in order to list the attack target of the imperial army. The number of k is different, and all the 0 to n-1
In the surrounding.

Output

The first line of the output file is the number of connected blocks at the beginning of the planet. The next n rows, one integer per line, indicate the existing planet after the strike
Number of connected blocks.

Sample Input8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
9 |
7 6
3 6
5
1
6
3
5
7Sample Output1
1
1
2
3
3Analysis: Previously learned the method of using DFS to find connected blocks, but this dynamic solution to the problem of connected blocks with DFS absolute tle bar ... And then found that this is an tarjan graph, can be used to find the algorithm? It seems also tle, what to do ...The reason for these algorithms time-out is to calculate a lot of the calculated amount of each time, if you want to avoid the calculation of the dynamic calculation, the first gross position of the number of connected blocks to find out, and then constantly delete points, reduce the number of connected blocks, the key is how to award connectivity, it is clear that, and check set can be, but also How to delete the edge? so you can think of reverse, since it is constantly deleted points, then we continue to add, first find out the deleted points are deleted after the number of connected blocks, and then constantly added points, each add this point, then it as a separate point, the number of connected blocks plus 1, and then enumerate and the point connected to the edge connected to the point, If two points are not in a set, and they are connected by an edge, it must be connected, if connected, the number of connected blocks will be reduced by 1, reverse order records the answer, the final sequence output answer can be.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =400010;intN, M,fa[maxn],cur[maxn], Head[maxn],flag[maxn],ans, K,tot, TO[MAXN], NEXTT[MAXN],CAN[MAXN];voidAddintAintb) {Tot++; To[tot]=b; Nextt[tot]=Head[a]; Head[a]=tot;}intFindintx) {    returnx = = Fa[x]? X:FA[X] =find (Fa[x]);}intMain () {memset (flag,1,sizeof(flag)); scanf ("%d%d", &n, &m);  for(inti =1; I <= m; i++)    {        intx, y; scanf ("%d%d", &x, &y);        Add (x, y);    Add (y, x); } scanf ("%d", &k);  for(inti =1; I <= K; i++) {scanf ("%d", &Can[i]); Flag[can[i]]=0; }     for(inti =0; I <= N; i++) Fa[i]=i; Ans= N-k;//use each point as a separate point first     for(intU =1; U <= N; u++)//then the number of connected blocks after deletion of the requested point is calculated by the relationship of the edge.        if(Flag[u]) { for(inti = head[u];i; i =Nextt[i])if(Flag[to[i]]) {intx = Find (u), y =find (To[i]); if(X! =y) {fa[y]=x; Ans--; }}} Cur[k]=ans; intu;  for(intt = k; T >=1; t--) {Flag[u= Can[t]] =1;//Keep adding pointsans++;  for(inti = Head[u]; I i =Nextt[i]) {            if(Flag[to[i]]) {intx = Find (u), y =find (To[i]); if(X! =y) {fa[y]=x; Ans--; } }} cur[t-1] =ans; }      for(inti =0; I <= K; i++)//Finally, along the outputprintf"%d\n", Cur[i]); return 0;}

bzoj1015 [JSOI2008] Star Wars Starwar

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.