您的位置:首页 > 其它

BZOJ 2594: [Wc2006]水管局长数据加强版(LCT+最小生成树+离线)

2017-06-29 15:07 387 查看

题目戳我

Solution

题目大意:就是让你维护一棵动态的最小生成树, 并询问两点路径中边权最大值。

很明显,用LCT来做这个。有几个关键点:

①删边维护mst不好搞,我们离线然后删边变加边。

②一开始用kruskal搞出底图的mst,然后加边,判断一条路径的最大值是否大于当前边的长度,是就替换掉原先的边。因为这样答案肯定不会差。

③注意答案的保存及下标的转换等大量细节。

④一开始建底图时找出有用的边用排序+二分查找,打上标记。

⑤LCT不要写错,否则就完了。

⑥本人的LCT自带巨大常数,所以我用了很多底层优化。诸如register,inline,位运算。。。。还有,一开始构mst时要注意,注意。。。

⑦忘记说了,本题将边当成点储存,一条边拆成两条,所以点数为n+m,所以写不好(没有⑥)就会TLE。。。

另外,我们证明(kou hu)一下为什么是求mst。首先容易发现按题目得到一个图。一开始我想了很久如何在这个图上搞事情,后来发现其实离线维护mst就可以了。因为两点之间的路径的最小的最大边一定在这个连通图的mst上。反证一下,假如不在,那么将这个“最小的最大边”替换掉mst中的那条唯一路径上的“不够优秀的最大边”换掉,mst肯定会更加的mst。就是说如果解不在mst上,那么它理应在mst上。所以它一定本来就在mst上。(以上纯属博主乱扯)

然后我们用常数巨大的LCT去应付10^6+10^5个点就可以了。重要的事情再次说一遍,不卡常数有很大风险超时。。。

下面附图佐证:



Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 100010
#define M 1000010

using namespace std;

int n, m, Q, cnt;
int Ans
;
struct Ab{int u, v, l, id;  bool f;} Edg[M], orgin[M];
struct Ever{int op, u, v, id;} oper
;

inline int Read(){
int x = 0;  char ch = getchar();
while(ch < '0' || ch > '9')  ch = getchar();
while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + ch - '0';  ch = getchar();}
return x;
}

void Print(int x){
if(x > 9)  Print(x / 10);
putchar(x % 10 + '0');
}

bool cmp(Ab A, Ab B){
if(A.u ^ B.u)  return A.u < B.u;
return A.v < B.v;
}

bool cmp__(Ab A, Ab B){
if(!(A.f ^ B.f))  return A.l < B.l;
return !B.f;
}

struct Tnode{
Tnode *son[2], *fa;
bool rev;
int val, Max, parent, id, Maxid;
int Get_d(){return fa->son[1] == this;}
void Connect(Tnode *now, int d){(son[d] = now)->fa = this;}
inline void Up(){
Max = val;
Maxid = id;
if(son[0] && son[0]->Max > Max)  Max = son[0]->Max, Maxid = son[0]->Maxid;
if(son[1] && son[1]->Max > Max)  Max = son[1]->Max, Maxid = son[1]->Maxid;
}
inline void Down(){
if(rev){
Tnode *now = son[0];
son[0] = son[1];
son[1] = now;
if(son[0])  son[0]->rev ^= 1;
if(son[1])  son[1]->rev ^= 1;
rev = false;
}
}
}tree[N+M], *Node[N+M];

inline Tnode *NewTnode(int id){
tree[cnt].rev = false;
tree[cnt].son[0] = tree[cnt].son[1] = tree[cnt].fa = NULL;
tree[cnt].parent = tree[cnt].val = tree[cnt].Max = 0;
tree[cnt].Maxid = tree[cnt].id = id;
return tree+cnt++;
}

void Zig(Tnode *now){
Tnode *last = now->fa;
int d = now->Get_d();
if(now->son[!d])  last->Connect(now->son[!d], d);
else  last->son[d] = NULL;
if(last->fa)  last->fa->Connect(now, last->Get_d());
else  now->fa = NULL;
now->Connect(last, !d);
last->Up();
now->parent = last->parent;
last->parent = 0;
}

void Splay(Tnode *now){
Tnode *last;
while(now->fa){
last = now->fa;
if(last->fa)  last->fa->Down();
last->Down();  now->Down();
if(last->fa)  (last->Get_d() ^ now->Get_d()) ? Zig(now) : Zig(last);
Zig(now);
}
if(!now->fa)  now->Down();
now->Up();
}

