题目链接: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+最优生成树+并查集的更多相关文章

  1. bzoj 4736: 温暖会指引我们前行 (LCT 维护最大生成树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4736 题面: 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出 ...

  2. bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct

    [清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...

  3. UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]

    #274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...

  4. 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT

    [UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...

  5. [清华集训2016]温暖会指引我们前行——LCT+最大生成树

    题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...

  6. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...

  7. 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)

    [BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...

  8. UOJ_274_[清华集训2016]温暖会指引我们前行_LCT

    UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...

  9. [UOJ#274][清华集训2016]温暖会指引我们前行

    [UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...

随机推荐

  1. Filebeat使用模块收集日志

    1.先决条件 在运行Filebeat模块之前: 安装并配置Elastic stack 完成Filebeat的安装 检查Elasticsearch和Kibana是否正在运行,以及Elasticsearc ...

  2. 07.安装及使用gitlub

    博客为日常工作学习积累总结: 1.安装gitlub sudo yum install -y curl policycoreutils-python openssh-server openssh-cli ...

  3. 利用clear清除浮动的一些问题

    下面这段代码是用来清除浮动带来的高度塌陷问题 .clearfix:before { content: "."; display: block; height: 0; clear: ...

  4. javascript 时间倒计时效果

    <div id="divdown1"></div> <script language="javascript" type=&quo ...

  5. Java 面试题 百度/参考的答案

    "a=b"和"a.equals(b)"有什么区别? 如果 a 和 b 都是对象,则 a==b 是比较两个对象的引用,只有当 a 和 b 指向的是堆中的同一个对象 ...

  6. IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?

    IE8  如何 不显示 选项 卡,直接在任务显示 各个页面? 在  工具->Internet 选项(o) ->常规--选项卡-设置->把第一个勾去掉  “启用选项卡浏览(需要重新启动 ...

  7. Git安装配置(Windows)

    下载Git并安装 下载地址:https://git-scm.com/ 安装一般默认即可 配置用户信息 配置之前最好已经有了Github的账号,如果没有可以先去注册一个 安装后打开Git Bash gi ...

  8. C#中如何使用JS脚本

    C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...

  9. Lingo基本操作

    目录 Lingo基本操作 前言 一.Lingo基本运算符 1.1 算术运算符 1.2 逻辑运算符 1.3 关系运算符 二.函数 2.1 标准数学函数 2.2 集循环函数 三.待更新 Lingo基本操作 ...

  10. 前端模拟API数据的两种方式

    第一种方法:使用 mock-api 1.创建一个项目 2.创建一个资源 3.拖动创建记录 点击data查看记录 4.如何访问API的数据 第一种方式:在终端中通过 curl + 地址,如图: 第二种方 ...