hdoj 5631 Rikka with Graph 【并查集】

来源:互联网 发布:word表格数据求和 编辑:程序博客网 时间:2024/05/16 05:01

Rikka with Graph

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


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

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

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
131 22 33 11 3
 

Sample Output
9
 

题意:给你n个点和n+1条边,要求去掉至少一条边使得图依旧连通。求方案数。


思路:暴力,并查集判连通。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF (2000000000+10)#define eps 1e-8#define MAXN (200000+10)#define MAXM (600000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;int father[110];int Find(int p){    int t, child = p;    while(p != father[p]) p = father[p];    while(child != p) {t = father[p]; father[child] = p; child = t;}    return p;}void Merge(int x, int y){    int fx = Find(x);    int fy = Find(y);    if(fx != fy)        father[fx] = fy;}int u[110], v[110];int n;bool use[110];bool judge(){    for(int i = 1; i <= n; i++) father[i] = i;    for(int i = 0; i <= n; i++)        if(use[i]) Merge(u[i], v[i]);    int cnt = 0; bool flag = true;    for(int i = 1; i <= n; i++)    {        if(father[i] == i) cnt++;        if(cnt > 1) {flag = false; break;}    }    return flag == true;}int main(){    int t; Ri(t);    W(t)    {        Ri(n);        for(int i = 0; i <= n; i++) Ri(u[i]), Ri(v[i]), use[i] = true;        int ans = 0;        for(int i = 0; i <= n; i++)        {            use[i] = false;            if(judge()) ans++;            for(int j = i+1; j <= n; j++)            {                use[j] = false;                if(judge()) ans++;                use[j] = true;            }            use[i] = true;        }        Pi(ans);    }    return 0;}


0 0
原创粉丝点击