Gym100676 H. Capital City
感觉题目都已经快把正解给说出来了...
strongly connected的两个点的消耗为0,其实就是同一个边双连通分量里面的点消耗为0。
然后缩一下点,再树形DP一下就完了。
第一次写边双,但感觉挺简单的。
#include <bits/stdc++.h>
#define ll long long
using namespace std; const int N = 4e5 + ;
int c[N];
bool bridge[N * ]; struct E {
int v, ne;
ll c;
} e[N * ], tree[N * ];
int head[N], hd[N], cnt1, cnt2;
int dfn[N], low[N], id;
int color[N]; inline void add1(int u, int v, ll c) {
e[++cnt1].v = v; e[cnt1].ne = head[u]; e[cnt1].c = c; head[u] = cnt1;
} inline void add2(int u, int v, ll c) {
tree[++cnt2].v = v; tree[cnt2].ne = hd[u]; tree[cnt2].c = c; hd[u] = cnt2;
} void tarjan(int u, int edge) {
dfn[u] = low[u] = ++id;
for (int i = head[u]; i; i = e[i].ne) {
int v = e[i].v;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] > low[u])
bridge[i] = bridge[i ^ ] = ;
} else if (i != (edge ^ ))
low[u] = min(low[u], dfn[v]);
}
} int dcc; void dfs0(int u) {
c[u] = dcc;
color[dcc] = min(color[dcc], u);
for (int i = head[u]; i; i = e[i].ne) {
int v = e[i].v;
if (c[v] || bridge[i]) continue;
dfs0(v);
}
} ll dp[N][];
int son[N][];
void dfs(int u, int fa) {
dp[u][] = dp[u][] = ;
son[u][] = son[u][] = ;
for (int i = hd[u]; i; i = tree[i].ne) {
int v = tree[i].v;
if (v == fa) continue;
dfs(v, u);
if (dp[v][] + tree[i].c > dp[u][]) {
dp[u][] = dp[u][];
son[u][] = son[u][];
dp[u][] = dp[v][] + tree[i].c;
son[u][] = v;
} else if (dp[v][] + tree[i].c > dp[u][]) {
son[u][] = v;
dp[u][] = dp[v][] + tree[i].c;
}
}
} void dfs2(int u, int fa, ll c) {
if (fa) {
if (son[fa][] != u) {
if (dp[fa][] + c > dp[u][]) {
dp[u][] = dp[u][];
son[u][] = son[u][];
dp[u][] = dp[fa][] + c;
son[u][] = fa;
} else if (dp[fa][] + c > dp[u][]) {
son[u][] = fa;
dp[u][] = dp[fa][] + c;
}
} else {
if (dp[fa][] + c > dp[u][]) {
dp[u][] = dp[u][];
son[u][] = son[u][];
dp[u][] = dp[fa][] + c;
son[u][] = fa;
} else if (dp[fa][] + c > dp[u][]) {
son[u][] = fa;
dp[u][] = dp[fa][] + c;
}
}
}
for (int i = hd[u]; i; i = tree[i].ne) {
int v = tree[i].v;
if (v == fa) continue;
dfs2(v, u, tree[i].c);
}
} int main() {
freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
head[i] = hd[i] = ;
c[i] = ;
low[i] = dfn[i] = ;
color[i] = n + ;
}
memset(bridge, , sizeof(bridge));
cnt1 = ;
for (int u, v, i = ; i <= m; i++) {
ll c;
scanf("%d%d%lld", &u, &v, &c);
add1(u, v, c);
add1(v, u, c);
}
id = ;
for (int i = ; i <= n; i++)
if (!dfn[i])
tarjan(i, );
dcc = ;
for (int i = ; i <= n; i++)
if (!c[i]) {
++dcc;
dfs0(i);
}
cnt2 = ;
for (int i = ; i <= cnt1; i++) {
int u = e[i ^ ].v, v = e[i].v;
if (c[u] == c[v]) continue;
add2(c[u], c[v], e[i].c);
}
memset(dp, , sizeof(dp));
dfs(, );
dfs2(, , );
int ans1 = ; ll ans2 = 1e18;
for (int i = ; i <= dcc; i++) {
if (dp[i][] < ans2) ans2 = dp[i][], ans1 = color[i];
else if (dp[i][] == ans2 && ans1 > color[i]) ans1 = color[i];
//printf("%lld\n", dp[i][0]);
}
printf("%d %lld\n", ans1, ans2);
}
return ;
}
Gym100676 H. Capital City的更多相关文章
- ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量
题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...
- Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)
https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- CodeForcesGym 100676H Capital City
H. Capital City Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForcesG ...
- 图论trainning-part-1 H. Qin Shi Huang's National Road System
H. Qin Shi Huang's National Road System Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO f ...
- Topcoder SRM590 Fox And City
Problem Statement There is a country with n cities, numbered 0 through n-1. City 0 is the capit ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- cf #365b 巧妙的统计
Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- R----ggplot2包介绍学习
分析数据要做的第一件事情,就是观察它.对于每个变量,哪些值是最常见的?值域是大是小?是否有异常观测? ggplot2图形之基本语法: ggplot2的核心理念是将绘图与数据分离,数据相关的绘图与数据无 ...
随机推荐
- Linux常用命令wc
wc名字来源: wc -- word, line, character, and byte count The wc utility displays the number of lines, wor ...
- nginx 反向代理Jenkins
进入nginx 配置文件 cd /root/nginx/conf 找到nginx.conf 修改server块内容: server { listen 80; ...
- 第一章 Maven 安装配置
Maven基于(POM)项目对象模型,通过一小段描述信息来管理项目的构建.文档.和报告的项目管理软件,类似于php 的管理构建工具composer. 有关详细的Maven学习,可以参考学习https: ...
- 详细的Hadoop的入门教程-伪分布模式Pseudo-Distributed Operation
一. 伪分布模式Pseudo-Distributed Operation 这里关于VM虚拟机的安装就不再介绍了,详细请看<VMware虚拟机的三种网络管理模式>一章介绍.这章只介绍hado ...
- Java自学-I/O File类
Java 的File类,以及常用方法 文件和文件夹都是用File代表 步骤 1 : 创建一个文件对象 使用绝对路径或者相对路径创建File对象 package file; import java.io ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- 工具sublime安装
默认安装后是英文版 view-show console 安装packagecontrol https://packagecontrol.io/installation ctrl+`打开控制台,输入代码 ...
- Node.js 中 exports 和 module.exports 的区别
每一个模块中都有一个 module 对象, module 对象中有一个 exports 对象 我们可以把需要导出的成员都放到 module.exports 这个接口对象中,也就是 module.exp ...
- YUV详解
YUV格式解析2 又确认了一下H264的视频格式——H264支持4:2:0的连续或隔行视频的编码和解码 YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL).YUV主要用 ...
- Mysql连接数过多、Mysql连接错误过多的问题处理
在使用Mysql的过程中,你总是会遇到这样那样的问题,每次去网上查找也相对比较麻烦,所以在此整理一下(以linux ubantu16 系统为例). ========================== ...