图论(生成树):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来做 ...
随机推荐
- C# 内存管理优化畅想(三)---- 其他方法&结语
前两篇文章提出的优化方法,都是不需要修改源代码的,而是在CLR或JIT层面进行自动优化的.但本文中提出的优化方法则需要引入新的语法,开发者只有在源代码中使用了这些新语法,才会获得优化. 1. 允许对象 ...
- jquery选择器取值和url正则匹配
用到的简单jquery知识,简单总结一下,一是能加深自己的记忆,二是方便自己以后查看.常言道"好记性不如烂笔头",要养成常总结.常记录的好习惯.慢慢的发现jquery很有意思,很强 ...
- (转)system()函数
[C/C++]Linux下system()函数引发的错误 今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...
- sql根据'/'截取最后的字符串
filpath字段值:/DataFile/UpLoad/Logo/NoPhoto.jpg select filpath,REVERSE((SUBSTRING(REVERSE(FilPath),0,CH ...
- SQL某个字段在原内容上增加固定内容或replace查找替换内容
今天正好遇到一个SQL小问题,特做备注 在原有的表中数据如pic 在不动原内容的基础上增加../路径,但不能修改原数据值 原数据 SQL: pic字段 需要增加'../'的内容 update Bmps ...
- Dictionary的遍历和修改
/// <summary> /// 初始化一个Dic /// </summary> public static void mainTe ...
- Java环境的安装与配置
Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...
- Alljoyn 概述(3)
开发工具 • scons:一个 Python写的自动化构建工具,是对 gnu make 改进的替代工具 • D-Feet:一个D-Bus调试工具 • C++ Code Generator Tool ( ...
- 【转】伟大的RAC和MVVM入门(一)
原文:http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-introduction/ 翻译自ReactiveCocoa and MVV ...
- Xcode4.4中,代码无法高亮、无法自动补全
1.代码无法高亮显示:2.代码不能自动补全,或者给出提示建议:(当然这个补全的功能我在设置当中是打开的状态)3.新建一个项目,代码还是依然没有高亮显示,但是有补全功能:4.然后我就在网络上搜索了相关的 ...