https://www.luogu.org/problemnew/show/T16502

无向图  缩点  树的直径  到直径两个端点的距离的较大值

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring> using namespace std;
const int N = 2e5 + ; #define yxy getchar() int n, m, now = , now_2 = , Tarjan_tim, Top, Bel_tim, Root, Max_d;
int head[N], head_2[N], Stack[N], Dfn[N], Low[N], Bel[N], dis[N], dist[N];
bool vis[N];
struct Node {int u, v, w, nxt;} E[N << ];
struct Node_2 {int u, v, w, nxt;} G[N << ]; inline int read(){
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} void add_E(int u, int v, int w){
E[now].v = v; E[now].w = w; E[now].nxt = head[u]; head[u] = now ++;
} void Tarjan(int u, int fa){
Dfn[u] = Low[u] = ++ Tarjan_tim;
vis[u] = ;
Stack[++ Top] = u;
for(int i = head[u]; ~ i; i = E[i].nxt) {
int v = E[i].v;
if(!Dfn[v]){
Tarjan(v, u);
Low[u] = min(Low[u], Low[v]);
}
else if(vis[v] && v != fa) Low[u] = min(Low[u], Low[v]);
}
if(Dfn[u] == Low[u]){
++ Bel_tim;
Bel[u] = Bel_tim; vis[u] = ;
while(Stack[Top] != u){
Bel[Stack[Top]] = Bel_tim;
vis[Stack[Top]] = ;
Top --;
} Top --;
}
} void add_G(int u, int v, int w){
G[now_2].v = v; G[now_2].w = w; G[now_2].nxt = head_2[u]; head_2[u] = now_2 ++;
} void Build_G(){
for(int i = ; i <= n; i ++) head_2[i] = -;
for(int u = ; u <= n; u ++)
for(int i = head[u]; ~ i; i = E[i].nxt)
if(Bel[u] != Bel[E[i].v])
add_G(Bel[u], Bel[E[i].v], E[i].w);
} void dfs_first(int u, int fa){
for(int i = head_2[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v == fa) continue ;
dis[v] = dis[u] + G[i].w;
if(dis[v] > Max_d) Max_d = dis[v], Root = v;
dfs_first(v, u);
}
} void dfs_second(int u, int fa){
for(int i = head_2[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v == fa) continue ;
dist[v] = dist[u] + G[i].w;
dfs_second(v, u);
}
} void Get_Answer(){
dfs_first(, -);
memset(dis, , sizeof dis);
Max_d = ;
dfs_first(Root, -);
Max_d = ;
dfs_second(Root, -);
for(int i = ; i <= n; i ++) printf("%d\n", max(dis[Bel[i]], dist[Bel[i]]));
} int main()
{
n = read(); m = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= m; i ++){
int u = read(), v = read(), w = read();
add_E(u, v, w); add_E(v, u, w);
}
for(int i = ; i <= n; i ++) if(!Dfn[i]) Tarjan(i, );
Build_G();
Get_Answer();
return ;
}

[DK] 化学竞赛的大奖的更多相关文章

  1. 2017-11-7 NOIP模拟赛

    1.数学老师的报复 #include<iostream> #include<cstdio> using namespace std; int cnt; ]; long long ...

  2. 2017-11-07-noip模拟题

    T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...

  3. 关于ACM,关于CSU

    原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...

  4. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

  5. 1011: [HNOI2008]遥远的行星

    1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 2241  Solved ...

  6. [2017BUAA软工]第0次个人作业

    第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 我觉得我选择计算机系完全是误打误撞吧.当时我的分数上北航是没问题的,所以填专业时就是机械,电气,自动化,计算机等 ...

  7. may be a diary?

    [About Me] SD某弱校高二的OIer. qq 995681518,欢迎一起交流~ 喵喵喵喵喵 "当你想要颓废的那一刻,想一想当初为什么走到了这里." 以下文字充满负面情绪 ...

  8. P2580 于是他错误的点名开始了

    题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...

  9. 于是他错误的点名开始了(trie树)

    题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...

随机推荐

  1. @Adaptive注解

    关于@Adaptive注解 引用dubbo官方文档的一段话: ​ Adaptive 可注解在类或方法上.当 Adaptive 注解在类上时,Dubbo 不会为该类生成代理类.注解在方法(接口方法)上时 ...

  2. c++学习---const 和 string

    当在两个文件定义了同名的const变量时,相对于定义了两个独立的变量 想要在一个文件中定义一个const变量并在其他文件中使用他:不管时声明还是定义,都加上extern关键字 因为const对象一经初 ...

  3. 剑指offer(9)——用两个栈实现队列

    题目: 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 首先定义两个栈stack1. ...

  4. java实现工程配置文件敏感字段加解密

    以下引自他人博客: 1. 需求背景我们在开发应用时,需要连接数据库,一般把数据库信息放在一个属性配置文件中,比如***.properties,具体的内容 #mysql的配置文件jdbc.url=jdb ...

  5. [Vue]避免 v-if 和 v-for 用在同一个元素上

    一般我们在两种常见的情况下会倾向于这样做: 情形1:为了过滤一个列表中的项目 (比如 v-for="user in users" v-if="user.isActive& ...

  6. CCF 201809-1 卖菜

    题目: 问题描述 在一条街上有n个卖菜的商店,按1至n的顺序排成一排,这些商店都卖一种蔬菜. 第一天,每个商店都自己定了一个价格.店主们希望自己的菜价和其他商店的一致,第二天,每一家商店都会根据他自己 ...

  7. ligerui tab 部分记载

    打开一个Tab $(".strength_box").click(function () { var id = $(this).attr("data"); va ...

  8. springboot启动流程(三)Environment简介

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 简介 上一篇文章中,我们简单了解了一下SpringApplication的run方法的代码逻辑 ...

  9. 图片预加载的插件使用-jquery.imgpreload.min.js

    使用方法: //图片预加载 var the_images = [];//新建一个数组,然后将要加载的图片地址推入这个数组中: the_images.push( 'bg.jpg' ); var load ...

  10. el-table表格错误问题

    .el-table--border th.gutter:last-of-type { display: block!important; width: 17px!important; } 如果不行,则 ...