【bzoj3246】 Ioi2013—Dreaming
来源:互联网 发布:网络与系统攻击技术 编辑:程序博客网 时间:2024/04/30 21:24
www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接)
题意:给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点件最大距离最小。
Solution
今天考试题,有情况没考虑到。。。
http://www.ccf.org.cn/resources/1190201776262/fujian/xuhaoran2013-07-25-03_33_55.pdf
做法的话其实很简单。我们先把每个连通块两遍dfs,O(n)的找出块内的“接点”和直径,至于怎么找,自己YY一下吧,很简单的。然后考虑将所有连通块联通,不妨将每个连通块看成一个点,将连通块内到“接点”的最远距离看成点权,那么一定是连成一棵菊花树。
答案一共有3种情况。第一,是一个连通块内的直径。第二,是点权最大和次大的两个连通块之间的距离。第三是点权次大和次次大的连通块之间的距离。
奇丑无比的代码:
// bzoj3246#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#define LL long long#define inf 2147483640#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);using namespace std;const int maxn=500010;struct edge {int to,next;LL w;}e[maxn<<1];LL vis[maxn],head[maxn],a[maxn],f[maxn][2],son[maxn];LL cnt,sum,n,m,L,tt,tmp,d,ans;void link(int u,int v,LL w) { e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w; e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].w=w;}bool cmp(LL a,LL b) { return a>b;}void dfs1(int x,int fa) { //x的子树中到x的最远距离和次远距离 vis[x]=1; for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) { dfs1(e[i].to,x); if (f[e[i].to][0]+e[i].w>f[x][0]) { son[x]=e[i].to;f[x][0]=f[e[i].to][0]+e[i].w; } } for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa && e[i].to!=son[x]) f[x][1]=max(f[x][1],f[e[i].to][0]+e[i].w);}void dfs2(int x,LL d,int fa) { //整棵树到x的最远距离 if (d>f[x][0]) { f[x][1]=f[x][0];son[x]=fa;f[x][0]=d; } else if (d>f[x][1]) f[x][1]=d; tt=min(tt,f[x][0]); ans=max(ans,f[x][0]); for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) { if (e[i].to!=son[x]) dfs2(e[i].to,f[x][0]+e[i].w,x); else dfs2(e[i].to,f[x][1]+e[i].w,x); }}int main() { scanf("%lld%lld%lld",&n,&m,&L); for (int u,v,i=1;i<=m;i++) { LL w; scanf("%d%d%lld",&u,&v,&w); link(u,v,w); } for (int i=0;i<n;i++) if (!vis[i]) { tt=inf; dfs1(i,n); dfs2(i,0,n); a[++sum]=tt; } sort(a+1,a+1+sum,cmp); if (sum>=2) ans=max(ans,a[1]+a[2]+L); if (sum>=3) ans=max(ans,a[2]+L+L+a[3]); printf("%lld",ans); return 0;}
0 0
- 【bzoj3246】 Ioi2013—Dreaming
- [bzoj3246] [Ioi2013]Dreaming
- 【BZOJ】3246 [Ioi2013]Dreaming
- bzoj 3246: [Ioi2013]Dreaming
- bzoj 3246 [Ioi2013] Dreaming 题解
- [BZOJ]3246: [Ioi2013]Dreaming 暴力乱搞
- bzoj 3246: [Ioi2013]Dreaming 树形dp+树的直径
- Dreaming
- dreaming
- CSDN——can't stop dreaming neala
- CALIFORNIA DREAMING
- Dreaming in code, Dreaming out reality.
- Dreaming In Code
- Dreaming in code
- stop dreaming now
- Dreaming In Code
- 南邮 OJ 2077 Dreaming
- 《dreaming in code》摘抄
- android Dagger2依赖注入框架总结
- 医电园——开启医疗器械在线交易新模式
- maven 编译报错 java: -source 1.5 中不支持switch 中存在字符串
- 自定义View实现进度条
- Github pages is pretty cool
- 【bzoj3246】 Ioi2013—Dreaming
- SpringMvc 中RedirectAttributes对象重定向传参
- 关于Spring事务<tx:annotation-driven/>的理解。
- Linux内核移植 part4:内核线程对信号的处理
- 元素的padding值使元素整体扩大
- Android提高与总结的Android技能导图
- 3.2.1_Android Training 学习笔记_简单地录像
- 成为首席软件工程师的一些经验
- 欢迎使用CSDN-markdown编辑器-生成效果