图论(生成树):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来做 ...
随机推荐
- [HNOI2012] 矿场搭建
/* codevs 1996 连通性问题 Tarjan+割点 可以感性的想一想 一定炸割点最好 否则 没有什么影响 先求出割点来 对于剩下的点们 缩一下 当然不能包括割点 这里的缩 因为删了割点就不是 ...
- js获取上一个月、下一个月
/** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ function getPreMonth(date) { var arr = date. ...
- Struts2 中拦截器和Action的调用关系(写的很好)
http://blog.csdn.net/hackerain/article/details/6991082
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
http://blog.csdn.net/agileclipse/article/details/17161225 详情请点击链接查看
- Alljoyn 概述(1)
Alljoyn Overview Feb. 2012- AllJoyn 是什么? • 2011年2月9日发布,由 QuiC(高通创新中心)开发维护的开源软 件项目,采用 Apache license ...
- iOS 常见错误:CALayer position contains NaN: [14 nan]
Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contai ...
- 【转】 iOS KVO KVC
原文: http://www.cocoachina.com/industry/20140224/7866.html Key Value Coding Key Value Coding是cocoa的一个 ...
- 显示scrollbar
修改CSS overflow的值 overflow: 参考MDN 例子: overflow: auto or scroll
- Bridge 模式
Bridge 模式将抽象和行为划分开来,各自独立,但能动态的结合.在面向对象设计的基本概念中,对象这个概念实际是由属性和行为两个部分组成的,属性我们可以认为是一种静止的,是一种抽象,一般情况下,行为是 ...
- SGU 195. New Year Bonus Grant
时间限制:0.75s 空间限制:4M 题意: 在一颗树(最多500000个节点)中,可以对节点染色,但是一个节点染了色后,它的父节点和兄弟节点都不能再染了,求最大的染色节点数,并输出所有染色节点. S ...