题目分析:

差评,最大生成树裸题。hack数据还卡常。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; struct LCT{
int fa[maxn],lazy[maxn],ch[maxn][],d1[maxn],d2[maxn];
int val[maxn],tot[maxn],num;
stack<int> sta;
void push_up(int now){
val[now] = min(d1[now],min(val[ch[now][]],val[ch[now][]]));
tot[now] = d2[now] + tot[ch[now][]] + tot[ch[now][]];
}
void push_down(int now){
if(lazy[now]){
swap(ch[now][],ch[now][]);
lazy[now]=;
lazy[ch[now][]] ^= ; lazy[ch[now][]] ^= ;
lazy[] = ;
}
}
int is_root(int now){return !(ch[fa[now]][]==now||ch[fa[now]][]==now);}
void r0(int now,int dr){
int ff = fa[now],gf = fa[ff],son = ch[now][dr];
int flag = is_root(ff);
ch[ff][dr^] = son; fa[son] = ff;
ch[now][dr] = ff; fa[ff] = now;
fa[now] = gf;
if(!flag){if(ch[gf][] == ff) ch[gf][] = now; else ch[gf][] = now;}
push_up(ff); push_up(now);
fa[] = ;ch[][] = ch[][] = ;
}
void splay(int now){
int pp = now;
while(!is_root(pp)) sta.push(pp),pp = fa[pp];
sta.push(pp);
while(!sta.empty()) push_down(sta.top()),sta.pop();
while(!is_root(now)){
if(is_root(fa[now])){
if(ch[fa[now]][] == now) r0(now,); else r0(now,);
}else{
int ff = fa[now],gf = fa[ff];
int alpha = ch[gf][]==ff,beta = ch[ff][]==now;
if(alpha^beta) r0(now,beta),r0(now,alpha);
else r0(ff,alpha),r0(now,beta);
}
}
}
void access(int now){
splay(now);ch[now][] = ;push_up(now);
while(fa[now]){
int nxt=fa[now];splay(nxt);ch[nxt][]=now; push_up(nxt); now=nxt;
}
}
void make_root(int now){
access(now);
splay(now);
lazy[now] ^= ;
}
void cut(int u,int v){
make_root(u); access(v); splay(u);
fa[v] = ;
if(ch[u][] == v) ch[u][] = ; else ch[u][] = ;
push_up(u);
}
void link(int u,int v){make_root(u); fa[u] = v;}
int lft(int dt){
splay(dt);
if(lazy[dt]) push_down(dt);
while(ch[dt][]){
dt = ch[dt][];
if(lazy[dt]) push_down(dt);
}
return dt;
}
int find_min(int now){
splay(now);
while(d1[now] != val[now]){
if(val[ch[now][]] == val[now]) now = ch[now][];
else now = ch[now][];
}
return now;
}
}T; int n,m,pre[maxn]; struct edge{int from,to,tmp,len;}edges[maxn]; char str[]; int found(int x){
int rx = x; while(pre[rx] != rx) rx = pre[rx];
while(pre[x]!=rx){int tt = x; pre[x] = rx; x = tt;}
return rx;
} void work(){
T.d1[] = 2e9;T.val[] = 2e9;T.d2[] = ;
for(int i=;i<=n;i++){T.num++;T.d1[i] = 2e9; T.val[i] = 2e9; pre[i] = i;}
for(int i=;i<=m;i++){
scanf("%s",str);
if(str[] == 'f'){
int id;scanf("%d",&id);
scanf("%d%d",&edges[id].from,&edges[id].to);
scanf("%d%d",&edges[id].tmp,&edges[id].len);
edges[id].from++;edges[id].to++;
T.d1[n+id+] = edges[id].tmp; T.d2[n+id+] = edges[id].len;
T.val[n+id+] = edges[id].tmp; T.tot[n+id+] = edges[id].len;
T.make_root(edges[id].from);
T.access(edges[id].to);
int pd = T.lft(edges[id].to);
if(pd == edges[id].from){
if(T.val[edges[id].to] > edges[id].tmp) continue;
int pla = T.find_min(edges[id].to);
T.make_root(pla);T.cut(pla,edges[pla-n-].from);
T.cut(pla,edges[pla-n-].to);
}
T.link(edges[id].from,n+id+);T.link(edges[id].to,n+id+);
pre[found(edges[id].from)] = found(edges[id].to);
}else{
if(str[] == 'm'){
int u,v; scanf("%d%d",&u,&v); v++;u++;
if(found(u) != found(v)){puts("-1");continue;}
T.make_root(u); T.access(v); T.splay(v);
printf("%d\n",T.tot[v]);
}else{
int id,l; scanf("%d%d",&id,&l);
T.make_root(n+id+); T.d2[n+id+] = l;
T.push_up(n+id+);
}
}
}
} int main(){
scanf("%d%d",&n,&m);
work();
return ;
}

