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]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
随机推荐
- iOS 获取蜂窝网络信号强度 包含iPhoneX XS XR XSMASX (最新)
1.虽然各种直接获取信号强度的api都被封杀了.但是还有一个另类的黑魔法可以获取到.那就是遍历UIStatusBar了 网络上有的文章写的会崩溃 比如: - (int)getSignalStrengt ...
- js取一个对象中的另一个对象
最开始的截图 原本是想取到其中的foodName 先是用一个for循环循环了下 for (var i=0;i<res.data.length;i++) { this.goodsList.res. ...
- Zabbix——设置报警阈值
前提条件: 1. Zabbix-server 版本为4.0 2.邮件告警正常使用 3. 阈值改为1分钟进行邮件发送 点击: 找到agent,点击触发器: 设置网络ping包进行检测
- Laravel 开发支付宝支付与提现转账问题小结
由于项目需要,所以需要开发支付宝支付与微信支付,支付部分采用了 yansongda/pay https://packagist.org/packages/yansongda/pay https ...
- Delphi7 GDI+学习
Delphi7自带的绘图有锯齿,所以要学习GDI+ 主要是从这个网站学习 http://www.bianceng.com/Programming/Delphi/201212/34691.htm 相关控 ...
- 从零开始一个http服务器(二)-请求request解析
从零开始一个http服务器 (二) 代码地址 : https://github.com/flamedancer/cserver git checkout step2 解析http request 观察 ...
- char[] 转换string时的自动截断问题
在char[] 转换string时可以直接转换,但当用char[]读取一个二进制文件之后,若char[] 中包含有'\0'时,在转换时会被string检测到并认为字符串末尾,后面内容会被截断,导致转换 ...
- mybatis中的resultMap实际作用
resultMap和resultType在实际的使用上完全可以进行替换,但是resultMap有比resultType更多的一个功能.我们先定义一个简单的resultMap例子 <resultM ...
- [原创]python写的sniffer
import socket s=socket.socket(socket.PF_PACKET,socket.SOCK_RAW,8) while 1: data=s.recv(65535) print ...
- [Real World Haskell翻译]第22章 扩展示例:Web客户端编程
第22章 扩展示例:Web客户端编程 至此,您已经看到了如何与数据库交互,解析一些数据,以及处理错误.现在让我们更进了一步,引入Web客户端库的组合. 在本章,我们将开发一个真正的应用程序:一个播客下 ...