POJ 3352 Road Construction 中一个结论的证明
题面
分析:
很多人都给出了做法,在这里不赘述。大概就是先把桥找出来,然后边双缩点,最后统计新图上的度数。因为缩点后为一棵树,所以度数为1(即为叶子)的点的数目+1再除以2下取整就是答案。
这里主要证明一下为什么是对的。
表达式:$$答案=\lfloor\frac{叶子数+1}{2}\rfloor$$
证明:考虑一棵树中,我们找出带权重心,使得重心下每个子节点的叶子节点数尽量的平均(具体实现不讲了),那么在这棵尽量平均的树上,我们每次取两个根节点下子树不同的叶子节点连边,比如说最左边连最右边,左二连右二……,假如是偶数,那么搞定了。否则,那个点再随便连别的一棵子树的一个叶子(其实不是也行)就OK了。
觉得证明不严谨或者有问题的请指出,共同交流。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
using namespace std;
const int maxn=1010;
int n,m,tot,head[maxn],cnt,dfn[maxn],low[maxn],c[maxn],ans,d[maxn];
bool br[maxn<<1],vis[maxn];
struct node
{
int nxt,to;
}edge[maxn<<1];
int read()
{
int x=0,f=1;
char c=getchar();
while (!isdigit(c))
f=c=='-'?-1:1,c=getchar();
while (isdigit(c))
x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
void add(int u,int v)
{
edge[++tot]=(node){head[u],v};
head[u]=tot;
}
void tarjan(int u,int fa) //get bridges
{
int i,v;
low[u]=dfn[u]=++cnt;
for (i=head[u];i;i=edge[i].nxt)
{
v=edge[i].to;
if (!dfn[v])
{
tarjan(v,i);
low[u]=min(low[u],low[v]);
if (low[v]>dfn[u])
br[i]=br[i^1]=1;
}
else
if (i!=(fa^1))
low[u]=min(low[u],dfn[v]);
}
}
void dfs(int u,int co)
{
c[u]=co;
int i,v;
for (i=head[u];i;i=edge[i].nxt)
{
v=edge[i].to;
if (br[i]||c[v])
continue;
dfs(v,co);
}
}
int main()
{
int i,j,u,v;
n=read();
m=read();
tot=1;
for (i=1;i<=m;i++)
{
u=read();
v=read();
add(u,v);
add(v,u);
}
for (i=1;i<=n;i++)
if (!dfn[i])
tarjan(i,0);
cnt=0;
for (i=1;i<=n;i++)
if (!c[i])
dfs(i,++cnt);
for (i=1;i<=n;i++)
for (j=head[i];j;j=edge[j].nxt)
if (c[i]!=c[edge[j].to])
{
d[c[i]]++;
d[c[edge[j].to]]++;
}
for (i=1;i<=cnt;i++)
if (d[i]==2)
ans++;
printf("%d\n",(ans+1)/2);
return 0;
}
POJ 3352 Road Construction 中一个结论的证明的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- 【边双连通】poj 3352 Road Construction
http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- POJ 3352 Road Construction (边双连通分量)
题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...
随机推荐
- Maven下载安装测试
一.Maven下载 在Maven官网下载压缩包 二.安装 解压后目录如下 bin目录包含mvn的运行脚本 boot目录包含一个类加载器的框架,加载自己的类库 conf是配置文件目录 lib目录包含一些 ...
- java中的包注意事项
1:需要导入包的三个地方 a:需要导入第三方的jar包中的类或接口 b:需要导入除了java.lang包的其他包中的类(jdk中的类) c:需要导入自己写的不同包的类 2:引入包的三种方式 a:imp ...
- Linux(17):Shell编程(4)
案例1:批量生成随机字 符 文件名案例 使用for 循环在 /neo 目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串 neo创建的结果名称示例 如下: [root ...
- [转载]C++二维动态数组memset()函数初始化
来源:https://blog.csdn.net/longhopefor/article/details/20994919 先说说memset函数: void *memset(void *s,int ...
- Redis分布式之前篇
第一篇:初识Redis 一.Redis是什么? Redis 是一个开源(BSD许可)的,使用ANSI C语言编写的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据 ...
- Vue报错:Property or method "XXX" is not defined on the instance but referenced during render. Make sure that this property is reactive...
在Vue中定义方法或者属性时,因为粗心疏忽可以能会报该错误 [Vue warn]: Property or method "search" is not defined on th ...
- LLVM 安装教程(包安装)
LLVM 安装教程 环境:ubuntu16.04 llvm-4.0 clang-4.0 步骤: 1.依赖库安装 $ sudo apt-get install build-essential curl ...
- Linux Exploit系列之七 绕过 ASLR -- 第二部分
原文地址:https://github.com/wizardforcel/sploitfun-linux-x86-exp-tut-zh/blob/master/7.md 这一节是简单暴力的一节,作者讲 ...
- 在iPhone开发中实现解压缩gzip
在iPhone开发中实现解压缩gzip是本文要介绍的内容,最近做的一个东西中,需要从网络获取xml文件,但是该文件用了gzip压缩的.搜索一 下有人说gzip压缩的用urlrequest可以自己解压, ...
- log4net 报错
之前在网上学习了一种log4net的日志监控,这种方式我觉得很不错,至少我个人认为很好,但是最紧缺发现一个为题,就是再session过期的时候页面跳转时候 会报错,这个搞了很久没搞明白,我直接用例子讲 ...