UOJ274 [清华集训2016] 温暖会指引我们前行 【LCT】【最大生成树】的更多相关文章

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

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

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

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

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

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

  4. 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT

    题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...

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

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

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

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

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

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

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

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

  9. BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集

    题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...

随机推荐

  1. 【开源】微信小程序、小游戏以及 Web 通用 Canvas 渲染引擎 - Cax

    Cax 小程序.小游戏以及 Web 通用 Canvas 渲染引擎 Github → https://github.com/dntzhang/cax 点我看看 DEMO 小程序 DEMO 正在审核中敬请 ...

  2. sublime插件不能使用,提示plugin_host has exited unexpectedly

    sublime Text3一打开软件就提示plugin_host has exited unexpectedly,插件不能使用 解决方法很简单: 1.首先,ctrl + shift + p  --&g ...

  3. babel-preset-env使用指南

    文章概览 babel-preset-env是非常重要且常用的一个插件预设,掌握它的用法以及实现原理非常有必要. 本文主要内容包括:babel-preset-env是什么.入门实例.如何配置以支持特定版 ...

  4. nodejs图片处理工具gm用法

    在做H5应用中,有时候会涉及到一些图片加工处理的操作,nodejs有一个很好的后台图片处理module,就是这里说的gm.gm有官方文档,但感觉写得太抽象,反而看不懂了.这里把一些常见的用法写下,供大 ...

  5. C#中委托,匿名函数,lamda表达式复习

    一.委托 1.就给类比较,类用class声明,委托用delegate声明. 2.委托要指向一个真正的方法. 3.委托的签名,要和指向的方法一样. //1.声明一个委托 public delegate ...

  6. c++继承实例

    #include <iostream> #include <vector> #include <string> using namespace std; class ...

  7. P66 整环的零元

    R/I=0的零因子是0+I吗? 如果不是,那请问R/I的零因子是什么呢? R/I没有零因子 R/I的零元 是I中的元素定义的等价类 么  a是理想I的元素,自然也是R的元素

  8. Innodb 实现高并发、redo/undo MVCC原理

    一.并发控制   因为并发情况下有可能出现不同线程对同一资源进行变动,所以必须要对并发进行控制以保证数据的同一与安全.   可以参考CPython解释器中的GIL全局解释器锁,所以说python中没有 ...

  9. Redis客户端断开重连功能要点

    Redis客户端: Java基于Jedis开发 C#基于StackExchange开发 C++基于acl开发 首先确保在主从模式下,客户端能分辨主从节点,自动连接正确的客户端,这样只要有一个节点可用, ...

  10. semantic-ui 图标

    semantic-ui提供了很多的图标,基本常用的在官网上面都能找到.要想记住这么多图标是不可能的,但是也是有简便方法记忆. 首先,图标其实和按钮的区别基本没有,要说有的话,也就是基础样式的大小不同吧 ...