zoukankan      html  css  js  c++  java
  • bzoj 2594: [Wc2006]水管局长数据加强版 动态树

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

    Time Limit: 25 Sec  Memory Limit: 128 MB
    Submit: 934  Solved: 291
    [Submit][Status]

    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;
    }

      动态树的常数优化根本搞不懂,这道题我是全加inline,register,26000ms水过的(bzoj真神奇)
      还是有一点点小技巧,比如说set的find如果要调用多次,就尽量用iterator实现,access函数可以返回access后根节点的值,然后这次犯的错误是删边时只清空了ch[][],没有清空pnt[].
      如果在noi之前做到这道题,那么noi的《魔法森林》应该就是水题了。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<vector>
    using namespace std;
    #define MAXM 1100000
    #define MAXN 110000
    #define MAXQ 110000
    #define MAXT MAXM+MAXN
    inline int nextInt()
    {
            register char ch;
            register int x=0;
            while (ch=getchar(),ch<'0' || ch>'9');
            do
                    x=x*10+ch-'0';
            while (ch=getchar(),ch<='9' && ch>='0');
            return x;
    }
    int n,m,q;
    int ch[MAXT][2],pnt[MAXT];
    int mx[MAXT],mt[MAXT];
    int val[MAXT],siz[MAXT],pos[MAXT];
    bool rev[MAXT];
    int stack[MAXT],tops=-1;
    inline bool is_root(int now)
    {
            return !pnt[now] || (ch[pnt[now]][0]!=now && ch[pnt[now]][1]!=now);
    }
    inline void update(int now)
    {
            siz[now]=siz[ch[now][0]]+siz[ch[now][1]]+1;
            if (mx[ch[now][0]]>mx[ch[now][1]])
            {
                    mx[now]=mx[ch[now][0]];
                    mt[now]=mt[ch[now][0]];
            }else
            {
                    mx[now]=mx[ch[now][1]];
                    mt[now]=mt[ch[now][1]];
            }
            if (mx[now]<val[now])
            {
                    mx[now]=val[now];
                    mt[now]=now;
            }
    }
    inline void reverse(int now)
    {
            if (!now)return ;
            swap(ch[now][0],ch[now][1]);
            rev[now]^=1;
    }
    inline void down(int now)
    {
            if (rev[now])
            {
                    reverse(ch[now][0]);
                    reverse(ch[now][1]);
                    rev[now]=0;
            }
    }
    inline void rotate(int now)
    {
            register int p=pnt[now],anc=pnt[p];
            register int dir=ch[p][0]==now;
            if (!is_root(pnt[now]))
                    ch[anc][ch[anc][1]==p]=now;
            pnt[now]=anc;
            if (ch[now][dir])
                    pnt[ch[now][dir]]=p;
            ch[p][1-dir]=ch[now][dir];
            pnt[p]=now;
            ch[now][dir]=p;
            update(p);
            update(now);
    }
    void splay(int now)
    {
            int x=now;
            stack[++tops]=x;
            while (!is_root(x))
            {
                    x=pnt[x];
                    stack[++tops]=x;
            }
            while (~tops)
                    down(stack[tops--]);
            while (!is_root(now))
            {
                    int p=pnt[now],anc=pnt[p];
                    if (is_root(p))
                            rotate(now);
                    else if ((ch[anc][0]==p) == (ch[p][0]==now))
                            rotate(p),rotate(now);
                    else
                            rotate(now),rotate(now);
            }
    }
    int access(int now)
    {
            int son=0;
            while (now)
            {
                    splay(now);
                    ch[now][1]=son;
                    son=now;
                    update(now);
                    now=pnt[now];
            }
            return son;
    }
    inline void make_root(int now)
    {
            access(now);
            splay(now);
            reverse(now);
    }
    struct aaa
    {
            int x,y,d,id;
    }el[MAXM],e[MAXM];
    struct bbb
    {
            int x,y,d,t,id;
    }qur[MAXQ];
    bool operator <(aaa a1,aaa a2)
    {
            if (a1.x==a2.x)return a1.y<a2.y;
            return a1.x<a2.x;
    }
    set<aaa> S;
    bool cmp_d(aaa a1,aaa a2)
    {
            return a1.d<a2.d;
    }
    int uf[MAXN];
    int get_fa(int now)
    {
            return now==uf[now] ? now : uf[now]=get_fa(uf[now]);
    }
    bool comb(int x,int y)
    {
            x=get_fa(x);
            y=get_fa(y);
            if (x==y)return false;
            uf[x]=y;
            return true;
    }
    void add_edge(int x,int y)
    {
            //cout<<"Add:"<<x<<" "<<y<<endl;
            make_root(x);
            make_root(y);
            ch[x][0]=y;
            pnt[y]=x;
            update(x);
    }
    void erase_edge(int x,int y)
    {
            //cout<<"Del:"<<x<<" "<<y<<endl;
            make_root(x);
            access(y);
            splay(x);
            if (ch[x][0]==y)
            {
                    splay(y);
                    ch[y][1]=pnt[x]=0;
                    update(y);
            }else if (ch[x][1]==y)
            {
                    ch[x][1]=pnt[y]=0;//注意清空pnt[]
                    update(x);
            }else throw 1;
    }
    vector<int> ans;
    pair<int,int> Qry_path(int x,int y)
    {
            make_root(x);
            int t=access(y);
            return make_pair(mx[t],mt[t]-n-1);
    }
    int main()
    {
            freopen("input.txt","r",stdin);
            int i,j,k;
            scanf("%d%d%d",&n,&m,&q);
            aaa at;
            for (i=1;i<=n;i++)uf[i]=i;
            for (i=0;i<m;i++)
            {
                    at.x=nextInt();at.y=nextInt();at.d=nextInt();
                    //scanf("%d%d%d",&at.x,&at.y,&at.d);
                    val[n+i+1]=at.d;
                    at.id=i;
                    if (at.x>at.y)
                            swap(at.x,at.y);
                    S.insert(at);
                    el[i]=at;
            }
            set<aaa>::iterator it1;
            for (i=0;i<q;i++)
            {
                    qur[i].t=nextInt();qur[i].x=nextInt();qur[i].y=nextInt();
                    //scanf("%d%d%d",&qur[i].t,&qur[i].x,&qur[i].y);
                    if (qur[i].t==2)
                    {
                            if (qur[i].x>qur[i].y)
                                    swap(qur[i].x,qur[i].y);
                            at.x=qur[i].x,at.y=qur[i].y;
                            it1=S.find(at);
                            qur[i].d=it1->d;
                            qur[i].id=it1->id;
                            S.erase(it1);
                    }
            }
            m=0;
            for (it1=S.begin();it1!=S.end();it1++)
            {
                    e[m++]=*it1;
            }
            sort(e,e+m,cmp_d);
            for (i=0;i<m;i++)
            {
                    if (comb(e[i].x,e[i].y))
                    {
                            add_edge(e[i].x,n+e[i].id+1);
                            add_edge(e[i].y,n+e[i].id+1);
                    }
            }
            for (i=q-1;i>=0;i--)
            {
                    if (qur[i].t==2)
                    {
                            pair<int,int> pr;
                            pr=Qry_path(qur[i].x,qur[i].y);
                            if (pr.first<=qur[i].d)continue;
                            erase_edge(el[pr.second].x,pr.second+1+n);
                            erase_edge(el[pr.second].y,pr.second+1+n);
                            add_edge(qur[i].x,qur[i].id+n+1);
                            add_edge(qur[i].y,qur[i].id+n+1);
                    }else
                    {
                            pair<int,int> pr;
                            pr=Qry_path(qur[i].x,qur[i].y);
                            ans.push_back(pr.first);
                            //printf("%d
    ",pr.first);
                    }
            }
            while (ans.size())
            {
                    printf("%d
    ",ans[ans.size()-1]);
                    ans.pop_back();
            }
    }
    by mhy12345(http://www.cnblogs.com/mhy12345/) 未经允许请勿转载

    本博客已停用,新博客地址:http://mhy12345.xyz

  • 相关阅读:
    【数据结构】线性表&&顺序表详解和代码实例
    【智能算法】超详细的遗传算法(Genetic Algorithm)解析和TSP求解代码详解
    【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
    【智能算法】迭代局部搜索(Iterated Local Search, ILS)详解
    10. js时间格式转换
    2. 解决svn working copy locked问题
    1. easyui tree 初始化的两种方式
    10. js截取最后一个斜杠后面的字符串
    2. apache整合tomcat部署集群
    1. apache如何启动
  • 原文地址:https://www.cnblogs.com/mhy12345/p/4109052.html
Copyright © 2011-2022 走看看