luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
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的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短).
题解:将所有操作逆着来,把删边看作加边,动态维护一下最小生成树即可.
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 | #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() { int x=0,f=1; char c=nc(); while (c<48) { if (c== '-' ) f=-1; c=nc();} while (c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x*f;} void setIO(string s) { string in=s+ ".in" ; string out=s+ ".out" ; freopen (in.c_str(), "r" ,stdin); freopen (out.c_str(), "w" ,stdout); } struct Union { int p[maxn]; inline void init() { for ( int i=0;i<maxn;++i) p[i]=i; } inline int find( int x) { return p[x]==x?x:p[x]=find(p[x]); } inline int merge( int x, int y) { int a=find(x),b=find(y); if (a==b) return 0; p[a]=b; return 1; } }con; struct LCT { #define lson ch[x][0] #define rson ch[x][1] #define isrt(x) (!(ch[f[x]][1]==x || ch[f[x]][0]==x)) #define get(x) (ch[f[x]][1]==x) int ch[maxn][2], maxv[maxn], rev[maxn], sumv[maxn], val[maxn], sta[maxn], idx[maxn], f[maxn]; inline void mark( int x) { if (!x) return ; swap(lson,rson), rev[x]^=1; } inline void pushup( int x) { if (!x) return ; idx[x]=x, maxv[x]=val[x]; if (maxv[lson] > maxv[x]) idx[x] = idx[lson], maxv[x]=maxv[lson]; if (maxv[rson] > maxv[x]) idx[x] = idx[rson], maxv[x]=maxv[rson]; sumv[x]=sumv[lson]+sumv[rson]+val[x]; } inline void pushdown( int x) { if (!x) return ; if (rev[x]) mark(lson), mark(rson), rev[x]^=1; } inline void rotate( int x) { int old=f[x],fold=f[old], which=get(x); if (!isrt(old)) ch[fold][ch[fold][1]==old]=x; ch[old][which]=ch[x][which^1], f[ch[old][which]]=old; ch[x][which^1]=old, f[old]=x, f[x]=fold; pushup(old), pushup(x); } inline void splay( int x) { int u=x,v=0,fa; for (sta[++v]=u;!isrt(u);u=f[u]) sta[++v]=f[u]; while (v) pushdown(sta[v--]); for (u=f[u];(fa=f[x])!=u;rotate(x)) if (f[fa]!=u) rotate(get(fa)==get(x)?fa:x); } inline void Access( int x) { int t=0; while (x) { splay(x), rson=t, pushup(x), t=x,x=f[x]; } } inline void MakeRoot( int x) { Access(x), splay(x), mark(x); } inline void split( int x, int y) { MakeRoot(x), Access(y), splay(y); } inline void cut( int x, int y) { MakeRoot(x), Access(y), splay(y); ch[y][0]=f[x]=0; pushup(y); } inline void link( int x, int y) { MakeRoot(x), f[x]=y; } }tr; int n,m,Q,eds; int from[maxn],to[maxn], cap[maxn], answer[maxn]; map< int , int >ck[maxn]; struct OPT { int o, x,y,eds, id, ans; }opt[maxn]; int main() { int i,j; setIO( "input" ); con.init(); n=rd(),m=rd(),Q=rd(); for (i=1;i<=m;++i) { ++eds; from[eds]=rd(), to[eds]=rd(), cap[eds]=rd(); ck[from[eds]][to[eds]]=ck[to[eds]][from[eds]]=eds; } for (i=1;i<=Q;++i) { opt[i].o=rd(); if (opt[i].o==1) opt[i].x=rd(), opt[i].y=rd(); if (opt[i].o==2) { int a,b; a=rd(),b=rd(); opt[i].eds = ck[a][b]; ck[a][b]=ck[b][a]=0; } } for (i=1;i<=eds;++i) { if (ck[from[i]][to[i]]) { ++Q; opt[Q].o=2; opt[Q].eds=ck[from[i]][to[i]]; } } for (i=Q;i>=1;--i) { if (opt[i].o==1) { tr.split(opt[i].x,opt[i].y); opt[i].ans=tr.maxv[opt[i].y]; } if (opt[i].o==2) { int cur=opt[i].eds,c,_new; int x=from[cur], y=to[cur], cc=cap[cur],old; if (con.merge(x, y)) { _new=cur + n; tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur]; tr.link(_new, x), tr.link(_new, y); } else { tr.split(x,y); c=tr.idx[y]; old=tr.val[c]; if (cc < old) { int a=from[c-n], b=to[c-n]; tr.cut(a, c), tr.cut(b, c); _new=cur+n; tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur]; tr.link(_new, x), tr.link(_new, y); } } } } for (i=1;i<=Q;++i) if (opt[i].o==1) printf ( "%d\n" ,opt[i].ans); return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库