图论(生成树):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
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5634 5633 5632 5630 5629 
 

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

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

  哔了狗有木有!!!!!!!!!!!!!!!!!!!!!!!!

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

  我的思路是:先DFS,建一棵生成树,树中只有n-1条边,那剩下来的两条边,再加入生成树中后就会形成两个环,设两个环边的集合分别为A与B,它们的交集为C,设a=|A|,b=|B|,c=|C|,那么答案就是

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,就是只删一条边有a+b-c种情况,删两条边用总可能数-不合法数得到。

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

  第一名哦!!!

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=;
int edge[maxn][];
int fa[maxn],cnt=,fir[maxn],nxt[maxn<<],to[maxn<<];
int dep[maxn],pre[maxn],ret[maxn],ID[maxn<<];
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} void addedge(int a,int b,int id)
{
nxt[++cnt]=fir[a];ID[cnt]=id;
to[cnt]=b;fir[a]=cnt;
} void DFS(int node)
{
for(int i=fir[node];i;i=nxt[i])
{
if(dep[to[i]])continue;
dep[to[i]]=dep[node]+;
pre[to[i]]=i;
DFS(to[i]);
}
}
int Search(int x,int y,int d)
{
while(x!=y){
if(dep[x]<dep[y])
swap(x,y);
ret[ID[pre[x]]]+=d;
x=to[pre[x]^];
}
}
void Init()
{
memset(fir,,sizeof(fir));
memset(ret,,sizeof(ret));
cnt=;
} int main()
{
freopen("data.in","r",stdin);
freopen("wrong.out","w",stdout);
int T,n;
scanf("%d",&T);
while(T--)
{
Init();
scanf("%d",&n);
for(int i=;i<=n+;i++){
fa[i]=i;
} for(int i=;i<=n+;i++)
scanf("%d%d",&edge[i][],&edge[i][]);
for(int i=;i<=n+;i++){
int a=find(edge[i][]),b=find(edge[i][]);
if(a==b)
edge[i][]=;
else{
fa[a]=b;
addedge(edge[i][],edge[i][],i);
addedge(edge[i][],edge[i][],i);
}
}
int flag=;
for(int i=;i<=n;i++)
if(find(i)!=find(i-)){
printf("0\n");
flag=;
break;
}
if(flag==)continue;
dep[]=;
DFS();
int ans=,a=,b=,c=;
for(int i=;i<=n+;i++){
if(edge[i][]){
if(!ans){
Search(edge[i][],edge[i][],);
ret[i]+=;
} else {
Search(edge[i][],edge[i][],);
ret[i]+=;
}
ans+=;
}
}
for(int i=;i<=n+;i++){
if(ret[i]==)a++;
else if(ret[i]==)b++;
else if(ret[i]==)c++,b++,a++;
}
printf("%d\n",a+b-c+(a+b-c-)*(a+b-c)/-(a-c)*(a-c-)/-(b-c)*(b-c-)/-c*(c-)/);
}
return ;
}
上一篇:[cocos2dx笔记008]cocos2d 用luabridge手动绑定类


下一篇:Linq中常用的方法