离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM)

-----------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
 
using namespace std;
 
const int maxn = 100009;
const int maxm = 1000009;
 
inline int read() {
char c = getchar();
int ret = 0;
for(; !isdigit(c); c = getchar());
for(; isdigit(c); c = getchar())
ret = ret * 10 + c - '0';
return ret;
}
 
bool F[maxm];
int N, M, Q;
int ans[maxn], r[maxm];
 
struct Event {
int typ, u, v;
Event(int _typ = 0, int _u = 0, int _v = 0) :
typ(_typ), u(_u), v(_v) {
}
} q[maxn];
 
struct EDGE {
int u, v, w;
EDGE(int _u = 0, int _v = 0, int _w = 0) : u(_u), v(_v), w(_w) {
}
bool operator < (const EDGE &o) const {
return u < o.u || (u == o.u && v < o.v);
}
} e[maxm];
 
struct Node* Null;
 
struct Node {
Node *ch[2], *fa, *par, *c;
int v, Id;
bool rev, isRoot;
inline void Init(int _v) {
ch[0] = ch[1] = fa = par = Null;
v = _v;
c = this;
isRoot = true;
rev = false;
}
inline void setc(Node* t, int v) {
ch[v] = t;
t->fa = this;
}
inline int d() {
return this == fa->ch[1];
}
inline void Rev() {
rev ^= 1;
}
inline void setRoot() {
isRoot = true;
par = fa;
fa = Null;
}
inline void pushDown() {
if(rev) {
swap(ch[0], ch[1]);
ch[0]->Rev();
ch[1]->Rev();
rev = false;
}
}
inline void upd() {
c = ch[0]->c->v > ch[1]->c->v ? ch[0]->c : ch[1]->c;
if(v > c->v)
c = this;
}
} pool[maxn * 3], *Stack[maxn * 3], *V[maxn], *pt;
 
void Init_LCT() {
pt = pool;
(Null = pt++)->Init(0);
}
 
Node* newNode(int v, int Id = 0) {
pt->Init(v);
pt->Id = Id;
return pt++;
}
 
void Rotate(Node* t) {
Node* p = t->fa;
p->pushDown();
t->pushDown();
int d = t->d();
p->fa->setc(t, p->d());
p->setc(t->ch[d ^ 1], d);
t->setc(p, d ^ 1);
p->upd();
if(p->isRoot) {
p->isRoot = false;
t->isRoot = true;
t->par = p->par;
}
}
 
void Splay(Node* t, Node* f = Null) {
int n = 0;
for(Node* o = t; o != Null; o = o->fa) {
Stack[n++] = o;
}
while(n--)
Stack[n]->pushDown();
for(Node* p = t->fa; p != f; p = t->fa) {
if(p->fa != f)
p->d() != t->d() ? Rotate(t) : Rotate(p);
Rotate(t);
}
t->upd();
}
 
void Access(Node* t) {
for(Node* o = Null; t != Null; o = t, t = t->par) {
Splay(t);
t->ch[1]->setRoot();
t->setc(o, 1);
}
}
 
void makeRoot(Node* t) {
Access(t);
Splay(t);
t->Rev();
}
 
void Cut(Node* x, Node* y) {
makeRoot(x);
Access(y);
Splay(x);
x->setc(Null, 1);
x->upd();
y->setRoot();
y->par = Null;
}
 
void Join(Node* x, Node* y) {
makeRoot(x);
x->par = y;
}
 
Node* Path(Node* x, Node* y) {
makeRoot(x);
Access(y);
Splay(y);
return y;
}
 
void Delete(Node* t) {
Cut(t, V[e[t->Id].u]);
Cut(t, V[e[t->Id].v]);
}
 
bool cmp(const int &l, const int &r) {
return e[l].w < e[r].w;
}
 
struct DSU {
int fa[maxn];
void Init() {
for(int i = 0; i < N; i++)
fa[i] = i;
}
int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
bool Unite(int x, int y) {
int _x = Find(x), _y = Find(y);
fa[_x] = _y;
return _x != _y;
}
} dsu;
 
void BuildMST() {
for(int i = 0; i < M; i++)
r[i] = i;
sort(r, r + M, cmp);
dsu.Init();
for(int i = 0; i < M; i++) if(F[r[i]]) {
if(!dsu.Unite(e[r[i]].u, e[r[i]].v))
continue;
Node* t = newNode(e[r[i]].w, r[i]);
Join(V[e[r[i]].u], t);
Join(V[e[r[i]].v], t);
}
}
 
void Work() {
for(int i = Q; i--; ) if(q[i].typ == 1) {
ans[i] = Path(V[q[i].u], V[q[i].v])->c->v;
} else {
int d = lower_bound(e, e + M, EDGE(q[i].u, q[i].v, 0)) - e;
if(F[d])
continue;
F[d] = true;
Node* t = Path(V[e[d].u], V[e[d].v]);
if(e[d].w < t->c->v) {
Delete(t->c);
Node* np = newNode(e[d].w, d);
Join(V[e[d].v], np);
Join(V[e[d].u], np);
}
}
for(int i = 0; i < Q; i++)
if(q[i].typ == 1) printf("%d\n", ans[i]);
}
 
