CSUOJ 1601 War (offline query and query the number of connected blocks)

Source: Internet
Author: User

CSUOJ 1601 War (offline query and query the number of connected blocks)

1601: WarTime Limit: 1 Sec Memory Limit: 128 MB
Submit: 130 Solved: 38
[Submit] [Status] [Web Board] Description

AME decided to destroy CH's country. in CH 'country, There are N ages, which are numbered from 1 to N. we say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. to defend the country from the attack of AME, CH has decided to build some roads between some versions. let us say that two versions ages belong to the same garrison area if they are connected.
Now AME has already worked out the overall plan including which road and in which order wocould be attacked and destroyed. CH wants to know the number of garrison areas in his country after each of AME's attack.

Input

The first line contains two integers N and M-the number of ages and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000 ). each of the next M lines contains two different integers u, v (1 <= u, v <= N)-which means there is a road between u and v. the next line contains an integer Q which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M ). the last line contains a series of numbers each of which denoting a road as its order of appearance-different integers separated by spaces.

Output

Output Q integers-the number of garrison areas in CH's country after each of AME's attack. Each pair of numbers are separated by a single space.

Sample Input
3 11 2114 41 22 31 33 432 4 3
Sample Output
31 2 3
 

Question link: http://acm.csu.edu.cn/OnlineJudge/problem.php? Id = 1601


N vertices, m edges (side 1, side 2... edge m), q edge to be destroyed, calculate the number of connected blocks in the graph after each edge is destroyed in order

 

Question Analysis: offline query and set, reverse edge adding, calculate the number of connected blocks after all the q-entries are destroyed, and then add an edge, if two points are not in the same connected block, they are merged and the number of connected blocks is reduced by 1.

 

 

#include 
 
  #include 
  
   int const MAX = 1e5 + 5;int fa[MAX], n, m;int x[MAX], y[MAX], r[MAX];int ans, res[MAX];bool vis[MAX];void UF_set(){for(int i = 0; i <= n; i++)fa[i] = i;}int Find(int x){return x == fa[x] ? x : fa[x] = Find(fa[x]);}void Union(int a, int b){int r1 = Find(a);int r2 = Find(b);if(r1 != r2){fa[r2] = r1;ans --;}}int main(){while(scanf("%d %d", &n, &m) != EOF){UF_set();ans = n;memset(vis, false, sizeof(vis));for(int i = 1; i <= m; i++)scanf("%d %d", &x[i], &y[i]);int q;scanf("%d", &q);for(int i = 1; i <= q; i++){scanf("%d", &r[i]);vis[r[i]] = true;}for(int i = 1; i <= m; i++)if(!vis[i])Union(x[i], y[i]);for(int i = q; i >= 1; i--){res[i] = ans;Union(x[r[i]], y[r[i]]);}for(int i = 1; i < q; i++)printf("%d ", res[i]);printf("%d\n", res[q]);}}
  
 


 

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.