BNUOJ 33895 D-City

D-City

Time Limit: 1000ms

Memory Limit: 65535KB

This problem will be judged on HDU. Original ID: 4496
64-bit integer IO format: %I64d      Java class name: Main

Luxer is a really bad guy. He destroys everything he met. 
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. 
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.

Input

First line of the input contains two integers N and M. 
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. 
Constraints: 
0 < N <= 10000 
0 < M <= 100000 
0 <= u, v < N.

Output

Output M lines, the ith line is the answer after deleting the first i edges in the input.

Sample Input

5 10
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4

Sample Output

1
1
1
2
2
2
2
3
4
5

Hint

The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there‘s only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.

Source

2013 ACM-ICPC吉林通化全国邀请赛——题目重现

解题:并查集,逆向求解。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxn = 10010;
17 int uf[maxn],n,m;
18 int ans[maxn*10];
19 int a[maxn*10],b[maxn*10];
20 int Find(int x){
21     if(x != uf[x])
22         uf[x] = Find(uf[x]);
23     return uf[x];
24 }
25 int main(){
26     int i,j,k;
27     while(~scanf("%d %d",&n,&m)){
28         for(i = 0; i <= n; i++)
29             uf[i] = i;
30         for(i = 1; i <= m; i++){
31             scanf("%d %d",a+i,b+i);
32         }
33         ans[m] = n;
34         for(i = m; i; i--){
35             int tx= Find(a[i]);
36             int ty = Find(b[i]);
37             uf[tx] = ty;
38             if(tx != ty){
39                 ans[i-1] = ans[i]-1;
40             }else ans[i-1] = ans[i];
41         }
42         for(i = 1; i <= m; i++){
43             printf("%d\n",ans[i]);
44         }
45     }
46     return 0;
47 }

BNUOJ 33895 D-City,布布扣,bubuko.com

时间: 2024-07-31 06:01:51

BNUOJ 33895 D-City的相关文章

BNUOJ 2528 Mayor&#39;s posters

Mayor's posters Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1058764-bit integer IO format: %lld      Java class name: Main The citizens of Bytetown, AB, could not stand that the candidates in the mayoral

BNUOJ 19792 Airport Express

Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1137464-bit integer IO format: %lld      Java class name: Main In a small city called Iokh, a train service, Airport-Express, takes residents t

City Upgrades

City Upgrades Time limit: 1000 msMemory limit: 128 MB There are N cities placed in a line. For each city ii you know its coodinate x??. You can upgrade exactly K of these cities. Your goal is to choose what cities to upgrade in a way the minimizes th

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

UIPicker city.plist(实例)

前言: UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIControl 那样绑定事件处理方法, UIPickerView 的事件处理由其委托对象完成. 正文: UIPickerView 控件常用的属性和方法如下:   numberOfComponents: 获取UIPickerView指定列中

City Game(动态规划)

City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5984    Accepted Submission(s): 2569 Problem Description Bob is a strategy game programming specialist. In his new city building game t

bzoj:1687;poj 2434:[Usaco2005 Open]Navigating the City 城市交通

Description A dip in the milk market has forced the cows to move to the city. The only employment available is in the venerable field of taxi-driving. Help the cows learn their way around the city. Given a city map with E (1 <= E <= 40) east/west st

HDU 1505 City Game (hdu1506 dp二维加强版)

F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1505 Appoint description: Description Bob is a strategy game programming specialist. In his new city building game the gaming enviro