【WC2006】【BZOJ2594】水管局长数据加强版

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

鸣谢Kac

LCT维护动态最小生成树.
边权排序然后加不会被删除的边.
把询问倒序,就可以将删边看成加边,加边时候边权判断,link cut一下.
对每个删除记录删除的边的编号.
和魔法森林还是很类似的.
被卡了一发内存…必须卡着150W开…开到200W就炸飞

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1500010
#define GET (ch>='0'&&ch<='9')
using namespace std;
int n,m,q;
int u,v,w;
int sta[MAXN],top;
int f[MAXN];
int find(int x) {return f[x]==x?f[x]:f[x]=find(f[x]);}
bool Union(int a,int b) {int x=find(a),y=find(b);if (x!=y){f[x]=y;return 1;}return 0;}
struct edge
{
    int u,v,w,id;
    bool del;
    bool operator <(const edge& a)const {return u==a.u?v<a.v:u<a.u;}
    bool operator ==(const edge& a)const    {return u==a.u&&v==a.v;}
}e[MAXN],*prev[MAXN];
struct opt
{
    int opt,u,v,id,ans;
}que[MAXN];
struct splay
{
    int ch[2],fa,maxn,val;
    bool rev;
}tree[MAXN];
bool is_root(int x)
{
    return tree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;
}
void push_up(int x)
{
    tree[x].maxn=x;
    if (tree[tree[tree[x].ch[0]].maxn].val>tree[tree[x].maxn].val)  tree[x].maxn=tree[tree[x].ch[0]].maxn;
    if (tree[tree[tree[x].ch[1]].maxn].val>tree[tree[x].maxn].val)  tree[x].maxn=tree[tree[x].ch[1]].maxn;
}
void push_down(int x)
{
    if (tree[x].rev)    tree[tree[x].ch[0]].rev^=1,tree[tree[x].ch[1]].rev^=1,swap(tree[x].ch[0],tree[x].ch[1]);
    tree[x].rev=0;
}
void rot(int x)
{
    int y=tree[x].fa,z=tree[y].fa,l,r;
    l=(tree[y].ch[1]==x);r=l^1;
    if (!is_root(y))    tree[z].ch[tree[z].ch[1]==y]=x;
    tree[x].fa=z;tree[y].fa=x;tree[tree[x].ch[r]].fa=y;tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;
    push_up(y);push_up(x);
}
void Splay(int x)
{
    top=0;sta[++top]=x;
    for (int i=x;i;i=tree[i].fa)    sta[++top]=tree[i].fa;
    while (top) push_down(sta[top--]);
    while (!is_root(x))
    {
        int y=tree[x].fa,z=tree[y].fa;
        if (!is_root(y))
        {
            if ((tree[y].ch[0]==x)^(tree[z].ch[0]==y))  rot(x);
            else    rot(y);
        }
        rot(x);
    }
}
void access(int x)
{
    for (int i=0;x;i=x,x=tree[x].fa)    Splay(x),tree[x].ch[1]=i,push_up(x);
}
void make_root(int x)
{
    access(x);Splay(x);tree[x].rev^=1;
}
void link(int x,int y)
{
    make_root(x);tree[x].fa=y;
}
int query(int x,int y)
{
    make_root(x);access(y);Splay(y);return tree[y].maxn;
}
void cut(int x,int y)
{
    make_root(x);access(y);Splay(y);tree[y].ch[0]=tree[x].fa=0;push_up(y);
}
void in(int &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
bool cmp(edge a,edge b) {return a.w<b.w;}
bool cmp2(edge a,edge b)    {return a.id<b.id;}
int find(edge t)
{
    int l=1,r=m,mid=(l+r)>>1;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (e[mid]==t)  return mid;
        if (e[mid]<t)   l=mid+1;
        else    r=mid-1;
    }
}
int main()
{
    in(n);in(m);in(q);
    for (int i=1;i<=n;i++)  f[i]=i;
    for (int i=1;i<=m;i++)
    {
        in(e[i].u),in(e[i].v),in(e[i].w);
        if (e[i].u>e[i].v)  swap(e[i].u,e[i].v);
    }sort(e+1,e+m+1,cmp);
    for (int i=1;i<=m;i++)  e[i].id=i,tree[n+i].val=e[i].w,tree[n+i].maxn=n+i;
    sort(e+1,e+m+1);
    for (int i=1;i<=q;i++)
    {
        in(que[i].opt),in(que[i].u),in(que[i].v);
        if (que[i].opt==1)  continue;
        if (que[i].u>que[i].v)  swap(que[i].u,que[i].v);
        edge x;x.u=que[i].u;x.v=que[i].v;int pos=find(x);
        e[pos].del=1;que[i].id=e[pos].id;
    }sort(e+1,e+m+1,cmp2);
    for (int i=1;i<=m;i++)
        if (!e[i].del&&Union(e[i].u,e[i].v))    link(e[i].u,i+n),link(e[i].v,i+n);
    for (int i=q;i;i--)
    {
        if (que[i].opt==1)  que[i].ans=tree[query(que[i].u,que[i].v)].val;
        else
        {
            int pos=query(que[i].u,que[i].v);
            if (e[que[i].id].w<tree[pos].val)   cut(e[pos-n].u,pos),cut(e[pos-n].v,pos),link(que[i].u,que[i].id+n),link(que[i].v,que[i].id+n);
        }
    }
    for (int i=1;i<=q;i++)
        if (que[i].opt==1)  printf("%d\n",que[i].ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值