并查集-HDU-5631-Rikka with Graph

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 525 Accepted Submission(s): 240

Problem Description
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)——The number of the testcases.

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

Then n+1 lines follow. Each line contains two numbers u,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

Source
BestCoder Round #73 (div.2)

题意:
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:
给出一张 n 个点 n+1 条边的无向图,你可以选择一些边(至少一条)删除。
现在勇太想知道有多少种方案使得删除之后图依然联通。
当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
第一行一个整数表示数据组数 T(T≤30)。
每组数据的第一行是一个整数 n(n≤100)。
接下来 n+1 行每行两个整数 u,v 表示图中的一条边。
输出描述
对每组数据输出一行一个整数表示答案。

题解:
由于一些个人原因,缺席了这场估摸着可以涨分的BC。。。
这道题其实就是枚举的删除2条边,因为总共有n+1条边,那么剩下的n-1条边如果能构成生成树,就能够删去这2条边中的任意一条或者这两条,需要注意的是判断单独一条边是否已经删除过了,标记一下即可。
在判断生成树方面,一般是BFS,由于最近在做并查集,所以我用了并查集。

//
// main.cpp
// HDU-5631-Rikka with Graph
//
// Created by 袁子涵 on 16/2/24.
// Copyright © 2016年 袁子涵. All rights reserved.
//
// 343ms 1616KB

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;
long long int t,n,u,v,out;
bool vis[105];
long long int father[105];
typedef struct edge
{
    long long int u,v;
}Edge;
Edge E[105];

void init()
{
    for (long long int i=0; i<=n; i++)
        father[i]=i;
}

long long int find(long long int num)
{
    long long int root,now=num,tmp;
    while (father[now]!=now)
        now=father[now];
    root=now;
    now=num;
    while (father[now]!=now) {
        tmp=father[now];
        father[now]=root;
        now=tmp;
    }
    return root;
}

bool check(long long int num1,long long int num2)
{
    init();
    long long int fx,fy,result=0;
    for (long long int i=1; i<=n+1; i++) {
        if (i==num1 || i==num2)
            continue;
        fx=find(E[i].u);
        fy=find(E[i].v);
        if (fx!=fy)
            father[fx]=father[fy];
    }
    for (long long int i=1; i<=n; i++)
        if (father[i]==i)
            result++;
    if (result==1)
        return 1;
    return 0;
}

int main(int argc, const char * argv[]) {
    cin >> t;
    while (t--) {
        cin >> n;
        out=0;
        memset(vis, 0, sizeof(vis));
        memset(E, 0, sizeof(E));
        for (long long int i=1; i<=n+1; i++)
            scanf("%lld %lld",&E[i].u,&E[i].v);
        for (long long int i=1; i<=n; i++) {
            for (long long int j=i+1; j<=n+1; j++) {
                if (check(i, j)) {
                    if (vis[i]==0) {
                        vis[i]=1;
                        out++;
                    }
                    if (vis[j]==0) {
                        vis[j]=1;
                        out++;
                    }
                    out++;
                }
            }
        }
        cout << out << endl;
    }
    return 0;
}

你可能感兴趣的:(Algorithm,ACM)