void Access(int x){

Splay(Node[x]);
if(Node[x]->son[1]){
Node[x]->son[1]->fa = NULL;
Node[x]->son[1]->parent = x;
Node[x]->son[1] = NULL;
Node[x]->Up();
}
int y = Node[x]->parent;
while(y){
Splay(Node[y]);
if(Node[y]->son[1]){
Node[y]->son[1]->fa = NULL;
Node[y]->son[1]->parent = y;
Node[y]->son[1] = NULL;
Node[y]->Up();
}
Node[y]->Connect(Node[x], 1);
Node[y]->Up();
Node[x]->parent = 0;
x = y;
y = Node[x]->parent;
}
}

void Evert(int x){
Access(x);
Splay(Node[x]);
Node[x]->rev ^= 1;
}

void Link(int x, int y){
Evert(x);
Node[x]->parent = y;
}

void Cut(int x, int y){
Evert(x);
Access(y);
Splay(Node[x]);
Node[x]->son[1]->fa = NULL;
Node[x]->son[1] = NULL;
Node[x]->Up();
}

int Find_Root(int x){
Access(x);
Splay(Node[x]);
Tnode *now = Node[x];
while(now->son[0])  now = now->son[0], now->Down();
return now->id;
}

int main(){

freopen("bzoj2594.in", "r", stdin);
freopen("bzoj2594.out",
f7d4
"w", stdout);

n = Read();  m = Read();  Q = Read();

for(register int i = 1; i <= n+m; ++i)  Node[i] = NewTnode(i);
for(register int i = 1; i <= m; ++i){
Edg[i].u = Read();  Edg[i].v = Read();  Edg[i].l = Read();
if(Edg[i].u > Edg[i].v)  Edg[i].u ^= Edg[i].v, Edg[i].v ^= Edg[i].u, Edg[i].u ^= Edg[i].v;
Edg[i].f = true;
Edg[i].id = i+n;
Node[i+n]->val = Node[i+n]->Max = Edg[i].l;
orgin[i] = Edg[i];
}

sort(Edg+1, Edg+m+1, cmp);
for(register int i = 1; i <= Q; ++i){
oper[i].op = Read();  oper[i].u = Read();  oper[i].v = Read();
if(oper[i].u > oper[i].v)   oper[i].u ^= oper[i].v, oper[i].v ^= oper[i].u, oper[i].u ^= oper[i].v;
int L = 1, R = m, mid;
while(L <= R){
mid = (L + R) >> 1;
if(Edg[mid].u == oper[i].u && Edg[mid].v == oper[i].v){
if(oper[i].op == 2)  Edg[mid].f = false;
oper[i].id = Edg[mid].id;
break;
}
if(Edg[mid].u < oper[i].u || (Edg[mid].u == oper[i].u && Edg[mid].v < oper[i].v))  L = mid + 1;
else  R = mid - 1;
}
}
sort(Edg+1, Edg+m+1, cmp__);

for(register int i = 1; i < n; ++i){
int a = Edg[i].u, b = Edg[i].v;
int x = Find_Root(a), y = Find_Root(b);
if(x ^ y){
Link(a, Edg[i].id);
Link(Edg[i].id, b);
}
else{
Evert(a);
Access(b);
Splay(Node[a]);
int id = Node[a]->Maxid;
if(Node[a]->Max > Edg[i].l){
Cut(orgin[id-n].u, id);
Cut(id, orgin[id-n].v);
Link(a, Edg[i].id);
Link(Edg[i].id, b);
}
}
}

for(register int i = Q; i; --i){
int a = oper[i].u, b = oper[i].v;
int x = Find_Root(a), y = Find_Root(b);
if(oper[i].op == 1){
Evert(a);
Access(b);
Splay(Node[a]);
Ans[++Ans[0]] = Node[a]->Max;
}
else{
if(x ^ y){
Link(a, oper[i].id);
Link(oper[i].id, b);
}
else{
Evert(a);
Access(b);
Splay(Node[a]);
int id = Node[a]->Maxid;
if(Node[a]->Max > orgin[oper[i].id-n].l){
Cut(orgin[id-n].u, id);
Cut(id, orgin[id-n].v);
Link(a, oper[i].id);
Link(oper[i].id, b);
}
}
}
}

for(register int i = Ans[0]; i; --i){
Print(Ans[i]);
putchar('\n');
}
return 0;
}




世界以痛吻我 我却报之以歌
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: