九度oj 题目1325:Battle Over Cities

http://ac.jobdu.com/problem.php?id=1325  并查集

View Code
 1 #include<iostream>
2 using namespace std;
3 struct node
4 {
5 int x,y;
6 };
7 node edge[1000008];//edge
8 int father[1008];
9 int n,m,k;
10 void initSet()
11 {
12 for(int i=0;i<=n;i++) father[i]=i;
13 }
14 int find(int x)
15 {
16 int i=x,temp;
17 while(i!=father[i]) i=father[i];
18 while(x!=i)
19 {
20 temp=father[x];
21 father[x]=i;
22 x=temp;
23 }
24 return i;
25 }
26 int main()
27 {
28 cin>>n>>m>>k;//only one case
29 int i;
30 for(i=0;i<m;i++) cin>>edge[i].x>>edge[i].y;
31 int count,temp,fatherx,fathery;
32 while(k--)
33 {
34 count=0;
35 initSet();
36 cin>>temp;
37 for(i=0;i<m;i++)
38 {
39 fatherx=find(edge[i].x);
40 fathery=find(edge[i].y);
41 if(fatherx!=fathery && edge[i].x!=temp && edge[i].y!=temp)//生成树中排除temp点
42 {
43 father[fatherx]=fathery;
44 count++;//记录出去temp这个点后生成树的边数
45 }
46 }
47 //出去一个点后,的最小生成树如果连通,边数该为(n-1)-1
48 if(n==1) cout<<"0"<<endl;
49 else cout<<n-2-count<<endl;
50 }
51 //system("pause");
52 return 0;
53 }


 

转载于:https://www.cnblogs.com/keepmoving89/archive/2012/03/30/2425871.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值