void Init() {
N = read(); M = read(); Q = read();
for(int i = 0; i < M; i++)
F[i] = true;
for(int i = 0; i < M; i++) {
int u = read() - 1, v = read() - 1, w = read();
if(u > v) swap(u, v);
e[i] = EDGE(u, v, w);
}
sort(e, e + M);
for(int i = 0; i < Q; i++) {
int t = read(), u = read() - 1, v = read() - 1;
if(u > v) swap(u, v);
q[i] = Event(t, u, v);
if(t == 2)
F[lower_bound(e, e + M, EDGE(u, v, 0)) - e] = false;
}
Init_LCT();
for(int i = 0; i < N; i++)
V[i] = newNode(0);
}
 
int main() {
Init();
BuildMST();
Work();
return 0;
}

-----------------------------------------------------------------------

2594: [Wc2006]水管局长数据加强版

Time Limit: 25 Sec  Memory Limit: 128 MB
Submit: 1826  Solved: 553
[Submit][Status][Discuss]

Description

SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。
 

Input

输入文件第一行为3个整数:N, M, Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
以下M行,每行3个整数x, y和t,描述一条对应的水管。x和y表示水管两端结点的编号,t表示准备送水所需要的时间。我们不妨为结点从1至N编号,这样所有的x和y都在范围[1, N]内。
以下Q行,每行描述一项任务。其中第一个整数为k:若k=1则后跟两个整数A和B,表示你需要为供水公司寻找一条满足要求的从A到B的水管路径;若k=2,则后跟两个整数x和y,表示直接连接x和y的水管宣布报废(保证合法,即在此之前直接连接x和y尚未报废的水管一定存在)。
 

Output

按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。
 

Sample Input

4 4 3
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4

Sample Output

2
3

【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。

【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。

【C/C++选手注意事项】
由于此题输入规模较大(最大的测试点约20MB),因此即使使用scanf读入数据也会花费较多的时间。为了节省读入耗时,建议使用以下函数读入正整数(返回值为输入文件中下一个正整数):
int getint()
{
char ch = getchar();
for ( ; ch > '9' || ch < '0'; ch = getchar());
int tmp = 0;
for ( ; '0' <= ch && ch <= '9'; ch = getchar())
tmp = tmp * 10 + int(ch) - 48;
return tmp;
}

HINT

Source

BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )的更多相关文章

  1. BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 2917  Solved: 918[Submit][St ...

  2. BZOJ 2594: [Wc2006]水管局长数据加强版 (LCT维护最小生成树)

    离线做,把删边转化为加边,那么如果加边的两个点不连通,直接连就行了.如果联通就找他们之间的瓶颈边,判断一下当前边是否更优,如果更优就cut掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...

  3. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 934  Solved: 291[Submit][Sta ...

  4. BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  5. [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

    题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...

  6. 【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  7. bzoj 2594: [Wc2006]水管局长数据加强版

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  8. bzoj 2594 [Wc2006]水管局长数据加强版(LCT+最小生成树)

    [深坑勿入] [给个链接] http://blog.csdn.net/popoqqq/article/details/41348549 #include<cstdio> #include& ...

  9. [bzoj2594][Wc2006]水管局长数据加强版 (lct)

    论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...

随机推荐

  1. Java主线程等待子线程、线程池

    public class TestThread extends Thread { public void run() { System.out.println(this.getName() + &qu ...

  2. poj 1064 Cable master ,二分 精度!!!

    给出n根绳子,求把它们分割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 取代   while(r-l>eps) 循环100次精度能达到1e ...

  3. mvp框架

    本文在于巩固基础 mvp框架的概念: MVP是MVC模式的另一个变种,MVP即可以应用到WEB项目中, 也可以应用到Winform项目中,它的方便的测试机制为大型复杂的企业级应用带来了福音,MVP模式 ...

  4. IT人员----怎么把电脑窗口设置成淡绿色

    大夫建议电脑屏幕不要用白色,因为白色对眼睛的刺激是最大的.像我们这样整天对着电脑,也应该注意一下.其实,只要稍微设置一下,就能让你电脑上的窗口从白花花的颜色变成淡淡的绿色. 设置方法:打开控制面板中的 ...

  5. Winform mschart 动态绑定X时间表

    效果图: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  6. fragment详解(官方文档)

    原作者为: 苍山.感谢他分享的内容,现在分享出来给eoeAndroid的各位同胞. 详情参考http://www.eoeandroid.com/thread-71642-1-1.html和http:/ ...

  7. There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?

    错误信息: 严重: Exception starting filter struts2 Unable to load configuration. - action - file:/C:/Users/ ...

  8. CentOS安装maven3.2.2(Linux系列适用)

    首先,下载最新的maven3.2.2,地址:http://mirrors.cnnic.cn/apache/maven/maven-3/3.2.2/binaries/apache-maven-3.2.2 ...

  9. js——cookie

    cookie:存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来向访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2. cookie的存储是 ...

  10. JAVA 从GC日志分析堆内存 第七节

    JAVA 从GC日志分析堆内存 第七节   在上一章中,我们只设置了整个堆的内存大小.但是我们知道,堆又分为了新生代,年老代.他们之间的内存怎么分配呢?新生代又分为Eden和Survivor,他们的比 ...