hdu6041
hdu6041
题意
给出一个仙人掌。
如果一个无向连通图的任意一条边最多属于一个简单环,我们就称之为仙人掌。所谓简单环即不经过重复的结点的环。
求前 \(K\) 小生成树 。
分析
仙人掌中每个环中我们最多可以删掉一条边,题目就变成了有 \(M\) 个数组,每次从每个数组中分别取一个数字并求和,前 \(K\) 大的和。
首先用 \(Tarjan\) 算法找环,然后使用优先队列不断合并两个数组( 多路归并问题,见白书 P189 )。
code
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1e5 + 10;
const int M = 2e3;
int n, m, K;
struct Edge {
int to, w, next;
}e[M << 1];
int cnt, head[M << 1];
void addedge(int u, int v, int w) {
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}
int a[MAXN], b[MAXN], c[MAXN];
int dfn[M], sz;
stack<int> sta;
struct Item {
int s;
int x;
Item(int s, int x):s(s), x(x) {}
bool operator<(const Item& other) const {
return s < other.s;
}
};
void cal() {
priority_queue<Item> q;
for(int i = 1; i <= b[0]; i++) {
q.push(Item(a[1] + b[i], 2));
}
c[0] = 0;
while(c[0] < K && !q.empty()) {
Item it = q.top(); q.pop();
c[++c[0]] = it.s;
if(it.x <= a[0]) q.push(Item(it.s - a[it.x - 1] + a[it.x], it.x + 1));
}
a[0] = c[0];
for(int i = 1; i <= c[0]; i++) {
a[i] = c[i];
}
}
int tarjan(int fa, int u) {
dfn[u] = ++sz;
int lowu = sz;
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if(!dfn[v]) {
sta.push(i);
int lowv = tarjan(u, v);
if(lowu <= lowv) {
b[0] = 0;
while(1) {
int j = sta.top(); sta.pop();
b[++b[0]] = e[j].w;
if(j == i) break;
}
if(b[0] > 1) cal();
} else lowu = lowv;
} else if(v != fa && lowu > dfn[v]) {
sta.push(i);
lowu = dfn[v];
}
}
return lowu;
}
int main() {
int kase = 1;
while(~scanf("%d%d", &n, &m)) {
while(!sta.empty()) sta.pop();
cnt = 0;
sz = 0;
memset(dfn, 0, sizeof dfn);
memset(head, -1, sizeof head);
a[0] = 1; a[1] = 0;
int s = 0;
for(int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
s += w;
addedge(u, v, w);
addedge(v, u, w);
}
scanf("%d", &K);
tarjan(-1, 1);
unsigned ans = 0;
for(int i = 1; i <= a[0]; i++) {
ans = ans + (unsigned)i * (s - a[i]);
}
if(a[0] == 0) ans = s;
printf("Case #%d: %u\n", kase++, ans);
}
return 0;
}
hdu6041的更多相关文章
随机推荐
- 【CF edu 30 A. Chores】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 微信小程序使用Socket
首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...
- innodb log file与binlog的区别在哪里?
Q: innodb log file与binlog的区别在哪里?有人说1.mysql的innodb引擎实际上是包装了inno base存储引擎.而innodb log file是由 inno base ...
- Step-By-Step: Setting up Active Directory in Windows Server 2016
There are interesting new features now made available in Windows Server 2016 such as time based grou ...
- jw player学习笔记
一.是否支持IE7/8 本地离线播放不支持IE7/8,部署在服务器上时可以. 本地播放报错示意图 二.如何去Logo 1.网页版--HTML5---破解 桌面浏览器看到的效果: jwplayer(&q ...
- 说明exit()函数作用的程序
// algo1-4.cpp 说明exit()函数作用的程序 #include"c1.h" int a(int i) { if(i==1) { printf("退出程序的 ...
- 解析 Array.prototype.slice.call(arguments,0)
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 微信小程序基础知识
一.基本目录结构 app.js 定义app入口 app.json 定义页面配置 index.js 页面中的事件和监听 index.wxml 定义布局文件 1.app.json配置基本信息 { “pag ...
- 「6月雅礼集训 2017 Day1」看无可看
[题目大意] 给出n个数,a[1]...a[n],称作集合S,求
- 关于dlib人脸对比,人脸识别
人脸检测 人脸特征点提取 人脸对比,等于两张人脸对比,识别 封装的所有识别函数,直接看下面调用就好了. # coding:utf-8 ''' 本本次封装,我主要是做两张人脸对比. 就只人脸识别部分,简 ...