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的更多相关文章

随机推荐

  1. AtCoder Grand Contest 025 Problem D - Choosing Points

    题目大意:输入$n,d1,d2$,你要找到$n^2$个整点 x, y 满足$0 \leqslant x, y<2n$.并且找到的任意两个点距离,既不是$\sqrt{d1}$,也不是 $\sqrt ...

  2. Link Cat Tree (连喵树) 学习笔记

    Link Cat Tree 一.感性定义 所谓连喵树,即一种对森林支持修改,查询,连边,删边等操作的数据结构(姑且算她是吧).她用一颗颗互相连接的辅助树维护原森林的信息,辅助树相互连接的边叫虚边,辅助 ...

  3. oracle设置自动清理归档日志脚本

    设置定时自动清理归档日志脚本 root用户下 [root@localhost ~]# mkdir /nstg [root@localhost ~]# cd /nstg/ [root@localhost ...

  4. JavaScript 被忽视的细节

    语句/表达式 换个角度理解语句(statemaents)和表达式(expressions):表达式不会改变程序的运行状态,而语句会.还有一种叫做表达式语句,可以理解为表达式和语句的交集,如({a:1} ...

  5. SpringMVC学习 -- IDEA 创建 HelloWorld

    SpringMVC 概述: Spring 为展现层提供的基于 MVC 实际理念的优秀 Web 框架 , 是目前最主流的 MVC 框架之一. 自 Spring3.0 发布及 2013 年 Struts ...

  6. 51Nod 1256 求乘法逆元--扩展欧几里德

    #include<stdio.h> int exgcd(int a,int b,int &x,int &y) { ) { x=; y=; return a; } int r ...

  7. bzoj1577 [Usaco2009 Feb]庙会捷运Fair Shuttle

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1577 [题解] 我们把每坨奶牛按s排个序. 对于每坨奶牛,如果车上有空位置就塞. 否则,看下 ...

  8. codechef September Challenge 2017 Sereja and Commands

    ———————————————————————————— 这道题维护一下原序列的差分以及操作的差分就可以了 记得倒着差分操作 因为题目保证操作2的l r 小与当前位置 #include<cstd ...

  9. bzoj 1901 线段树套平衡树+二分答案查询

    我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...

  10. 如何使用SDK在Ubuntu设备(包括仿真器和桌面)上运用应用程序

    简介 有三种运行通过SDK创建的应用程序的方式:在桌面上,在联网的Ubuntu设备上,以及在仿真器中.这些方式为互补性方式,因为各有优缺点.您首先将了解如何管理SDK的设备类型,以及哪一个类型用于测试 ...