HDU 4496D-City2013通化邀請賽D題(並查集 需要壓縮路徑)
D-City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 83 Accepted Submission(s): 49
Problem Description
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.
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.
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
題目大意:給你n個頂點,m條邊。每次會摧毀一條邊,到最後把m條邊都摧毀完。問你每次摧毀的時候還有幾個集合。
解題思路:邊會逐漸摧毀,我們關心的是點上還連著多少邊。摧毀1條邊還有m-1條邊,摧毀2條邊還有m-2條邊。我們可以直接使用並查集處理,從後往前處理,依次加上第m條邊,第m-1條邊。這樣處理即可。不過需要在找father的時候壓縮路徑,不然會TLE詳見程式碼。
題目地址:D-City
AC程式碼:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=100005;
int a[maxn],b[maxn],father[maxn],res[maxn],n,cnt;
void init()
{
int i;
for(i=0;i<n;i++)
father[i]=i;
}
int find1(int x) //路徑壓縮的find1
{
if(father[x]!=x)
father[x]=find1(father[x]);
return father[x];
}
/*int find1(int x) //不壓縮路徑會TlE
{
int r=x;
while(father[r]!=r)
r=father[r];
return r;
}*/
void merge1(int p1,int p2)
{
int s1=find1(p1);
int s2=find1(p2);
if(s1!=s2) cnt--; //若有合併的集合就減少
father[s1]=s2;
}
int main()
{
int m,i;
while(~scanf("%d%d",&n,&m))
{
init();
cnt=n;
for(i=0;i<m;i++)
scanf("%d%d",&a[i],&b[i]);
res[m]=cnt;
for(i=m-1;i>=1;i--) //逆序加邊
{
merge1(a[i],b[i]);
res[i]=cnt;
}
for(i=1;i<=m;i++)
printf("%d\n",res[i]);
}
return 0;
}
相關文章
- HDU-3635 Dragon Balls 並查集路徑壓縮Go並查集路徑壓縮
- 並查集系列之「路徑壓縮( path compression ) 」並查集路徑壓縮
- 為什麼並查集路徑壓縮時不需要維護rank?並查集路徑壓縮
- 2012長春站D題||hdu4424 並查集並查集
- HDU5441 Travel (2015年長春網路賽,並查集)並查集
- 洛谷 P3366 【模板】最小生成樹(並查集+壓縮路徑(縮短到最高上一級的步驟))並查集
- 第三屆華中地區邀請賽網路賽題解
- 並查集的應用:hdu 1213並查集
- HDU 1213 How Many Tables(並查集)並查集
- 2013長沙網路賽H題Hypersphere (蛋疼的題目 神似邀請賽A題)
- NJUST 1746 Similar Number(南京邀請賽 J題)MILA
- HDU-3172 Virtual Friends 並查集+map並查集
- hdu5222 拓撲+並查集並查集
- hdu 1811 並查集+拓撲排序並查集排序
- Xcode外掛路徑、快取路徑、圖片壓縮工具路徑、程式碼片段路徑、symbolicatecrash路徑XCode快取Symbol
- 2013成都站F題||hdu4786 並查集 生成樹並查集
- zoj 3811||牡丹江網賽 c題 並查集並查集
- HDU-1272 小希的迷宮 並查集並查集
- HDU 1198Farm Irrigation(並查集)並查集
- hdu 1829 並查集(食物鏈的弱化版)並查集
- HDU 5200 Tree (離線並查集)並查集
- HDU 3234 Exclusive-OR 擴充套件並查集套件並查集
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- 並查集題目合集並查集
- (UVA - 208)Firetruck(路徑輸出問題,回溯+並查集/floyd演算法+dfs)並查集演算法
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- hdu4313 貪心並查集 || 樹形dp並查集
- HDU 1272小希的迷宮(簡單並查集)並查集
- HDU 3038 How Many Answers Are Wrong (帶權並查集)並查集
- HDU4592 Boring Game (2013 ACM-ICPC南京賽區全國邀請賽) 高斯消元GAMACM
- 2018天梯賽、藍橋杯、(CCPC省賽、邀請賽、ICPC邀請賽)校內選拔賽反思總結!
- HDU4514湫湫系列故事——設計風景線(並查集判環+最長直徑)並查集
- HDU-3461 Code Lock 並查集 + 二分求冪並查集
- 關於並查集問題並查集
- pigz 並行壓縮並行
- 並查集到帶權並查集並查集
- 【構造共軛函式+矩陣快速冪】HDU 4565 So Easy! (2013 長沙賽區邀請賽)函式矩陣
- 2014廣州網路賽1002||hdu5023 線段樹&&狀態壓縮