图论(生成树):HDU 5631Rikka with Graph

Rikka with Graph

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

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)

Recommend

hujie   |   We have carefully selected several similar problems for you:  5634 5633 5632 5630 5629

  题意:给出一张 n 个点 n+1 条边的无向图,你可以选择一些边(至少一条)删除。有多少种方案使得删除之后图依然联通。

  这是best coder上面比赛的题目,可我比赛时数组开小了!!!!!

  内心哔了狗有木有!!!!!!!!!!!!!!!!!!!!!

  这里我用了组合数学,0ms过(其实暴力N³也可以过)~~~

  第一名哦!!!

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 using namespace std;
  5 const int maxn=110;
  6 int edge[maxn][3];
  7 int fa[maxn],cnt=1,fir[maxn],nxt[maxn<<1],to[maxn<<1];
  8 int dep[maxn],pre[maxn],ret[maxn],ID[maxn<<1];
  9 int find(int x)
 10 {
 11     return fa[x]==x?x:fa[x]=find(fa[x]);
 12 }
 13
 14 void addedge(int a,int b,int id)
 15 {
 16     nxt[++cnt]=fir[a];ID[cnt]=id;
 17     to[cnt]=b;fir[a]=cnt;
 18 }
 19
 20 void DFS(int node)
 21 {
 22     for(int i=fir[node];i;i=nxt[i])
 23     {
 24         if(dep[to[i]])continue;
 25         dep[to[i]]=dep[node]+1;
 26         pre[to[i]]=i;
 27         DFS(to[i]);
 28     }
 29 }
 30 int Search(int x,int y,int d)
 31 {
 32     while(x!=y){
 33         if(dep[x]<dep[y])
 34             swap(x,y);
 35         ret[ID[pre[x]]]+=d;
 36         x=to[pre[x]^1];
 37     }
 38 }
 39 void Init()
 40 {
 41     memset(fir,0,sizeof(fir));
 42     memset(ret,0,sizeof(ret));
 43     cnt=1;
 44 }
 45
 46 int main()
 47 {
 48     freopen("data.in","r",stdin);
 49     freopen("wrong.out","w",stdout);
 50     int T,n;
 51     scanf("%d",&T);
 52     while(T--)
 53     {
 54         Init();
 55         scanf("%d",&n);
 56         for(int i=1;i<=n+1;i++){
 57             fa[i]=i;
 58         }
 59
 60         for(int i=1;i<=n+1;i++)
 61             scanf("%d%d",&edge[i][0],&edge[i][1]);
 62         for(int i=1;i<=n+1;i++){
 63             int a=find(edge[i][0]),b=find(edge[i][1]);
 64             if(a==b)
 65                 edge[i][2]=1;
 66             else{
 67                 fa[a]=b;
 68                 addedge(edge[i][0],edge[i][1],i);
 69                 addedge(edge[i][1],edge[i][0],i);
 70             }
 71         }
 72         int flag=0;
 73         for(int i=2;i<=n;i++)
 74         if(find(i)!=find(i-1)){
 75             printf("0\n");
 76             flag=1;
 77             break;
 78         }
 79         if(flag==1)continue;
 80         dep[1]=1;
 81         DFS(1);
 82         int ans=0,a=0,b=0,c=0;
 83         for(int i=1;i<=n+1;i++){
 84             if(edge[i][2]){
 85                 if(!ans){
 86                     Search(edge[i][0],edge[i][1],1);
 87                     ret[i]+=1;
 88                 }
 89
 90                 else {
 91                     Search(edge[i][0],edge[i][1],10);
 92                     ret[i]+=10;
 93                 }
 94                 ans+=1;
 95             }
 96         }
 97         for(int i=1;i<=n+1;i++){
 98             if(ret[i]==1)a++;
 99             else if(ret[i]==10)b++;
100             else if(ret[i]==11)c++,b++,a++;
101         }
102         printf("%d\n",a+b-c+(a+b-c-1)*(a+b-c)/2-(a-c)*(a-c-1)/2-(b-c)*(b-c-1)/2-c*(c-1)/2);
103     }
104     return 0;
105 }
时间: 2024-10-13 16:28:04

图论(生成树):HDU 5631Rikka with Graph的相关文章

HDU 5876 Sparse Graph

题目:Sparse Graph 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876 题意:给出一个图(V<=20万,E<=2万),要求先化为补图(每两个点,原本有边删去边,原本没边添加边),然后问指定点S到其他每个点的最短距离. 思路: 普通的广搜应该解决不了...O(n*m)太大,不会很难,比赛时没做出来有点可惜,当知道过了也进不了时,就安慰了许多. 变化一下思路,从起点出发,因为原本边<=2万,那么大部分的点都已经可以到达了

HDU 5333 Undirected Graph LCT+BIT

链接 Undirected Graph Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 184    Accepted Submission(s): 38 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

HDU 5876 Sparse Graph(补图中求最短路)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做. 处理的方法是开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstr

hdu 5876 Sparse Graph 无权图bfs求最短路

Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if

HDU 3435A new Graph Game(网络流之最小费用流)

题目地址:HDU 3435 这题刚上来一看,感觉毫无头绪. .再细致想想.. 发现跟我做的前两道费用流的题是差点儿相同的. 能够往那上面转换. 建图基本差点儿相同.仅仅只是这里是无向图.建图依旧是拆点,推断入度出度.最后推断是否满流,满流的话这时的费用流是符合要求的.输出.不能满流的话,输出NO. 代码例如以下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.

图论——生成树

Tyvj 3737 逐个击破 描述 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走,mzd制定了先切断敌人东洒两头退路然后再逐个歼灭敌人的战略方针. 秉承伟大军事家的战略思想,作为一个有智慧的军长你,遇到了一个类似的战场局面: 现在有N个城市,其中K个被敌方军团占领了,N个城市间有N-1条公路相连,破坏其中某条公路的代价是已知的,现在,告诉你K个敌方军团所在的城市,以及所有公路破坏的代

HDU 5876 Sparse Graph BFS 最短路

Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are notadjacent in G. Now you are given an undirected graph G of N n

HDU 4365——Palindrome graph——————【规律+快速幂】

Palindrome graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1727    Accepted Submission(s): 525 Problem Description In addition fond of programing, Jack also loves painting. He likes to dra

HDU 4365 Palindrome graph(几何变换+快速幂)

HDU 4365 题意:给你一个n*n的画,然后每个格子图上任意k种颜色之一,要求通过翻转旋转后与原图保持一致,且原图已有m个格子有颜色.求有多少种涂法? 思路: 可以发现,我们所求的画是个高度轴对称和中心对称的图形,我们沿两根对称轴与两根中心对称轴把图案切成八份,那么决定其涂色方案只需考虑其中一份即可,若其中一份有x个格子那么答案即是k^x. 然而还有一个条件,即已经有m个格子涂上了颜色,那么我们将m个格子映射至之前选择的八分之一区域内,表明该格子颜色已固定,假设有y个格子颜色已固定,那么答案