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的核心理念是将绘图与数据分离,数据相关的绘图与数据无 ...
随机推荐
- ElasticSearch中碰到的C10K问题
Elasticsearch基于Netty解决C10K问题背后的原理是JAVA NIO中的IO多路复用机制,涉及到三大"组件":SelectableChannel.Selector. ...
- C编程遇到的一些小细节
1 typedef int ElemType --> typedef int ElemType; 此处要加分行(:),要区别 #defin a 20 此处不需要加分号:#define是预 ...
- Java.util.Math类--数学相关的工具类
Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...
- 开发dubbo应用程序(一)入门demo详解
1.简介: 引用自Dubbo官方文档简介: http://dubbo.apache.org/zh-cn/docs/user/dependencies.html 随着互联网的发展,网站应用的规模不断扩大 ...
- 再谈初学者关心的ssh应用方方面面
http://blog.robertelder.org/what-is-ssh/ https://www.ssh.com/ssh/key/ 什么是ssh? ssh是一个在计算机之间实现安全通信的网络协 ...
- gcc 编译过程
gcc 编译过程从 hello.c 到 hello(或 a.out)文件, 必须历经 hello.i. hello.s. hello.o,最后才得到 hello(或a.out)文件,分别对应着预处理. ...
- springboot WebMvcConfigurer配置静态资源和解决跨域
前言 虽然现在都流行前后端分离部署,但有时候还是需要把前端文件跟后端文件一起打包发布,这就涉及到了springboot的静态资源访问的问题.不单只是静态资源打包,比如使用本地某个目录作为文件存储,也可 ...
- MySQL之命令行简单操作MySQL(二)
一:命令行连接数据库 打开终端,运行命令mysql -uroot -p (p后面加密码,可以直接加,也可以回车在下一行输入,为了不暴露密码,回车在下行输入 退出:exit或quit 查看版本信息: s ...
- Computer Networking: A Top Down Approach
目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...
- Vue学习之过滤器和自定义指令小结(三)
过滤器: 过滤是一个数据经过了这个过滤之后出来另一样东西,可以是从中取得你想要的,或者给那个数据添加点什么装饰.过滤器就是过滤用的工具.渲染数据用的!!! 我们需要知道的是: 1.Vue中的过滤器不能 ...