水管局长

来源:互联网 发布:java 调用 class 编辑:程序博客网 时间:2024/04/27 19:28

水管局长

时间限制: 3 Sec 内存限制: 128 MB

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

输入
输入文件第一行为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尚未报废的水管一定存在)。

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

样例输入
4 4 3
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4

样例输出
2
3

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

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

来源
原题:WC2006
加强版:bzoj2594

题解

lct维护最小生成树。因为如果在点上记录边权的话,由于树是动的,因此很难实现。所以可以把边变成点,并把边权变成点权。这样就只要维护任意两个点之间点权最大的点,每次修改把这个点上连得2条边去除,并加入新的点(边)即可。

代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<algorithm>#define N 100010#define M 1000010using namespace std;int n,m,Q,tot,fa[N+M],ans[N],e[N];int st[N],end[N],flag[M],fx[N];struct node{int a,b,c,pos;}map[M],ma[M];struct wbs{int type,a,b;}q[N];struct point{int c[2],fa,maxn,pos,num,rev;}t[N+M];bool cmp1(const node &x,const node &y){return x.a<y.a||(x.a==y.a&&x.b<y.b);}bool cmp2(const node &x,const node &y){return x.c<y.c;}int erfen(int x,int y){  int l=st[x],r=end[x];  while(r-l>1)  {    int mid=l+r>>1;    if(map[mid].b<=y)l=mid;    else r=mid-1;  }  return map[l].b==y?l:r;}int find(int x){  if(!fx[x])return x;  return fx[x]=find(fx[x]);}class lct{  void pushdown(int x)  {    if(!t[x].rev)return;    int lc=t[x].c[0],rc=t[x].c[1];    t[lc].rev^=1;swap(t[lc].c[0],t[lc].c[1]);    t[rc].rev^=1;swap(t[rc].c[0],t[rc].c[1]);    t[x].rev=0;  }  void update(int x)  {    int lc=t[x].c[0],rc=t[x].c[1];    t[x].maxn=t[x].num;t[x].pos=x;    if(t[lc].maxn>t[x].maxn)      t[x].maxn=t[lc].maxn,t[x].pos=t[lc].pos;    if(t[rc].maxn>t[x].maxn)      t[x].maxn=t[rc].maxn,t[x].pos=t[rc].pos;  }  void rotate(int x,int k)  {    int y=t[x].fa,f=(t[t[y].fa].c[1]==y);    pushdown(y);pushdown(x);    if(!t[y].fa)fa[x]=fa[y];    t[x].fa=t[y].fa;t[t[y].fa].c[f]=x;    t[y].fa=x;t[y].c[k]=t[x].c[k^1];    t[t[y].c[k]].fa=y;t[x].c[k^1]=y;    update(y);  }  void splay(int x,int des)  {    pushdown(x);    if(x==des)return;    while(t[x].fa!=des)    {      int y=t[x].fa,f=(t[y].c[1]==x);      if(t[y].fa==des)rotate(x,f);      else      {        if((t[t[y].fa].c[1]==y)==f)          rotate(y,f),rotate(x,f);        else rotate(x,f),rotate(x,f^1);      }    }    update(x);  }  void access(int x)  {    for(int y=0;x;y=x,x=fa[x])    {      splay(x,0);      t[t[x].c[1]].fa=0;fa[t[x].c[1]]=x;      t[x].c[1]=y;fa[y]=0;t[y].fa=x;      update(x);    }  }  int lca(int x,int y)  {    access(x);    for(splay(y,0);fa[y];splay(y,0))y=fa[y];    return y;  }  public:  void link(int x,int y)  {    access(x);splay(x,0);    swap(t[x].c[0],t[x].c[1]);    t[x].rev^=1;fa[x]=y;  }  void cut(int x,int y)  {    access(x);splay(y,0);    if(fa[y]==x)swap(x,y);splay(x,0);    t[t[x].c[0]].fa=0;    fa[t[x].c[0]]=fa[x];    t[x].c[0]=fa[x]=0;    update(x);  }  int qry(int x,int y)  {    int pos=lca(x,y),res=pos;    access(x);splay(x,0);splay(pos,x);if(t[t[pos].c[1]].maxn>t[res].num)res=t[t[pos].c[1]].pos;    access(y);splay(y,0);splay(pos,y);if(t[t[pos].c[1]].maxn>t[res].num)res=t[t[pos].c[1]].pos;    return res;  }}T;int main(){  int a,b,c,type;  scanf("%d%d%d",&n,&m,&Q);  for(int i=1;i<=m;i++)  {    scanf("%d%d%d",&a,&b,&c);    if(a>b)swap(a,b);map[i]=(node){a,b,c,i};ma[i]=map[i];    t[i+n].maxn=c;t[i+n].num=c;t[i+n].pos=i;  }  sort(map+1,map+m+1,cmp1);  for(int i=1;i<=m;i++)    if(map[i].a>map[i-1].a)      st[map[i].a]=i,end[map[i-1].a]=i-1;  end[map[m].a]=m;  for(int i=1;i<=Q;i++)  {    scanf("%d%d%d",&type,&a,&b);    if(a>b)swap(a,b);q[i]=(wbs){type,a,b};    if(type==2)flag[e[i]=map[erfen(a,b)].pos]=1;   }  sort(map+1,map+m+1,cmp2);  for(int i=1,sum=0;i<=m&&sum<n-1;i++)  {    if(flag[map[i].pos])continue;    int a=map[i].a,b=map[i].b,x=find(a),y=find(b);    if(x!=y)fx[y]=x,T.link(a,map[i].pos+n),T.link(b,map[i].pos+n),sum++;  }  for(int i=Q;i;i--)  {    int a=q[i].a,b=q[i].b,l=T.qry(a,b)-n;    if(q[i].type==2)    {      if(ma[l].c>ma[e[i]].c)      {        T.cut(ma[l].a,l+n);T.cut(ma[l].b,l+n);        T.link(a,e[i]+n);T.link(b,e[i]+n);      }    }     else ans[++tot]=ma[l].c;  }  for(int i=tot;i;i--)printf("%d\n",ans[i]);  return 0;}
0 0
原创粉丝点击