题目链接: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. BufPay.com 个人收款接口 接入步骤

    作为独立开发者产品需要收款是非常麻烦的,注册公司维护成本太高,市面上各种收款工具要么手续费太高,要么到账很慢,体验很不好. 看到 「BufPay.com 个人收款」 这个收款工具,挺有意思的.原理是监 ...

  2. netstat命令的用法

    netstat用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.利用netstat指令可让你得知整个Linux系统的网络情况.参数:-a或–all 显示 ...

  3. linux 学习第九天

    一.磁盘 (FHS:Filesystem Hierarchy Standard(文件系统层次化标准)的缩写) 1.常用目录 /var  主要存放经常变化的文件,如日志 /usr/local  用户自行 ...

  4. Kali Basic Configuration

    1:Kali Version root@kali-node01:~# cat /etc/os-release PRETTY_NAME="Kali GNU/Linux Rolling" ...

  5. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  6. 从零开始一个http服务器(一)-开始

    从零开始一个http服务器 (一) 代码地址 : https://github.com/flamedancer/cserver git checkout step1 一个简单的socket serve ...

  7. linux进程篇 (三) 进程间的通信1 管道通信

    通信方式分4大类: 管道通信:无名管道 有名管道 信号通信:发送 接收 和 处理 IPC通信:共享内存 消息队列 信号灯 socke 网络通信 用户空间 进程A <----无法通信----> ...

  8. P2P通讯

    转载: http://www.cnblogs.com/pannengzhi/p/4800526.html http://blog.csdn.net/lee353086/article/details/ ...

  9. linux 查看内置命令

    使用: man shell builtins 查找结果如下:

  10. ASCII、Unicode、UTF-8编码关系

    由于计算机是美国人发明的,因此,最早只有127个字符被编码到计算机里,也就是大小写英文字母.数字和一些符号,这个编码表被称为ASCII编码,比如大写字母A的编码是65,小写字母z的编码是122.但是要 ...