感觉题目都已经快把正解给说出来了...
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的更多相关文章

  1. ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量

    题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...

  2. Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)

    https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...

  3. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  4. CodeForcesGym 100676H Capital City

    H. Capital City Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForcesG ...

  5. 图论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 ...

  6. Topcoder SRM590 Fox And City

    Problem Statement      There is a country with n cities, numbered 0 through n-1. City 0 is the capit ...

  7. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  8. cf #365b 巧妙的统计

     Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. R----ggplot2包介绍学习

    分析数据要做的第一件事情,就是观察它.对于每个变量,哪些值是最常见的?值域是大是小?是否有异常观测? ggplot2图形之基本语法: ggplot2的核心理念是将绘图与数据分离,数据相关的绘图与数据无 ...

随机推荐

  1. Apache信息头

    package org.apache.http; public final class HttpHeaders { public static final String ACCEPT = " ...

  2. Golang检测Linux服务器端口占用

    代码实现 func CheckPort(port int) error { checkStatement := fmt.Sprintf(`netstat -anp | grep -q %d ; ech ...

  3. Linux设置SSH隧道连接

    因为安全考虑,服务器防火墙对某些端口进行了限制,原先通过客户端工具可以连接的端口,现在不能连接了,需要通过设置SSH隧道才可以,记录如下.

  4. ServerSocketChannel简述

    一.前言 前篇文章中了解了SocketChannel:提供了连接到套接字通道,从某种层面而言,NIO中提供了类似于java.net包中对于网络操作的api的功能.既然已经有连接到Socket套接字的通 ...

  5. SUSE12SP3-Samba配置

    简介 samba官网:https://www.samba.org/ 维基百科: https://zh.wikipedia.org/wiki/Samba Samba,是种用来让UNIX系列的操作系统与微 ...

  6. 经实验验证,修正对using namespace std的认识

    备注①:name:符号.指的实体包括:变量.函数.类 备注②:认为全局命名空间也是一个包,在此称作 ROOT:: 或 global:: (这样就有了两个特别的包:一个是全局包,一个是std包.但对于编 ...

  7. 使用JavaConfig配置SpringMVC

    目录结构 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...

  8. mybatis关联映射一对一

    在项目开发中,会存在一对一的关系,比如一个人只有一个身份证,一个身份证只能给一个人使用,这就是一对一关系.一对一关系使用主外键关联. table.sql,在数据库中创建如下两个表并插入数据 CREAT ...

  9. this、对象原型

    this和对象原型 第一章 关于this 1.1 为什么要用this this 提供了一种更优雅的方式来隐式"传递"一个对象引用,因此可以将 API 设计 得更加简洁并且易于复用. ...

  10. powershell 远程下载并执行

    远程下载文件到本地并执行cmd.exe /c powershell.exe -ExecutionPolicy bypass -noprofile -windowstyle hidden (new-ob ...