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种情况,删两条边用总可能数-不合法数得到。

  第一名哦!!!

 #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 ;
}

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

  1. HDU 6321 Dynamic Graph Matching

    HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...

  2. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  3. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  4. hdu 4647 Another Graph Game

    题意: 有N个点,M条边. 点有权值, 边有权值. Alice, Bob 分别选点. 如果一条边的两个顶点被同一个人选了, 那么能获得该权值.问 Alice - Bob? 链接:http://acm. ...

  5. HDU 5876 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  6. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  7. UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>

    J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  8. HDU 5333 Undirected Graph(动态树)

    题意 给定一棵 \(n\) 个节点, \(m\) 条边的无向图,每个点有点权,有 \(q\) 个询问,每次询问若删去存在一个节点权值在 \([L,R]\) 范围外的边,剩下的图构成了多少个连通块(询问 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

随机推荐

  1. JS快速排序和去重

    JS的快速排序和JS去重在面试的时候问的挺多的.下面是我对快速排序的理解,和快速排序,去重的代码. 1.什么是快速排序? 第一步: 快速排序就是去个中间值,把比中间值小的放在左边设为arrLeft,比 ...

  2. 处理移动端click事件300ms延迟的好方法—FastClick

    下载地址:https://github.com/ftlabs/fastclick 1.click事件为什么有延迟? “...mobile browsers will wait approximatel ...

  3. SQL Server Management Studio 使用作业实现数据库备份

    1.数数据库备份脚本: 数据库备份:DECLARE @BcpFile VARCHAR(30),@SQLBACKUP VARCHAR(1000),@BcpFullFile VARCHAR(100) SE ...

  4. django连接已有的数据库

    以连接postgresql为例: 1.安装psycopg2,下载地址:http://www.stickpeople.com/projects/python/win-psycopg/ 2.配置setti ...

  5. HibernateTool的安装和使用(Eclipse中)

    http://blog.sina.com.cn/s/blog_919273e20101g1t7.html

  6. NRPE: Unable to read output 问题处理总结

    自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...

  7. FluentNHibernate当数据库设置默认值时,使用插入操作,导致默认值没有写入问题

    需要再映射属性字段增加Not.Insert() Map(x => x.Provrince, "PROVRINCE").Not.Insert(); Map(x => x. ...

  8. Android布局管理器(线性布局)

    线性布局有LinearLayout类来代表,Android的线性布局和Swing的Box有点相似(他们都会将容器里面的组件一个接一个的排列起来),LinearLayout中,使用android:ori ...

  9. 【转】IOS 30多个iOS常用动画,带详细注释

    原文: http://blog.csdn.net/zhibudefeng/article/details/8691567 CoreAnimationEffect.h 文件 // CoreAnimati ...

  10. node http.get请求

    var http = require('http'); var querystring = require('querystring') var url = 'http://www.baidu.com ...