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

题解: 我们考虑倒着加边 类似维护最小生成树那样 如果路径上的最大值大于当前这条边 那么把最大的边用当前边替换掉 然后查询即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**************************************************************
    Problem: 2594
    User: c20161007
    Language: C++
    Result: Accepted
    Time:25896 ms
    Memory:77760 kb
****************************************************************/
  
#include <bits/stdc++.h>
#define ll long long
const int MAXN=1100010;
const int N=1e6+10;
using namespace std;
ll readll(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int readint(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
bool rt[MAXN];int maxx[MAXN],key[MAXN],ch[MAXN][2],pre[MAXN],res[MAXN],n,m,q,cnt;
typedef struct node{
    int u,v,vul,biao;bool tage;
}node;
node d[N];
int ans[MAXN];
node p[MAXN];
int fa[MAXN];
void Treavel(int x)
{
    if(x)
    {
        //cout<<x<<endl;
        Treavel(ch[x][0]);
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d,key=%2d\n",x,ch[x][0],ch[x][1],pre[x],key[x]);
        Treavel(ch[x][1]);
    }
}
void reverse(int r){
    if(!r) return ;
    swap(ch[r][0],ch[r][1]);
    res[r]^=1;
}
void push(int x){
    if(res[x]){
        reverse(ch[x][0]);
        reverse(ch[x][1]);
        res[x]^=1;
    }
}
void up(int x){
    if(key[maxx[ch[x][0]]]>key[maxx[ch[x][1]]]) maxx[x]=maxx[ch[x][0]];
    else maxx[x]=maxx[ch[x][1]];
    if(key[maxx[x]]<key[x]) maxx[x]=x;
}
void P(int x){
    if(!rt[x]) P(pre[x]);
    push(x);
}
void rotate(int x,int kind){
    int y=pre[x];
    ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;
    if(rt[y]) rt[y]=0,rt[x]=1;
    else ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;
    up(y);
}
void splay(int x){
    P(x);
    while(!rt[x]){
        if(rt[pre[x]]) rotate(x,ch[pre[x]][0]==x);
        else{
            int y=pre[x];int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==x) rotate(x,!kind),rotate(x,kind);
            else rotate(y,kind),rotate(x,kind);
        }
    }
    up(x);
}
void access(int x){
    int y=0;
    while(x){
        splay(x);
        if(ch[x][1]) rt[ch[x][1]]=1,pre[ch[x][1]]=x;
        ch[x][1]=y;
        up(x);
        if(y) rt[y]=0;
        y=x;x=pre[x];
    }
    return ;
}
void mroot(int x){
    access(x);
    splay(x);
    reverse(x);
}
void debug(int rp)
{
    mroot(rp);
    printf("root:%d\n",rp);
    Treavel(rp);
}
int querty(int u,int v){
    mroot(u);access(v);
    splay(v);
    return maxx[v];
}
void Link(int u,int v){
    mroot(u);pre[u]=v;
}
void destory(int t,int tt){
    int u=tt;int v=d[t].u;int vv=d[t].v;
    mroot(u);
    access(v);splay(v);
    pre[u]=pre[v]=0;rt[u]=rt[v]=1;ch[v][0]=0;
    up(v);up(u);
    access(vv);splay(vv);
    pre[u]=pre[vv]=0;rt[u]=rt[vv]=1;ch[vv][0]=0;
    up(vv);up(u);  
}
int find1(int x){
    if(fa[x]==x) return x;
    else return fa[x]=find1(fa[x]);
}
bool cmp(node aa,node bb){
    return aa.vul<bb.vul;
}
bool cmp1(node aa,node bb){
    if(aa.vul==bb.vul){
        if(aa.u==bb.u) return aa.v<bb.v;
        return aa.u<bb.u;
    }
    return aa.vul<bb.vul;
}
bool cmp2(node aa,node bb){
    if(aa.u==bb.u) return aa.v<bb.v;
    return aa.u<bb.u;
}
bool operator<(node aa,node bb){
    if(aa.u==bb.u) return aa.v<bb.v;
    return aa.u<bb.u;
}
int main(){
    ios::sync_with_stdio(false);
    //freopen("tube3.in","r",stdin);
    //freopen("123.in","w",stdout);
    n=readint();m=readint();q=readint();cnt=0;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=n+m;i++){
        res[i]=key[i]=pre[i]=ch[i][0]=ch[i][1]=0;rt[i]=1;
        maxx[i]=i;
    }
    for(int i=1;i<=m;i++){
        d[i].u=readint();d[i].v=readint();d[i].vul=readint();
        if(d[i].u>d[i].v) swap(d[i].u,d[i].v);
    }
    sort(d+1,d+m+1,cmp1);
    for(int i=1;i<=m;i++){
        key[i+n]=d[i].vul;d[i].biao=i;maxx[i+n]=i+n;
    }
    sort(d+1,d+m+1,cmp2);
    for(int i=1;i<=q;i++){
        p[i].vul=readint();p[i].u=readint();p[i].v=readint();
        if(p[i].u>p[i].v) swap(p[i].u,p[i].v);
        if(p[i].vul==2){
            node temp;temp.u=p[i].u;temp.v=p[i].v;temp.biao=0;temp.vul=0;
            int k=lower_bound(d+1,d+1+m,temp)-d;d[k].tage=1;
            p[i].biao=d[k].biao;
        }
    }
    int pp=0;
    sort(d+1,d+m+1,cmp1);
    for(int i=1;i<=m;i++){
        if(d[i].tage==1) continue;
        int t1=find1(d[i].u);int t2=find1(d[i].v);
        if(t1!=t2){
            fa[t1]=t2;
            Link(d[i].u,i+n);
            Link(d[i].v,i+n);
            pp++;
            if(pp==n-1) break;
        }
    }
    int u=0;
    for(int i=q;i>=1;i--){
        if(p[i].vul==1) ans[++u]=key[querty(p[i].u,p[i].v)];
        else{
            int uu=p[i].u;int vv=p[i].v;
                int tt=querty(uu,vv);int t=key[tt];
                if(t>d[p[i].biao].vul){
                    destory(tt-n,tt);
                    Link(uu,p[i].biao+n),Link(vv,p[i].biao+n);                   
                }
        }
    }
    for(int i=u;i>=1;i--) printf("%d\n",ans[i]);
    return 0;
}

 

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

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

posted @   wang9897  阅读(93)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示