[DK] 化学竞赛的大奖
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] 化学竞赛的大奖的更多相关文章
- 2017-11-7 NOIP模拟赛
1.数学老师的报复 #include<iostream> #include<cstdio> using namespace std; int cnt; ]; long long ...
- 2017-11-07-noip模拟题
T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...
- 关于ACM,关于CSU
原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...
- luogu P2580 于是他错误的点名开始了
luogu P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...
- 1011: [HNOI2008]遥远的行星
1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2241 Solved ...
- [2017BUAA软工]第0次个人作业
第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 我觉得我选择计算机系完全是误打误撞吧.当时我的分数上北航是没问题的,所以填专业时就是机械,电气,自动化,计算机等 ...
- may be a diary?
[About Me] SD某弱校高二的OIer. qq 995681518,欢迎一起交流~ 喵喵喵喵喵 "当你想要颓废的那一刻,想一想当初为什么走到了这里." 以下文字充满负面情绪 ...
- P2580 于是他错误的点名开始了
题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...
- 于是他错误的点名开始了(trie树)
题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...
随机推荐
- 安全篇-AES/RSA加密机制
在服务器与终端设备进行HTTP通讯时,常常会被网络抓包.反编译(Android APK反编译工具)等技术得到HTTP通讯接口地址和参数.为了确保信息的安全,我们采用AES+RSA组合的方式进行接口参数 ...
- java. util. concurrent. atomic
一.原子更新基本类型 AtomicInteger AtomicBoolean AtomicLong 二.原子更新数组 AtomicIntegerArray AtomicLongArray Atomic ...
- 【C#】上机实验一
1.开发一个控制台应用程序,根据提示从键盘获取一个华氏温度,请转换并输出对应的摄氏温度. using System; namespace Project { class Program { publi ...
- springboot 整合 web 项目找不到 jsp 文件
今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...
- jQuery_jQuery的两把利器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C++标准库里自带的数值类型和字符串互相转换函数
需要包含头文件 #include <string> 数值类型转成string类型: string to_string(int val); string to_string(unsigned ...
- css3 transform实现水平和垂直居中
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 封装jquery的ajax
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Linux Ubuntu XShell连接虚拟机问题记录
我们先用ip addr / ifconfig查看虚拟机ip地址,然后到windows下的cmd中ping 一下对应地址 一般是可以ping通的. 然后用Xshell或者其他工具连接虚拟机. 如果连不上 ...
- Mybatis设计模式
mybatis中使用到的设计模式 Mybatis 使用的 9 种设计模式 建造者模式(Configuration) 构造者模式的定义是“将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不 ...