hdu 4496 D-City

这题是并查集的应用 .

如果看出来怎么解答确实很快.套模板就可以了.

可惜自己太菜,没想出来.

比赛的时候想到要反向,不过事实证明还是思路错了.

做的题目太少的缘故,多做点题目,多见见世面.

题目的意思大概是每次删除一些边,然后问你有多少块.明显的并查集.

可以通过反向建图,就是对所有的状态取反,所以要从最后一条边开始建图.

感谢 http://blog.csdn.net/acmmmm/article/details/10820117 九野的博客  点明了想法.

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<math.h>
 4 #define N 10001
 5 #define M 100050
 6 using namespace std;
 7 int father[N];
 8 int find(int x){
 9     if(x!=father[x])
10         father[x]=find(father[x]);
11     return father[x];
12 }
13 int ans[M],s[M],t[M];
14 int main(){
15     int n,m,i;
16     while(~scanf("%d%d",&n,&m))
17     {
18         for(i=0;i<=n;i++) father[i]=i;
19         for(i=1;i<=m;i++)
20             scanf("%d%d",&s[i],&t[i]);
21         ans[m]=n;
22         for(i=m;i>1;i--)
23         {
24             int a=find(s[i]),b=find(t[i]);
25             if(find(a)!=find(b))
26             {
27                 father[a]=b;
28                 ans[i-1]=ans[i]-1;
29             }
30             else ans[i-1]=ans[i];
31         }
32         for(i=1;i<=m;i++)
33             printf("%d\n",ans[i]);
34     }
35     return 0;
36 }

时间: 2024-08-02 03:45:02

hdu 4496 D-City的相关文章

HDU 4496 D-City (并查集)

题意:给你n个点m条边,问删除前i条边后有多少个连通分块. 思路:从后往前操作,从后往前添加i条边等于添加完m条边后删掉前m-i条边,可知刚开始没有边,所以sum[m]=n; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010

HDU 4496 D-City(并查集,逆思维)

题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... #include<stdio.h> #include<string.h> int fx,fy,r,bin[10010]; int x[100010],y[100010],n,m,i,count,ans[100010],j; int find(int x) { //改成这样就不会超时了么好

hdu 4496 D-City (逆向思维的并查集)

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1388    Accepted Submission(s): 520 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-c

hdu 4496 D-City 并查集

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2317    Accepted Submission(s): 814 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-

HDU 4496 D-City(逆向并查集)

http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后往前处理变成加边,用并查集维护连通块个数.其实这题和BZOJ 1015差不多. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 int n, m,

HDU 4849-Wow! Such City!(最短路)

Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 824    Accepted Submission(s): 310 Problem Description Doge, tired of being a popular image on internet, is considering moving

hdu 4496

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

hdu 4496 D-City(并查集)

题意: 思路: #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #include<queue> #include<algorithm> using namespace std; const int MAXN=100000+5; int p[MAXN],u[MAXN],v[MAXN],vis[MAX

HDU 4496 D-City —— (并查集的应用)

给出n个点和m条边,一条一条地删除边,问每次删除以后有多少个联通块. 分析:其实就是并查集的应用,只是前一阵子一直做图论思路一直囿于tarjan了..方法就是,记录每一条边,然后从最后一条边开始不断的加边,如果用并查集来判断联通块有没有减少即可. 代码如下: 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <set> 5 using namespace