天天看点

HDU 5631 Rikka with Graph

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

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

Output

For each testcase, print a single number.

Sample Input

1

3

1 2

2 3

3 1

1 3

Sample Output

9

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 105;
const int low(int x){ return x&-x; }
int T, n, m, fa[maxn], ans;

struct point
{
    int x, y;
}a[maxn];

int get(int x)
{
    return x == fa[x] ? x : fa[x] = get(fa[x]);
}

bool merge(int x, int y)
{
    int fx = get(x), fy = get(y);
    if (fx == fy) return false;
    fa[fx] = fy;    return true;
}

int main()
{
    cin >> T;
    while (T--)
    {
        scanf("%d", &n);
        ans = 0;
        for (int i = 0; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y);
        for (int i = 0; i <= n; i++)
        {
            int cnt = n - 1;
            for (int j = 1; j <= n; j++) fa[j] = j;
            for (int j = 0; j <= n; j++)
                if (i != j&&merge(a[j].x, a[j].y)) cnt--;
            if (!cnt) ans++;
        }
        for (int i = 0; i <= n; i++)
        {
            for (int k = i + 1; k <= n; k++)
            {
                int cnt = n - 1;
                for (int j = 1; j <= n; j++) fa[j] = j;
                for (int j = 0; j <= n; j++)
                if (i != j&&k != j&&merge(a[j].x, a[j].y)) cnt--;
                if (!cnt) ans++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}