hdu 5631 Rikka with Graph 【并查集】

Time limit

1000 ms

Memory limit

65536 kB

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: 

Yuta has a non-direct graph with n vertices and n+1 edges. Rikka can choose some of the edges (at least one) and delete them from the graph. 

Yuta wants to know the number of the ways to choose the edges in order to make the remaining graph connected. 

It is too difficult for Rikka. Can you help her?

Input

The first line contains a number T(T≤30)T(T≤30)——The number of the testcases. 

For each testcase, the first line contains a number n(n≤100)n(n≤100). 

Then n+1 lines follow. Each line contains two numbers u,vu,v , which means there is an edge between u and v.

Output

For each testcase, print a single number.

Sample Input

1
3
1 2
2 3
3 1
1 3

Sample Output

9

题意:给定n个点和n+1条边,要求删除至少一条边后该图还得连通,求有多少种方案数。

思路:我们知道,n个点至少有n-1条边才能连通在一起,现在很明显题意给我们n+1条边,所有我们在删除边的时候有两种删除方案,一种是只删除一条边,另一种是删除两条边,累加两种方案便是答案!

#include<cstdio>
#include<cstring>
const int maxn = 203;
int pre[maxn];
struct node{
   int x,y;
}p[maxn];
int find(int x)  //递归找老大或者while循环都可
{
    return x==pre[x]?x:find(pre[x]);
}
void join(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx!=fy)
        pre[fx] = fy;
}
void init(int n)
{
    for(int i=1;i<=n;i++)
        pre[i]=i;  //初始化的时候将每个集合的老大都假设为本身
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,ans,ans1=0;  //ans1记录答案,用ans的值判断根节点是否唯一
        scanf("%d",&n);
        //init(n);   这个题在这一步不能这样初始化,不然会筛选不出来,这一步删除
        for(int i=1;i<=n+1;i++)
            scanf("%d %d",&p[i].x,&p[i].y);
        for(int i=1;i<=n+1;i++)  //这是考虑删除一条边的情况
        {   
            init(n);  //这个部分每次循环都要更新一下,不然pre[]数组会保留上一层循环的值
            for(int j=1;j<=n+1;j++)
            {
                if(j!=i)
                {
                    join(p[j].x,p[j].y);
                }
            }
            ans=0;
            for(int o=1;o<=n;o++)
            {
                if(pre[o]==o)
                   ans++;
            }
            if(ans==1)
                ans1++;
        }
        for(int i=1;i<=n+1;i++)  //考虑删除两条边的情况
        {
            for(int j=i+1;j<=n+1;j++) //这条边不能和上一条边重复
            {   
                init(n);    //这个部分每次循环都要更新一下,不然pre[]数组会保留上一层循环的值
                for(int k=1;k<=n+1;k++)
                {
                    if(k!=i && k!=j)
                    {
                        join(p[k].x,p[k].y);
                    }
                }
                ans=0;
                for(int o=1;o<=n;o++)
                {
                    if(pre[o]==o)
                        ans++;
                }
                if(ans==1)
                    ans1++;
            }
        }
        printf("%d\n",ans1);  //ans1就是最后的答案
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值