2227 邮票--FUoj(链接表+树的直径)】的更多相关文章

http://acm.fzu.edu.cn/problem.php?pid=2227 我感觉这道题可以随意搞 题目大意: 给你的一个图就是一条链,但是不知道起始点和结束点,而且每个点只会访问一次. 因为数太大了 只能用邻接表保存. 我不知不觉的用了树的直径   代码写的太乱了  但是是我自己写的,虽然这道题真的不难,但是还是很开心 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ma…
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明:   1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com…
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包括n个交叉路口和n-1条街道,任意一条街道连接两个交叉路口,并且任意两个交叉路口之间都存在一条路径互相连接.经过长期调查结果显示如果一个交叉路口位于W市交通网的最长路径上,那么这个路口必然拥挤不堪,所谓最长路径定义为某条路径p=(v1,v2,v3…vk),路径经过的路口各不相同且城市中不存在长度>k…
设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std; ; struct node { int to; int v; int next; }edge[MAXN…
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][Status][Discuss] Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Output 输出一个整数,表示新建了K 条道路后能达到的最小巡逻距离.…
B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3820 Appoint description:  System Crawler  (2015-08-15) Description Marjar University is a beautiful and peaceful place.…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ,ans = k-1: b:若k>=r+1,ans = r+(k-(r+1))*2: 代码: #include "stdio.h" #include "string.h&…
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ①任意从一个点u出发bfs,设其能到的最远点为v ②从v出发重新bfs,设其能到达的最远点为s ③则树的直径就是v->s 证明: 若能证明从任意一个点出发,bfs到的最远点一定在树的直径的端点上,那么第二次bfs就可以证明一定正确了,下面来证明第一次bfs正确性: ①若选择的点u在直径上,那么能到…
SDOI2011的Dayx第2题 题意: 在树中找到一条权值和不超过S的链(为什么是链呢,因为题目中提到“使得路径的两端都是城市”,如果不是链那不就不止两端了吗——怎么这么机智的感觉...),使得不在链上的点与这条链的距离最大值最小. SOL: 最大值最小!这不是二分的节奏么?然而hzw学长说二分更直观我却一点都没有体会到... 这道题的关键是猜想(貌似还挺好想)并证明(貌似一直都是可有可无的东西,不过还挺好证的),路径一定在直径上,那么我们先两遍*FS找到直径,用一个队列维护链上的路径,以及预…
Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers…
E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are n v…
Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2833    Accepted Submission(s): 917 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble i…
//Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; ; struct node { int u,v,c; node() { } node(int u,int v,int c):u(u),v(v),c(c) { } }p[*imax_n]; int e; i…
//Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring> #include <queue> #include <iostream> using namespace std; ; int dis[imax_n]; int d1[imax_n]; int d2[imax_n]; int head[imax_n]; *imax_n]; i…
一个男孩有n只玩具蜘蛛,每只蜘蛛都是一个树的结构,现在男孩准备把这n只小蜘蛛通过粘贴节点接在一起,形成一只大的蜘蛛.大的蜘蛛也依然是树的结构.输出大的蜘蛛的直径. 知识: 树的直径是指树上的最长简单路 求树的直径有个结论: 假设s-t这条路径为树的直径,或者称为树上的最长路. 从任意一点u出发搜到的最远的点一定是s.t中的一点,然后再从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜或者深搜就可以找出树的最长路 证明:反证法. 那回到这道题,要使得大蜘蛛的直径最大,就要使连接的小蜘蛛…
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair o…
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用的双连通分量,我直接无向图改成有向图搞得强连通水过. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <vector> #include <…
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either through a direct link, or through a chain of links, by relayi…
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” consists of n servers and m two-way communication links. Two servers can communicate either thr…
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan+缩点+树的直径 题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少. 先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. */ #pragma comment(linker, "/STACK:1024000000,1024000000")…
题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端,这样减少的桥边,最多,所以就是求树的直径 注:这题有重边,所以边双缩点也需要用重边版的 #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include…
Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的行业是消防业.由于政府对国民的热情忍无可忍(大量的消防经费开销)可是却又无可奈何(总统竞选的国民支持率),所以只能想尽方法提高消防能力. 现在这个国家的经费足以在一条边长度和不超过s的路径(两端都是城市)上建立消防枢纽,为了尽量提高枢纽的利用率,要求其他所有城市到这条路径的距离的最大值最小. 你受…
题目大意 给定一颗树,要求走过其中连续的k个点,使得步数最少 题解 每条边要么经过两次,要么一次,因为我们的目标就是使得走一次的边尽量的多,这样就转换成求树的直径了,求树的直径我用的是两次dfs,先随便从一个点开始dfs,找出以这个点为根距离它最远的结点,假设为s,然后再从s结点进行一个dfs,距离s结点最远的点与s点的距离就是树的直径(假设为d),最后判断一下k和树的直径的大小,如果k-1小于树的直径,那么直接输出k-1,否则答案就是d+(k-1-d)*2 代码: #include <iost…
Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5093    Accepted Submission(s): 1131 Problem Description N planets are connected by M bidirectional channels that allow instant transport…
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undire…
Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Description The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them eithe…
Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 1157 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such…
Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com…