图论(生成树):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
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?
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.
3
1 2
2 3
3 1
1 3
题意:给出一张 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的更多相关文章
- HDU 6321 Dynamic Graph Matching
HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...
- 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 ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu 4647 Another Graph Game
题意: 有N个点,M条边. 点有权值, 边有权值. Alice, Bob 分别选点. 如果一条边的两个顶点被同一个人选了, 那么能获得该权值.问 Alice - Bob? 链接:http://acm. ...
- HDU 5876 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- 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 ...
- HDU 5333 Undirected Graph(动态树)
题意 给定一棵 \(n\) 个节点, \(m\) 条边的无向图,每个点有点权,有 \(q\) 个询问,每次询问若删去存在一个节点权值在 \([L,R]\) 范围外的边,剩下的图构成了多少个连通块(询问 ...
- HDU 5876 Sparse Graph(补图中求最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...
随机推荐
- 浅谈Android系统的图标设计规范
http://homepage.yesky.com/89/11620089.shtml 目前移动平台的竞争日益激烈,友好的用户界面可以帮助提高用户体验满意度,图标Icon是用户界面中一个重要的组成部分 ...
- 游标中的static参数
以下测试用例将演示,使用static的游标和不使用的区别: if object_id(N't_test',N'u') is not null drop table t_test go create t ...
- RAC配置、安装
RAC 配置及安装 2012年12月30日 星期日 21:49 ******************************************************************* ...
- 读懂IL代码(二)
上一篇提到了最基本的IL代码,应该是比较通俗易懂的,所以有了上一篇的基础之后,这篇便要深入一点点的来讲述了. 首先我必须再来说一些重要的概念: Evaluation Stack(评估栈):这是由.NE ...
- 在WinForm应用程序中,使用选项卡控件来加载不同的Form界面!
TabPage tp=new TabPage(); your选项卡控件.Controls.Add(tp); From1 frm=new Form1(); frm.TopLevel = false; f ...
- PHPCMS 使用图示和PHPCMS二次开发教程(转)
PHPCMS V9 核心文件说明 模块与控制器 模块: phpcms v9框架中的模块,位于phpcms/modules目录中 每一个目录称之为一个模块.即url访问中的m. 访问content模块示 ...
- 自动化工具word文档批量转html
企业有很多的科室,科室的每个人或多或少都会写一些文档,有些文档领导需要浏览,解决的办法是将编辑的文档打印出来,供领导浏览,或是为了节约企业成本,文档就在人与人这间或部门之间copy过来,copy过去. ...
- win7访问windows server 2003服务器出现未知的用户名或者错误的密码(转载)
直接放答案,感谢网友提供答案,否则自已还一直在纳闷,为什么? win7系统的安全机制限制了登陆.只要系统时间和win2003服务器的系统时间相差很多,系统就会阻止其登陆,并显示错误信息:"未 ...
- ecshop后台根据条件查询后不填充table 返回的json数据,content为空?
做ecshop后台开发的时,根据条件查询后,利用ajax返回的content json数据内容为空,没有填充table 效果 预期效果 问题: make_json_result($smarty -&g ...
- PHP对象类型在内存中的分配
对象类型和整型.字符串等类型一样,也是PHP中的一种数据类型.都是在程序中用于存储不同类型数据使用的,在程序运行时它的每部分内容都要先加载到内存中再被使用.那么对象类型的数据在内存中是如何分配的呢?先 ...