2594: [Wc2006]水管局长数据加强版
S
C
SC
SC省
M
Y
MY
MY市有着庞大的地下水管网络,嘟嘟是
M
Y
MY
MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从
x
x
x处送往
y
y
y处,嘟嘟需要为供水公司找到一条从
A
A
A至
B
B
B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于
M
Y
MY
MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
不妨将
M
Y
MY
MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。
Input
输入文件第一行为
3
3
3个整数:
N
,
M
,
Q
N, M, Q
N,M,Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
以下
M
M
M行,每行
3
3
3个整数
x
,
y
x, y
x,y和
t
t
t,描述一条对应的水管。
x
x
x和
y
y
y表示水管两端结点的编号,
t
t
t表示准备送水所需要的时间。我们不妨为结点从
1
1
1至
N
N
N编号,这样所有的
x
x
x和
y
y
y都在范围
[
1
,
N
]
[1, N]
[1,N]内。
以下
Q
Q
Q行,每行描述一项任务。其中第一个整数为
k
k
k:若
k
=
1
k=1
k=1则后跟两个整数
A
A
A和
B
B
B,表示你需要为供水公司寻找一条满足要求的从
A
A
A到
B
B
B的水管路径;若
k
=
2
,
k=2,
k=2,则后跟两个整数
x
x
x和
y
y
y,表示直接连接
x
x
x和
y
y
y的水管宣布报废(保证合法,即在此之前直接连接
x
x
x和
y
y
y尚未报废的水管一定存在)。
Output
按顺序对应输入文件中每一项 k = 1 k=1 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
N \leq 1000
N≤1000
M ≤ 100000 M \leq 100000 M≤100000
Q ≤ 100000 Q \leq 100000 Q≤100000
测试数据中宣布报废的水管不超过 5000 5000 5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点 A A A必有至少一条水管路径通往任一结点 B B B。
【加强版数据范围】
N ≤ 100000 N \leq 100000 N≤100000
M ≤ 1000000 M \leq 1000000 M≤1000000
Q ≤ 100000 Q \leq 100000 Q≤100000
任何时候我们考虑的水管网络都是连通的,即从任一结点 A A A必有至少一条水管路径通往任一结点 B B B。
题意
- 就是给你一张带权且无重边,自环的无向图,然后有两种操作,一种是删除某一条一定存在的边,另一种是查询从 A A A走到 B B B的所有路径中最大边权最小的那个
题解
- 其实这题和BZOJ3669差不多,都是求路径上最大边权最小
- 考虑离线下来倒着做,那么对于每一个 2 2 2操作相当于加边操作,然后用 L C T LCT LCT去维护这棵最小生成树,注意不要像我一样用 L C T LCT LCT去跑剩下边的最小生成树,实际上只需要 K r u s k a l Kruskal Kruskal去跑,跑的时候如果顺带把边加到 L C T LCT LCT上即可,因为 L C T LCT LCT肯定没有 K r u s k a l Kruskal Kruskal优秀
- 然后这一题卡 m a p map map,先改成 u n o e d e r e d _ m a p unoedered\_map unoedered_map并且用 u < < 32 ∣ v u<<32|v u<<32∣v去映射,交上去发现 B Z O J BZOJ BZOJ不支持C++11,然后手写二分找位置才过,可见 m a p map map常数有多大!!(也或许是我的 L C T LCT LCT板子不太行,自带大常数
代码
/**************************************************************
Problem: 2594
User: wzw1105
Language: C++
Result: Accepted
Time:18416 ms
Memory:82092 kb
****************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+1e5+10;
const int maxm=1e6+10;
namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend){
p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1){IOerror=1;return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline bool read(double &x){
bool sign=0; char ch=nc(); x=0;
for(;blank(ch);ch=nc());
if(IOerror) return false;
if(ch=='-')sign=1,ch=nc();
for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if(ch=='.'){
double tmp=1;ch=nc();
for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
}
if(sign)x=-x;return true;
}
template<typename T>
inline bool read(T &x){
bool sign=0; char ch=nc(); x=0;
for(;blank(ch);ch=nc());
if(IOerror) return false;
if(ch=='-')sign=1,ch=nc();
for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if(sign)x=-x; return true;
}
inline bool read(char *s){
char ch=nc();
for(;blank(ch);ch=nc());
if(IOerror) return false;
for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0;return true;
}
};
using namespace IO;
namespace LCT{
int ch[maxn][2],fa[maxn],mark[maxn];
long long val[maxn],ans[maxn];
inline bool not_root(int x) {return ch[fa[x]][0]==x||ch[fa[x]][1]==x;}
inline int dir(int x) {return ch[fa[x]][1]==x;}
inline void add_mark(int x) {swap(ch[x][0],ch[x][1]);mark[x]^=1;}
inline void push_down(int x) {
if(mark[x]) {
if(ch[x][0]) add_mark(ch[x][0]);
if(ch[x][1]) add_mark(ch[x][1]);
mark[x]=0;
}
}
inline void push_up(int x) {
ans[x]=x;
if(val[ans[ch[x][0]]]>val[ans[x]]) ans[x]=ans[ch[x][0]];
if(val[ans[ch[x][1]]]>val[ans[x]]) ans[x]=ans[ch[x][1]];
}
inline void pushall(int x) {
if(not_root(x)) pushall(fa[x]);
push_down(x);
}
inline void rotate(int x){
int y=fa[x],z=fa[y],k=dir(x);
if(ch[x][k^1]) fa[ch[x][k^1]]=y;ch[y][k]=ch[x][k^1];
if(not_root(y)) ch[z][dir(y)]=x;fa[x]=z;
ch[x][k^1]=y;fa[y]=x;
push_up(y);push_up(x);
}
inline void splay(int x,int goal=0) {
pushall(x);
while(not_root(x)) {
int y=fa[x],z=fa[y];
if(not_root(y)) {
if(dir(x)==dir(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
}
inline void access(int x) { //从原树的根向x拉一条实链
for(int y=0;x;y=x,x=fa[x]) {
splay(x);ch[x][1]=y;push_up(x);
}
}
inline int find_root(int x) { //找到x在原树中的根
access(x);splay(x);
while(ch[x][0]) push_down(x),x=ch[x][0];
splay(x);return x;
}
inline void make_root(int x) { //使x成为splay的根
access(x);splay(x);add_mark(x);
}
inline void split(int x,int y) { //拉出一条x->y的实链,y为splay根
make_root(x);access(y);splay(y);
}
inline bool link(int x,int y) { //连接x与y,若已经在同一颗原树中,返回0
make_root(x);
if(find_root(y)==x) return 0;
fa[x]=y;return 1;
}
inline bool cut(int x,int y) {
make_root(x);
if(find_root(y)!=x||fa[y]!=x||ch[y][0]) return 0;
fa[y]=ch[x][1]=0;
push_up(x);
return 1;
}
inline long long query_max(int l,int r) {
split(l,r);
return ans[r];
}
};
using namespace LCT;
namespace dsu{
int f[maxn];
void init(int k=0) {for(int i=1;i<=k;i++) f[i]=i;}
int fin(int k) {return f[k]==k?k:(f[k]=fin(f[k]));}
void unite(int a,int b) {
int x=fin(a),y=fin(b);
if(x==y) return;
f[x]=y;
}
bool same(int a,int b) {return fin(a)==fin(b);}
};
using namespace dsu;
struct operation {int opt,u,v; }o[(int)(1e5+10)];
struct edge{
int u,v,w;
edge(int a=0,int b=0,int c=0) {
u=a,v=b,w=c;
}
friend bool operator<(const edge &a,const edge &b) {
if(a.u==b.u) return a.v<b.v;
return a.u<b.u;
}
}e[maxm],rest[maxm];
bool cmp(const edge &a,const edge &b) {return a.w<b.w;}
long long res[maxm];
int n,m,q,tot=0,qwq=0;
bool del[maxm];
int id(int u,int v)
{
int l=1,r=m,ans;
while(l<=r) {
int mid=(l+r)>>1;
if(e[mid].u>u) r=mid-1;
else if(e[mid].u<u) l=mid+1;
else {
if(e[mid].v>v) r=mid-1;
else if(e[mid].v<v) l=mid+1;
else return mid;
}
}
return -1;
}
int main()
{
read(n);read(m);read(q);
for(int i=1,u,v,w;i<=m;i++) {
read(e[i].u);read(e[i].v);read(e[i].w);
if(e[i].u>e[i].v) swap(e[i].u,e[i].v);
}
sort(e+1,e+m+1);
for(int i=1;i<=q;i++) {
read(o[i].opt);read(o[i].u);read(o[i].v);
if(o[i].u>o[i].v) swap(o[i].u,o[i].v);
if(o[i].opt==2) del[id(o[i].u,o[i].v)]=1;
}
for(int i=1;i<=m;i++) if(!del[id(e[i].u,e[i].v)]) rest[++tot]=e[i];
init(n+m+10);
sort(rest+1,rest+tot+1,cmp);
int cnt=0;
for(int i=1;i<=tot&&cnt!=n-1;i++) {
if(!same(rest[i].u,rest[i].v)) {
cnt++;int k=id(rest[i].u,rest[i].v);
unite(rest[i].u,rest[i].v);
val[k+n]=rest[i].w;
link(rest[i].u,n+k);
link(rest[i].v,n+k);
}
}
for(int i=q;i>=1;i--) {
if(o[i].opt==1) {
res[++qwq]=val[query_max(o[i].u,o[i].v)];
}else {
if(same(o[i].u,o[i].v)) {
int x=query_max(o[i].u,o[i].v);
int pos=id(o[i].u,o[i].v);
if(val[x]>e[pos].w){
cut(x,e[x-n].u);cut(x,e[x-n].v);
val[pos+n]=e[pos].w;
link(pos+n,o[i].u);link(pos+n,o[i].v);
}
}else {
int pos=id(o[i].u,o[i].v);
val[n+pos]=e[pos].w;
link(n+pos,o[i].u);link(n+pos,o[i].v);
unite(o[i].u,pos+n);
unite(o[i].v,pos+n);
}
}
}
for(int i=qwq;i>=1;i--) printf("%lld\n",res[i]);
}