zoukankan      html  css  js  c++  java
  • bzoj2594: [Wc2006]水管局长数据加强版

     地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2594

    题目:

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

    Time Limit: 25 Sec  Memory Limit: 128 MB
    Submit: 3770  Solved: 1194
    [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;
    }

    吐槽:
      代码一小时,debug四小时,写错一个变量,花式re,TLE,MLE,wa。
      最后还是靠换成fread的fastio卡过去的。
      总结,老年人不适合写代码。
     
    思路:
      动态维护最小生成树,直接处理不行,因为这是一个无向图。
      把询问拿下来逆序处理,要删的边先标记,把其他边跑个最小生成树。
      然后倒着处理询问,这样所有操作都是向最小生成树上加边。
      根据kruskal的思想,如果要加入的边的端点不在同一并查集中,直接连边。
      在同一集合中的话,加边后会成环,此时把环上最大一条边删除即可。
      lct怎么处理边权?把第i条边当做第i+n个点即可。
      1 /**************************************************************
      2     Problem: 2594
      3     User: weeping
      4     Language: C++
      5     Result: Accepted
      6     Time:25656 ms
      7     Memory:103836 kb
      8 ****************************************************************/
      9  
     10 #include <bits/stdc++.h>
     11  
     12 using namespace std;
     13 namespace fastIO{
     14     #define BUF_SIZE 500000
     15     bool IOerror=0;
     16     inline char nc(){
     17         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
     18         if(p1==pend){
     19             p1=buf;
     20             pend=buf+fread(buf,1,BUF_SIZE,stdin);
     21             if(pend==p1){
     22                 IOerror=1;
     23                 return -1;
     24             }
     25         }return *p1++;
     26     }
     27     inline bool blank(char ch){
     28         return ch==' '||ch=='
    '||ch=='
    '||ch=='	';
     29     }
     30     inline bool read(int &x){
     31         char ch;
     32         while(blank(ch=nc()));
     33         if(IOerror)return 0;
     34         for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
     35         return 1;
     36     }
     37     #undef BUF_SIZE
     38 };
     39  
     40 using namespace fastIO;
     41  
     42 struct node
     43 {
     44     int u,v,w,f;
     45     node(){}
     46     node(int x,int y,int z){u=x,v=y,w=z;}
     47 }eg[1000007],qs[100007];
     48 int n,m,q,f[100007],vis[1000007],ans[100007];
     49 map<pair<int,int>,int>hs;
     50  
     51 struct Link_Cut_Tree
     52 {
     53     static const int MAXN = 1200000 + 7;
     54  
     55     int ch[MAXN][2], fa[MAXN], rev[MAXN], sz[MAXN], v[MAXN], mx[MAXN], id[MAXN];
     56     int sk[MAXN];
     57  
     58     bool isroot(int x)
     59     {
     60         return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
     61     }
     62  
     63     void reverse(int x)
     64     {
     65         rev[x] ^= 1, swap(ch[x][0],ch[x][1]);
     66     }
     67  
     68     void update(int x)
     69     {
     70         int lc = ch[x][0], rc = ch[x][1];
     71         if(mx[lc]>mx[rc])
     72             mx[x]=mx[lc],id[x]=id[lc];
     73         else
     74             mx[x]=mx[rc],id[x]=id[rc];
     75         if(v[x]>mx[x])
     76             mx[x]=v[x],id[x]=x;
     77     }
     78  
     79     void push_down(int x)
     80     {
     81         if(!rev[x]) return ;
     82         if(ch[x][0]) reverse(ch[x][0]);
     83         if(ch[x][1]) reverse(ch[x][1]);
     84         rev[x]=0;
     85     }
     86  
     87     void rotate(int x)
     88     {
     89         int f = fa[x], gf = fa[f];
     90         int t1 = ( x != ch[f][0]), t2 = ( f != ch[gf][0]), tmp = ch[x][1^t1];
     91         if(!isroot(f)) ch[gf][0^t2] = x;
     92         fa[tmp] = f, fa[x] = gf, ch[x][1^t1] = f, fa[f] = x, ch[f][0^t1] = tmp;
     93         update(f);
     94     }
     95  
     96     void splay(int x)
     97     {
     98         int top = 0;
     99         sk[++top] = x;
    100         for(int i = x; !isroot(i); i = fa[i])   sk[++top] = fa[i];
    101         while(top)  push_down(sk[top--]);
    102         for(int f = fa[x], gf = fa[f]; !isroot(x); rotate(x), f = fa[x],gf = fa[f])
    103         if(!isroot(f))
    104             rotate((x==ch[f][0]) ^ (f==ch[gf][0]) ? x : f);
    105         update(x);
    106     }
    107  
    108     void access(int x)
    109     {
    110         for(int p = 0; x; p = x, x = fa[x])
    111             splay(x), ch[x][1] = p, update(x);
    112     }
    113  
    114     void makeroot(int x)
    115     {
    116         access(x), splay(x), reverse(x);
    117     }
    118  
    119     int findroot(int x)
    120     {
    121         access(x), splay(x);
    122         while(ch[x][0]) x = ch[x][0];
    123         return x;
    124     }
    125     void link(int x,int y)
    126     {
    127         makeroot(x), fa[x] = y;
    128     }
    129  
    130     void cut(int x,int y)
    131     {
    132         makeroot(x), access(y), splay(y);
    133         if(ch[y][0] == x)   ch[y][0] = fa[x] = 0;
    134         update(y);
    135     }
    136  
    137     int go(int op,int x,int y,int hid)
    138     {
    139         if(x>y) swap(x,y);
    140         if(op==1)
    141         {
    142             makeroot(x),access(y),splay(y);
    143             return mx[y];
    144         }
    145         if(findroot(x)!=findroot(y))
    146            link(x,hid+n),link(hid+n,y);
    147         else
    148         {
    149             makeroot(x),access(y),splay(y);
    150             if(mx[y]>eg[hid].w)
    151             {
    152                 int pp=id[y]-n;
    153                 cut(eg[pp].u,pp+n),cut(eg[pp].v,pp+n);
    154                 link(x,hid+n),link(hid+n,y);
    155             }
    156         }
    157         return 0;
    158     }
    159     void debug(void)
    160     {
    161         for(int i=1;i<=100;i++)
    162             printf("%d %d %d %d %d %d %d
    ",i,fa[i],ch[i][0],ch[i][1],rev[i],sz[i]);
    163     }
    164 }lct;
    165  
    166 int tid[1000007];
    167 bool cmp(const int &ta,const int &tb)
    168 {
    169     return eg[ta].w<eg[tb].w;
    170 }
    171 int fd(int x)
    172 {
    173     return f[x]==x?x:f[x]=fd(f[x]);
    174 }
    175 void mintree(void)
    176 {
    177     for(int i=1;i<=n;i++) f[i]=i;
    178     for(int i=1;i<=m;i++) tid[i]=i;
    179     sort(tid+1,tid+1+m,cmp);
    180     for(int i=1;i<=m;i++)
    181     {
    182         int p=tid[i];
    183         if(vis[p]) continue;
    184         int fa=fd(eg[p].u),fb=fd(eg[p].v);
    185         if(fa==fb)  continue;
    186         f[fa]=fb,lct.link(eg[p].u,p+n),lct.link(eg[p].v,p+n);
    187     }
    188 }
    189 int main(void)
    190 {
    191     //freopen("in.acm","r",stdin);
    192     read(n),read(m),read(q);
    193     for(int i=0;i<=n;i++) lct.v[i]=-1;
    194     for(int i=1,x,y,z;i<=m;i++)
    195     {
    196         read(x),read(y),read(z),eg[i]=node(x,y,z);
    197         if(x>y)swap(x,y);
    198         hs[make_pair(x,y)]=i;
    199         lct.v[i+n]=lct.mx[i+n]=z,lct.id[i+n]=i+n;
    200     }
    201     for(int i=1,op,x,y;i<=q;i++)
    202     {
    203         read(op),read(x),read(y),qs[i]=node(op,x,y);
    204         if(x>y)swap(x,y);
    205         qs[i].f=hs[make_pair(x,y)];
    206         if(op==2)
    207             vis[qs[i].f]=1;
    208     }
    209     mintree();
    210     for(int i=q;i;i--)
    211         ans[i]=lct.go(qs[i].u,qs[i].v,qs[i].w,qs[i].f);
    212     for(int i=1;i<=q;i++)
    213     if(qs[i].u==1)
    214         printf("%d
    ",ans[i]);
    215     return 0;
    216 }
  • 相关阅读:
    python threading 锁的应用
    python线程threading处理任务并发一
    *,arg,*args,**kwargs的使用
    web services 接口调用
    jsonp与ajax
    无缝滚动详解
    手机端使用rem适配
    css3写的实用表单美化
    经典仿淘宝商城菜单多条件查询
    css3 flex写的移动端界面
  • 原文地址:https://www.cnblogs.com/weeping/p/7617818.html
Copyright © 2011-2022 走看看