洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station
题目描述
The first stage of train system reform (that has been described in the problem Railways of the third stage of 14th Polish OI.
However, one needs not be familiar with that problem in order to solve this task.) has come to an end in Byteotia. The system consists of bidirectional segments of tracks that connect railway stations. No two stations are (directly) connected by more than one segment of tracks.
Furthermore, it is known that every railway station is reachable from every other station by a unique route. This route may consist of several segments of tracks, but it never leads through one station more than once.
The second stage of the reform aims at developing train connections.
Byteasar count on your aid in this task. To make things easier, Byteasar has decided that:
one of the stations is to became a giant hub and receive the glorious name of Bitwise, for every other station a connection to Bitwise and back is to be set up, each train will travel between Bitwise and its other destination back and forth along the only possible route, stopping at each intermediate station.
It remains yet to decide which station should become Bitwise. It has been decided that the average cost of travel between two different stations should be minimal.
In Byteotia there are only one-way-one-use tickets at the modest price of bythaler, authorising the owner to travel along exactly one segment of tracks, no matter how long it is.
Thus the cost of travel between any two stations is simply the minimum number of tracks segments one has to ride along to get from one stations to the other.
Task Write a programme that:
reads the description of the train system of Byteotia, determines the station that should become Bitwise, writes out the result to the standard output.
给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大
输入输出格式
输入格式:
The first line of the standard input contains one integer () denoting the number of the railway stations. The stations are numbered from to . Stations are connected by segments of tracks. These are described in the following lines, one per line. Each of these lines contains two positive integers and (), separated by a single space and denoting the numbers of stations connected by this exact segment of tracks.
输出格式:
In the first and only line of the standard output your programme should print out one integer - the optimum location of the Bitwise hub.
If more than one optimum location exists, it may pick one of them arbitrarily.
输入输出样例
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
7
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 1000010
using namespace std;
int n,num,head[maxn],id,dep[maxn],mx;
struct node{
int to,pre;
}e[maxn*];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void dfs(int now,int father){
dep[now]=dep[father]+;
if(dep[now]>mx||(dep[now]==mx&&id>now)){
mx=dep[now];
id=now;
}
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(to==father)continue;
dfs(to,now);
}
}
int main(){
scanf("%d",&n);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
dfs(,);
cout<<id;
}
70分 找树上最长链的一个端点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000010
using namespace std;
int n,num,head[maxn],sz[maxn],fa[maxn],dep[maxn];
long long f[maxn],ans;
struct node{int to,pre;}e[maxn*];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void dfs(int now,int father){
dep[now]=dep[father]+;
sz[now]=;
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(to==father)continue;
dfs(to,now);
sz[now]+=sz[to];
}
}
void find(int now){
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(dep[to]>dep[now]){
f[to]=f[now]+n-*sz[to];
find(to);
}
}
}
int main(){
scanf("%d",&n);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
dfs(,);
for(int i=;i<=n;i++)f[]+=dep[i];
find();
int mark=;
for(int i=;i<=n;i++)
if(ans<f[i]){
ans=f[i];
mark=i;
}
cout<<mark;
return ;
}
100分 树形dp
洛谷P3478 [POI2008]STA-Station的更多相关文章
- 洛谷 P3478 [POI2008]STA-Station
题目描述 The first stage of train system reform (that has been described in the problem Railways of the ...
- BZOJ1123或洛谷3469 [POI2008]BLO-Blockade
BZOJ原题链接 洛谷原题链接 若第\(i\)个点不是割点,那么只有这个点单独形成一个连通块,其它点依旧连通,则答案为\(2\times (n-1)\). 若第\(i\)个点是割点,那么去掉这个点相关 ...
- 题解【洛谷P3478】[POI2008]STA-Station
题面 设\(dp_i\)表示以\(i\)为根节点时所有节点的深度之和. 首先以 \(1\) 为根求出所有点深度之和\(dp_1\),并预处理每个点的子树大小. 设 \(v\) 是 \(u\) 的孩子, ...
- 洛谷 P3477 [POI2008]PER-Permutation 解题报告
P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...
- 洛谷 P3467 [POI2008]PLA-Postering
P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...
- 洛谷 P3469 [POI2008]BLO-Blockade (Tarjan,割点)
P3469 [POI2008]BLO-Blockade https://www.luogu.org/problem/P3469 题目描述 There are exactly nn towns in B ...
- 洛谷P3469[POI2008]BLO-Blockade
题目 割点模板题. 可以将图中的所有点分成两部分,一部分是去掉之后不影响图的连通性的点,一部分是去掉之后影响连通性的点,称其为割点. 然后分两种情况讨论,如果该点不是割点,则最终结果直接加上2*(n- ...
- 洛谷 P3469 [POI2008]BLO-Blockade 题解
一道经典的割点例题,用size数组记录该子树有多少个节点,sum是这棵搜索树上有多少个节点,sum*(n-sum-1)是将点删掉后的数对数量. #include<iostream> #in ...
- 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】
题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...
随机推荐
- pom详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- mybatis学习第(一)天
课程安排: Mybatis和springMVC通过订单商品案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开发)问题总结 Mybatis框架原理 Mybatis的入 ...
- numpy.ndarray类型的数组元素输出时,保留小数点后4位
因为计算结果数组中每个值都是很长的一串小数,看起来比较乱,想格式化一下输出方式. 这是个看起来很简单的问题,但是方法找了很久. 方法也是看起来很简单,用 numpy.set_printoptions( ...
- [BZOJ2780][SPOJ8093]Sevenk Love Oimaster
bzoj luogu 题面 给定n个模板串,以及m个查询串. 依次查询每一个查询串是多少个模板串的子串. sol 广义后缀自动机裸题? 先建出\(SAM\),然后记录一下每个节点分别在多少个模板串里出 ...
- 如何在开启了log-bin的MySQL Server中创建FUNCTION
在MySQL主从复制机器的master的数据库中创建function,报出如下错误: Error Code: 1418. This function has none of DETERMINISTIC ...
- TFS自定义开发中的反射应用
最近CM(Configuration Management) 的同事在自定义开发TFS的过程中遇到一个问题. 领导要求快速开发一个工具, 可以自动连接TFS,然后自动Check out一些word文件 ...
- 【转】 Pro Android学习笔记(七九):服务(4):远程服务的实现
目录(?)[-] 远程服务的实现小例子 对外开放远程服务的接口 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flow ...
- 《Kubernetes权威指南第2版》学习(四)kubernetes基本概念和术语
1: etcd是干什么的: 键-值存储仓库,用来配置共享和服务发现. k8s把Node, pod,replication controller, Services看做是资源对象,这些资源对象可以通过K ...
- Cypress USB3014 C++DLL 导入问题
VS2017编译cpp工程出现问题 硬件型号:芯片版本 Cypress FX3 USB3014(和芯片无关)) 重现步骤: 1.解压 FX3_SDK_Windows_v1.3.3.exe 2.VS20 ...
- uboot启动参数设置分类及方法
一.nfs启动内核与根文件系统,内核与根文件系统都在nfs上 bootargs=noinitrd root=/dev/nfs rw nfsroot=192.168.0.1:/home/tekkaman ...