2017 多校1 I Curse Myself
2017 多校2 I Curse Myself(第k小生成树)
题目: 给一张带权无向连通图,该图的任意一条边最多只会经过一个简单环,定义\(V(k)为第k小生成树的权值和\),求出\(\sum_{k=1}^{K}k \cdot V(k) mod 2^{32}\)
思路: 比赛的时候看了一眼,没有看清楚是仙人掌那句话,觉得好难啊
看完题解之后觉得就算看清了还是过不了嘛。
直接上题解
由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉。所以问题就变为有 M 个集合,每个集合里面都有一堆数字,要从每个集合中选择一个恰好一个数加起来。求所有的这样的和中,前 K 大的是哪些。这就是一个经典问题了。
对所有集合两个两个进行合并,设当前合并的集合是 A 和BB,合并的过程中用堆来求出当前 \(A_{i} + B_{j}\)
这样的复杂度看起来是\(\mathcal{O}(MK \log K)\),但如果合并的时候保证堆内的元素个数是新集合里的元素个数,设每个集合的大小分别为 \(m_{0}, m_{1}, \cdots, m_{M-1}\)
,则复杂度为 \(\mathcal{O}(\sum{K \log{m_{i}}}) = \mathcal{O}(K \log{\prod{m_i}})\)。当 \(m_{i}\)
都相等时取得最大值 \(\mathcal{O}\left(MK \log{\frac{\sum{m_i}}{M}}\right)\),所以实际复杂度为 \(\mathcal{O}(MK)\)。
就照着题解敲了一遍,该优化的地方优化了,
找环那里用时间戳的方法一直没写对,所以换成直接用树再加边的方法去暴力找环了。
合并有序表的问题 刘汝佳的书上有讲
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
using namespace std;
const LL mod = (1LL<<32);
const int N = 1200;
const int M = 4 * N;
int n, m, K;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48,c = getchar();
return x;
}
struct edge{
int v,w,nxt;
edge(){};
}e[M];
struct Edge{
int u, v, w;
Edge(int u,int v,int w):u(u),v(v),w(w){};
Edge(){};
};
vector<Edge> other;
int head[N],EN;
int pa[N];
int Find(int x){
return pa[x] == x?x:pa[x] = Find(pa[x]);
}
int A[100010],B[100010],ca,cb;
void init(){
ca = cb = EN = 0;
memset(head,-1,sizeof(head));
other.clear();
for(int i = 1;i <= n;i++) pa[i] = i;
}
void add(int u,int v,int w){
e[EN].v = v,e[EN].w = w,e[EN].nxt = head[u];
head[u] = EN++;
}
bool dfs(int u,int f,int ed){
if(u == ed) return true;
for(int i = head[u];~i;i = e[i].nxt){
if(i == f) continue;
if(dfs(e[i].v,i ^ 1,ed)){
B[cb++] = e[i].w;
return true;
}
}
return false;
}
bool cmp(int x,int y){
return x > y;
}
void Merge(){
if(ca > cb) swap(ca,cb),swap(A,B);
priority_queue<P> q;
for(int i = 0;i < ca;i++) q.push(P(A[i]+B[0],0));
int siz = min(K, ca * cb);
ca = 0;
for(int i = 0;i < siz;i++){
P cur = q.top();q.pop();
A[ca++] = cur.first;
if(cur.second + 1 < cb) q.push(P(cur.first - B[cur.second] + B[cur.second+1],cur.second + 1));
}
}
int main(){
int cas = 1, u, v, w;
while(scanf("%d%d",&n,&m)==2){
init();
int total = 0;
for(int i = 0;i < m;i++){
u = read(),v = read(),w = read();
if(Find(u) != Find(v)){
add(u, v, w);
add(v, u, w);
pa[Find(u)] = Find(v);
}else {
other.push_back(Edge(u,v,w));
}
total += w;
}
K = read();
int first = 1;
for(auto E:other){
dfs(E.u,-1,E.v);
B[cb++] = E.w;
sort(B, B + cb,cmp);
if(first) first = 0,swap(A,B),swap(ca,cb);
else Merge();
cb = 0;
}
LL ans = 0;
for(int i = 0;i < ca;i++) {
ans = (ans + 1LL * (i + 1) * (total - A[i]))%mod;
}
if(!ca) ans = total;
printf("Case #%d: %lld\n",cas++,ans);
}
return 0;
}
2017 多校1 I Curse Myself的更多相关文章
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- 2017 多校5 Rikka with String
2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...
- 2017 多校4 Wavel Sequence
2017 多校4 Wavel Sequence 题意: Formally, he defines a sequence \(a_1,a_2,...,a_n\) as ''wavel'' if and ...
- 2017 多校4 Security Check
2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...
- 2017 多校3 hdu 6061 RXD and functions
2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...
- 2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...
- hdu6136[模拟+优先队列] 2017多校8
有点麻烦.. /*hdu6136[模拟+优先队列] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long ...
- hdu6134[莫比乌斯反演] 2017多校8
/*hdu6134[莫比乌斯反演] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long long LL ...
- hdu6098[RMQ+筛法] 2017多校6
/*hdu6098[RMQ+筛法] 2017多校6*/ #include <bits/stdc++.h> using namespace std; ][], len[], a[]; voi ...
随机推荐
- springboot整合mybatis笔记
1首先创建一个springboot项目 创建项目的文件结构以及jdk的版本 选择项目所需要的依赖 之后点击finish,完成创建 2以下是文件结构 看一下啊pom.xml; <?xml vers ...
- IDEA怎么生成UML类图
说之前先说一下Diagram这个单词,意思是图表; 示意图; 图解; [数] 线图的意思. 打开设置 File->Setting或windows下按Ctrl+Alt+S 在搜索框中输入Diagr ...
- ECSHOP快递物流单号查询插件
本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...
- CentOS7 配置环境
1.安装CentOS 配置环境 (1)虚拟机中安装CentOS,进入系统使用yum命令不止正常执行…… 原因: 需要设置网卡激活 解决方法: vi /etc/sysconfig/network-scr ...
- Spark Streaming 交互 Kafka的两种方式
一.Spark Streaming连Kafka(重点) 方式一:Receiver方式连:走磁盘 使用High Level API(高阶API)实现Offset自动管理,灵活性差,处理数据时,如果某一时 ...
- python基础之流程控制、数字和字符串处理
流程控制 条件判断 if单分支:当一个“条件”成立时执行相应的操作. 语法结构: if 条件: command 流程图: 示例:如果3大于2,那么输出字符串"very good" ...
- 如何使用Python脚本
来自官方文档 一.写 python 脚本: import sys import datetime for line in sys.stdin: line = line.strip() userid, ...
- 通过aop添加日志管理
1.使用spring 的 aop 技术切到自定义注解上,所以先创建一个自定义注解类 import java.lang.annotation.*; @Target(ElementType.METHOD) ...
- Java线程和多线程(六)——守护线程
当我们在Java中创建线程的时候,这个线程在默认的情况下是一个用户线程,并且,如果这个线程在运行,那么JVM就不会终结这个应用.和用户线程不同,当一个线程被标记为守护线程的时候,JVM在用户线程结束的 ...
- adnroid 打包问题 :compileReleaseJavaWithJavac
今天打包的时候,由于着急.改了些本地的变化就assembleRelease. 然后就报错: compileReleaseJavaWithJavac 后来网上乱找,.... 之后我想到先跑一下,果然是因 ...