题目链接: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. Redis支持的五种数据类型

    redis支持的五种数据类型: 1.string(字符串) 2.hash(哈希) Redis hash 是一个键值(key=>value)对集合. Redis hash是一个string类型的f ...

  2. node的安装和配置

    一 . 直接安装node 1. http://nodejs.cn/download/ 根据自己的电脑选择适合的安装包 2.安装 , 无脑下一步 , 可以选择安装路径 , 但是一定要记住 . 3.命令行 ...

  3. tornado用户指引(三)------tornado协程使用和原理(二)

    Python3.5  async和await async和await是python3.5引入的2个新的关键字(用这两个关键字编写的函数也称之为"原生协程"). 从tornado4. ...

  4. 01-http简介-四层 七层 三次握手

    HTTP简介.请求方法与响应状态码 接下来想系统的回顾一下TCP/IP协议族的相关东西,当然这些东西大部分是在大学的时候学过的,但是那句话,基础的东西还是要不时的回顾回顾的.接下来的几篇博客都是关于T ...

  5. YII2.0 后台手动添加用户功能

    后台添加管理员用户使用SignupForm类实现 步骤一.复制一份前台frontend/models/SignupForm.php 到后台模型文件夹中 backend/models/SignupFor ...

  6. 解决gradle下载慢的问题(转)

    问题一:新建项目下载gradle慢的问题 解决方法: 打开用户主目录 linux平台/home/用户名/.gradle windows平台c:\Users\用户名\.gradle macos平台/Us ...

  7. socketpair通信

    1.线程间通信(参考安卓源码InputTransport.cpp) #include <pthread.h> #include <sys/types.h> /* See NOT ...

  8. Manjaro Linux KDE个人的一些安装配置

    安装manjaro kde linux的个人步骤 1 换源 1.1 自动寻找最快的源 sudo pacman-mirrors -i -c China -m rank 1.2 修改源文件 sudo ge ...

  9. Learning notes | Data Analysis: 1.2 data wrangling

    | Data Wrangling | # Sort all the data into one file files = ['BeijingPM20100101_20151231.csv','Chen ...

  10. 树莓派安装samba

    (1) sudo apt-get install samba samba-common (2)mkdir /home/lin/share #(文件路径自己添加) (3)sudo chmod 777 / ...