BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集
题目链接:http://uoj.ac/problem/274
题意概述:
没什么好概述的......概述了题意就知道怎么做了......我懒嘛
分析:
就是用lct维护最大生成树。
然后如果去UOJ上面交发现如果不用并查集判断连通性就要T?!
然后我就默默改了并查集。。。(hash表并查集输入输出占据了一半的行数?!)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
#define inf 1e9+5
using namespace std;
const int maxm=; int N,M;
struct edge{ int u,v; }E[maxm];
struct union_find{
static const int maxn=;
int pa[maxn],stk[maxn],top;
void initial(int n){ for(int i=;i<=n;i++) pa[i]=i; }
int find(int x){
while(x!=pa[x]) stk[++top]=x,x=pa[x];
while(top) pa[stk[top--]]=x;
return x;
}
bool judge(int x,int y){ return find(x)==find(y); }
void merge(int x,int y){ pa[find(x)]=find(y); }
}uf;
struct Link_Cut_Tree{
static const int maxn=;
int ch[maxn][],fa[maxn],sum[maxn],w[maxn],mn[maxn],t[maxn];
bool rev[maxn];
Link_Cut_Tree(){ mn[]=inf; }
void link(int x,int d,int y){ ch[x][d]=y,fa[y]=x; }
bool isrt(int x){ return ch[fa[x]][]!=x&&ch[fa[x]][]!=x; }
void pushup(int now){
sum[now]=sum[ch[now][]]+sum[ch[now][]]+w[now];
mn[now]=min(t[now],min(mn[ch[now][]],mn[ch[now][]]));
}
void pushdown(int now){
if(!rev[now]) return;
rev[ch[now][]]^=,swap(ch[ch[now][]][],ch[ch[now][]][]);
rev[ch[now][]]^=,swap(ch[ch[now][]][],ch[ch[now][]][]);
rev[now]=;
}
void rot(int x){
int y=fa[x],z=fa[y];
pushdown(y); pushdown(x);
int d=x==ch[y][];
if(!isrt(y)) link(z,ch[z][]==y,x); fa[x]=z;
link(y,d^,ch[x][d]);
link(x,d,y);
pushup(y); pushup(x);
}
void splay(int x){
pushdown(x);
while(!isrt(x)){
int y=fa[x],z=fa[y];
if(!isrt(y)) rot((ch[z][]==y)==(ch[y][]==x)?y:x);
rot(x);
}
}
void access(int x){
int y=;
while(x){
splay(x); ch[x][]=y; pushup(x);
y=x,x=fa[x];
}
}
void mroot(int x){
access(x); splay(x);
rev[x]^=,swap(ch[x][],ch[x][]);
}
void Link(int x,int y,int id,int w1,int w2){
mroot(x); mroot(y);
w[N+id]=sum[N+id]=w1,t[N+id]=mn[N+id]=w2;
fa[x]=fa[y]=N+id;
}
void Cut(int id,int x,int y){
mroot(N+id); access(N+id);
splay(x); fa[x]=;
splay(y); fa[y]=;
}
int query1(int x,int y){
mroot(y); access(x); splay(x);
return mn[x];
}
int query2(int x,int y){
mroot(y); access(x); splay(x);
return sum[x];
}
}lct;
struct Hash{
static const int maxn=;
static const int mo=;
int np,first[mo],next[maxn],id[maxn],val[maxn];
void ins(int p,int t){
int i=t%mo;
next[++np]=first[i],first[i]=np;
id[np]=p,val[np]=t;
}
int find(int t){
int i=t%mo;
for(int p=first[i];p;p=next[p])
if(val[p]==t) return id[p];
return -;
}
}hash; void _scanf(int &x)
{
x=;
char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') x=x*+c-'',c=getchar();
}
void _scanf(char *s)
{
int cnt=;
char c=getchar();
while(!isalpha(c)) c=getchar();
while(isalpha(c)) s[cnt++]=c,c=getchar();
s[cnt]='\0';
}
int out_cnt,out[];
void _printf(int x)
{
if(x<) putchar('-'),x=-x;
out[++out_cnt]=x%,x/=;
while(x) out[++out_cnt]=x%,x/=;
while(out_cnt) putchar(''+out[out_cnt--]);
putchar('\n');
}
void work()
{
_scanf(N);_scanf(M);
uf.initial(N);
for(int i=;i<=N;i++) lct.t[i]=lct.mn[i]=inf;
char op[];
int id,u,v,t,l,x;
for(int i=;i<=M;i++){
_scanf(op);
if(op[]=='f'){
_scanf(id);_scanf(u);_scanf(v);_scanf(t);_scanf(l);
u++,v++,id++;
E[id]=(edge){u,v};
hash.ins(id,t);
if(uf.judge(u,v)){
x=lct.query1(u,v);
if(x<t){
x=hash.find(x);
lct.Cut(x,E[x].u,E[x].v);
lct.Link(u,v,id,l,t);
}
}
else{
lct.Link(u,v,id,l,t);
uf.merge(u,v);
}
}
else if(op[]=='m'){
_scanf(u);_scanf(v); u++,v++;
_printf(uf.judge(u,v)?lct.query2(u,v):-);
}
else if(op[]=='c'){
_scanf(id);_scanf(l); id++;
lct.splay(N+id);
lct.w[N+id]=l;
}
}
}
int main()
{
work();
return ;
}
BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集的更多相关文章
- bzoj 4736: 温暖会指引我们前行 (LCT 维护最大生成树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4736 题面: 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树
Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...
- 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
[BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
随机推荐
- NodeJ node.js Jquery Ajax 跨域请求
Jquery + Ajax 跨域请求 说白了就是前台请求ajax数据(JSON)但是请求的数据不在本地的绝对路径下,接口数据 是没有这个安全性的我对外公开的接口数据,只要你找到接口你就可以使用里面的数 ...
- 多线程异步非阻塞之CompletionService
引自:https://www.cnblogs.com/swiftma/p/6691235.html 上节,我们提到,在异步任务程序中,一种常见的场景是,主线程提交多个异步任务,然后希望有任务完成就处理 ...
- 『Python基础-7』for循环 & while循环
『Python基础-7』for循环 & while循环 目录: 循环语句 for循环 while循环 循环的控制语句: break,continue,pass for...else 和 whi ...
- Python学习手册之内部方法、操作符重载和对象生命周期
在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...
- Leecode刷题之旅-C语言/python-217存在重复元素
/* * @lc app=leetcode.cn id=217 lang=c * * [217] 存在重复元素 * * https://leetcode-cn.com/problems/contain ...
- JZ2440开发板:修改ARM芯片时钟(学习笔记)
想要修改ARM芯片的时钟,需要去查询芯片手册和原理图,获取相关的信息(见下方图片) 首先来看时钟的结构图 根据结构图可以看出,时钟源有两种选择:1. XTIpll和XTOpll所连接的晶振 2. EX ...
- Java垃圾回收机制概述
总览 本文会介绍垃圾回收的以下几个方面. 为什么要垃圾回收 在哪里回收 哪些对象需要回收 怎么回收 HotSpotJVM中有哪些具体的回收器可以直接用. 在开始讲垃圾回收之前,先通过一张图快速回忆一下 ...
- lvs健康检查脚本第三版
如下是学习完马哥视频lvs后改写的健康检查脚本第三版.利用工作之余三四个小时时间才把整个逻辑搞清楚,有时候自己都有点蒙圈,尤其是在写到while循环的时候.总的来说非常感谢马哥的慷慨解囊!脚本原稿及思 ...
- 回顾爬虫的时候的一些小TIPS
1 json.dumps的时候默认会用ascii 所以在写入文件的时候会需要用到的指令变为json.dumps(a,ensuer_ascii=False),这样将禁止转换为ascii 然后再写入的时候 ...
- 20145207 myeclipse测试
实验博客