博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】
阅读量:4622 次
发布时间:2019-06-09

本文共 1490 字,大约阅读时间需要 4 分钟。

题目:

题意:给出n个点,n-1条边的一棵树。然后要在两个点上建立两个消防站。让全部点的到消防站最大距离的点的这个距离最小。

分析:首先先求这个树的直径。然后在树的直径的中点处把树分成两棵树。然后在把两棵树分别取中点的最大值就是ans值。

这个题目数据有点水了感觉。。。

AC代码:

#include 
#include
#include
#include
#include
#include
using namespace std;const int N = 204000;int n;vector
v[N];int vis[N],father[N];vector
line;int BFS(int s,int flag){ queue
q; int e=s; line.clear(); memset(vis,0,sizeof(vis)); memset(father,-1,sizeof(father)); vis[flag]=1; q.push(s); vis[s]=1; int ans=1; while(!q.empty()) { int f=q.front(); q.pop(); for(int i=0; i
ans){ e=k; ans=vis[k]; } q.push(k); } } for(int i=e;i!=-1;i=father[i]) line.push_back(i); return e;}struct Node{ int one,two,ans;};void print() //输出{ for(int i = 0;i
Yougth(int s,int t){ int p1 = BFS(s,t); int p2 = BFS(p1,t); int len = line.size(); int one = line[len/2]; int tmp = len/2; pair
ans(one,tmp); return ans;}Node Importent(int fir,int sec){ Node pps; int ans = -1; pair
tt = Yougth(fir,sec); pps.one = tt.first; ans = max(ans,tt.second); tt = Yougth(sec,fir); pps.two = tt.first; ans = max(ans , tt.second); pps.ans = ans; return pps;}void solve(){ Node pps,ans2; int p1 = BFS(1,n+1); int p2 = BFS(p1,n+1); int len = line.size()/2; int a = line[len-1],b = line[len] , c = line[len+1]; if(line.size()%2==0) { pps = Importent(a,b); } else { pps = Importent(a,b); ans2 = Importent(b,c); if(ans2.ans

转载于:https://www.cnblogs.com/jhcelue/p/7072423.html

你可能感兴趣的文章
LeetCode 112. Path Sum (二叉树路径之和)
查看>>
mysql数据恢复
查看>>
java list
查看>>
算法练习2---斐波那契数列java版
查看>>
用VISIO2013绘制E-R图
查看>>
每日站立会议03
查看>>
软件工程第一次作业
查看>>
初步了解HTML
查看>>
九度OJ 1165:字符串匹配 (模式匹配)
查看>>
Swift Storyboard找不到类文件
查看>>
Hibernate-延迟加载和立即加载
查看>>
Java中数据类型的转换
查看>>
闲扯一篇 聊聊与博客园代码改变世界的那些事
查看>>
237. Delete Node in a Linked List
查看>>
【口胡】简谈福建省夏令营
查看>>
wince 位图的使用
查看>>
WCF 配置说明
查看>>
Design Patterns Addendum
查看>>
List of FTP Sever/Client Software
查看>>
IDEA 14快捷键
查看>>