bzoj 2594 WC 2006 水管局长数据加强版 [Link-Cut Tree]

21 篇文章 0 订阅
19 篇文章 0 订阅

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

Time Limit: 25 Sec Memory Limit: 128 MB
Submit: 2560 Solved: 820

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读入数据也会花费较多的时间。


处理边权类LCT可以把边看成一个点然后分别连接这条边的两点。
然后维护一个路径上的最大值及其位置即可,可以用二分查找边,由于不能删边,所以要倒着做,离线处理。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
inline void read(int &x)
{
    x=0;
    int flag=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-') flag=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x*=10; x+=ch-'0';
        ch=getchar();
    }
    x*=flag;
}
inline void readout(int x)
{
    if(!x)
    {
        putchar('0'); putchar('\n');
        return;
    }
    char ans[15];
    int pos=0;
    while(x)
    {
        ans[++pos]=x%10+'0';
        x/=10;
    }
    while(pos) putchar(ans[pos--]);
    putchar('\n');
}
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 100005;
const int maxm = 1000005;
struct Node
{
    int fa;
    int ch[2];
    bool isroot;
    int rev;
    int mx,id;
    int val,orig;
}node[maxn<<2]; // count the nodes,edges for MST, and edges added later
int maxnode;
#define fa(x) node[x].fa
#define ch(x,d) node[x].ch[d]
#define isroot(x) node[x].isroot
#define rev(x) node[x].rev
#define mx(x) node[x].mx
#define id(x) node[x].id
#define val(x) node[x].val
#define orig(x) node[x].orig
inline void update(int x)
{
    if(!x) return;
    mx(x)=val(x); id(x)=orig(x);
    if(ch(x,0) && mx(ch(x,0))>mx(x)) mx(x)=mx(ch(x,0)),id(x)=id(ch(x,0));
    if(ch(x,1) && mx(ch(x,1))>mx(x)) mx(x)=mx(ch(x,1)),id(x)=id(ch(x,1));
}
inline void rotate(int x)
{
    int y=fa(x),z=fa(y);
    int l=(ch(y,1)==x),r=l^1;
    if(isroot(y)) isroot(y)=false,isroot(x)=true;
    else ch(z,ch(z,1)==y)=x;
    fa(ch(x,r))=y; fa(y)=x; fa(x)=z;
    ch(y,l)=ch(x,r); ch(x,r)=y;
    update(y); update(x);
}
inline void pushdown(int x)
{
    if(!x) return;
    if(!rev(x)) return;
    rev(x)^=1; swap(ch(x,0),ch(x,1));
    if(ch(x,0)) rev(ch(x,0))^=1;
    if(ch(x,1)) rev(ch(x,1))^=1;
}
int sta[maxn<<1];
int top;
inline void upgrade(int x)
{
    while(!isroot(x)) sta[++top]=x,x=fa(x);
    sta[++top]=x;
    while(top) pushdown(sta[top--]);
}
inline void splay(int x)
{
    upgrade(x);
    while(!isroot(x))
    {
        int y=fa(x),z=fa(y);
        if(!isroot(y))
            if(ch(y,0)==x ^ ch(z,0)==y) rotate(x);
            else rotate(y);
        rotate(x);
    }
}
inline int access(int x)
{
    int y=0;
    do
    {
        splay(x);
        isroot(ch(x,1))=true;
        isroot(ch(x,1)=y)=false;
        update(x);
        x=fa(y=x);
    }while(x);
    return y;
}
inline void makeroot(int x)
{
    int rt=access(x);
    rev(rt)^=1;
}
void cut(int u,int v)
{
    makeroot(u);
    access(v); splay(v);
    isroot(ch(v,0))=true;
    fa(ch(v,0))=0; ch(v,0)=0;
    update(v);
}
void join(int u,int v)
{
    access(u); splay(u);
    rev(u)^=1;
    fa(u)=v;
}
inline int findroot(int x)
{
    while(fa(x)) x=fa(x);
    return x;
}
inline bool query_block(int u,int v)
{
    int t1=findroot(u) , t2=findroot(v);
    return t1==t2;
}
int query(int u,int v) // return the id of nodes
{
    makeroot(u);
    access(v); splay(v);
    return id(v);
}
struct Road
{
    int u,v;
    int c;
    bool flag;
    bool operator < (const Road t) const { return c < t.c; }
}road[maxm];
struct Fnode
{
    int u,v;
}fnode[maxn<<2];
bool cmp(const Road x,const Road y)
{
    if(x.u ^ y.u) return x.u < y.u;
    return x.v < y.v;
}
struct Query
{
    int k,x,y;
}que[maxn];
int n,m,q;
void kruskal()
{
    int tot=0;
    for(int i=1;i<=m;i++) if(road[i].flag)
    {
        if(query_block(road[i].u,road[i].v)) continue;
        Road now=road[i];
        Node &t=node[++maxnode]; fnode[maxnode].u=now.u,fnode[maxnode].v=now.v;
        t.mx=t.val=now.c; t.id=t.orig=maxnode;
        t.isroot=true;
        join(maxnode,now.u);
        join(maxnode,now.v);
        if(++tot == n-1) break;
    }
}
int ans[maxn];
int topp;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("water.in","r",stdin);
    freopen("water.out","w",stdout);
#endif
    read(n),read(m),read(q);
    for(int i=1;i<=n;i++) isroot(i)=true;
    maxnode=n;
    for(int i=1;i<=m;i++)
    {
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        if(u>v) swap(u,v);
        road[i]=(Road){u,v,c,true};
    }
    sort(road+1,road+m+1,cmp);
    for(int i=1;i<=q;i++)
    {
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(x>y) swap(x,y);
        que[i]=(Query){k,x,y};
        if(k==1) continue;
        Road t; t.u=x; t.v=y;
        int pos=lower_bound(road+1,road+m+1,t,cmp)-road;
        road[pos].flag=false;
    }
    sort(road+1,road+m+1);
    kruskal();
    sort(road+1,road+m+1,cmp);
    for(int i=q;i>=1;i--)
        if(que[i].k==1) ans[++topp]=val(query(que[i].x,que[i].y));
        else
        {
            Road tt=(Road){que[i].x,que[i].y};
            int pos=lower_bound(road+1,road+m+1,tt,cmp)-road;
            int tmp=query(que[i].x,que[i].y);
            if(val(tmp)<road[pos].c) continue;
            cut(tmp,fnode[tmp].u); cut(tmp,fnode[tmp].v);
            Node &t=node[++maxnode]; fnode[maxnode].u=que[i].x,fnode[maxnode].v=que[i].y;
            t.mx=t.val=road[pos].c; t.id=t.orig=maxnode;
            t.isroot=true;
            join(maxnode,que[i].x);
            join(maxnode,que[i].y);
        }
    while(topp) readout(ans[topp--]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值