bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版
Time Limit: 25 Sec Memory Limit: 128 MB
Submit: 934 Solved: 291
[Submit][Status]
Description
Input
Output
Sample Input
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
Sample Output
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真神奇)
#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=;
while (ch=getchar(),ch<'' || ch>'');
do
x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x;
}
int n,m,q;
int ch[MAXT][],pnt[MAXT];
int mx[MAXT],mt[MAXT];
int val[MAXT],siz[MAXT],pos[MAXT];
bool rev[MAXT];
int stack[MAXT],tops=-;
inline bool is_root(int now)
{
return !pnt[now] || (ch[pnt[now]][]!=now && ch[pnt[now]][]!=now);
}
inline void update(int now)
{
siz[now]=siz[ch[now][]]+siz[ch[now][]]+;
if (mx[ch[now][]]>mx[ch[now][]])
{
mx[now]=mx[ch[now][]];
mt[now]=mt[ch[now][]];
}else
{
mx[now]=mx[ch[now][]];
mt[now]=mt[ch[now][]];
}
if (mx[now]<val[now])
{
mx[now]=val[now];
mt[now]=now;
}
}
inline void reverse(int now)
{
if (!now)return ;
swap(ch[now][],ch[now][]);
rev[now]^=;
}
inline void down(int now)
{
if (rev[now])
{
reverse(ch[now][]);
reverse(ch[now][]);
rev[now]=;
}
}
inline void rotate(int now)
{
register int p=pnt[now],anc=pnt[p];
register int dir=ch[p][]==now;
if (!is_root(pnt[now]))
ch[anc][ch[anc][]==p]=now;
pnt[now]=anc;
if (ch[now][dir])
pnt[ch[now][dir]]=p;
ch[p][-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][]==p) == (ch[p][]==now))
rotate(p),rotate(now);
else
rotate(now),rotate(now);
}
}
int access(int now)
{
int son=;
while (now)
{
splay(now);
ch[now][]=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][]=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][]==y)
{
splay(y);
ch[y][]=pnt[x]=;
update(y);
}else if (ch[x][]==y)
{
ch[x][]=pnt[y]=;//注意清空pnt[]
update(x);
}else throw ;
}
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-);
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k;
scanf("%d%d%d",&n,&m,&q);
aaa at;
for (i=;i<=n;i++)uf[i]=i;
for (i=;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+]=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=;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==)
{
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=;
for (it1=S.begin();it1!=S.end();it1++)
{
e[m++]=*it1;
}
sort(e,e+m,cmp_d);
for (i=;i<m;i++)
{
if (comb(e[i].x,e[i].y))
{
add_edge(e[i].x,n+e[i].id+);
add_edge(e[i].y,n+e[i].id+);
}
}
for (i=q-;i>=;i--)
{
if (qur[i].t==)
{
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++n);
erase_edge(el[pr.second].y,pr.second++n);
add_edge(qur[i].x,qur[i].id+n+);
add_edge(qur[i].y,qur[i].id+n+);
}else
{
pair<int,int> pr;
pr=Qry_path(qur[i].x,qur[i].y);
ans.push_back(pr.first);
//printf("%d\n",pr.first);
}
}
while (ans.size())
{
printf("%d\n",ans[ans.size()-]);
ans.pop_back();
}
}
bzoj 2594: [Wc2006]水管局长数据加强版 动态树的更多相关文章
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- 【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- bzoj 2594: [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】
题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 (LCT维护最小生成树)
离线做,把删边转化为加边,那么如果加边的两个点不连通,直接连就行了.如果联通就找他们之间的瓶颈边,判断一下当前边是否更优,如果更优就cut掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...
- bzoj 2594 [Wc2006]水管局长数据加强版(LCT+最小生成树)
[深坑勿入] [给个链接] http://blog.csdn.net/popoqqq/article/details/41348549 #include<cstdio> #include& ...
- 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
随机推荐
- 像C++一样写JavaScript
像C++一样写JavaScript C/C++/Java的include或import可以引用第3方文件和包. 这个功能在Html/Js里没有默认的实现. 假设我们有这样一个HTML文件index.h ...
- Ⅲ.AngularJS的点点滴滴-- 路由
路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...
- ASP.NET性能优化之分布式Session
如果我们正在使用Session,那么构建高性能可扩展的ASP.NET网站,就必须解决分布式Session的架构,因为单服务器的SESSION处理能力会很快出现性能瓶颈,这类问题也被称之为Session ...
- Managing Group Policy with PowerShell
Overview In this article, I’ll talk about your options when it comes to managing Group Policy using ...
- 数字证书简介及Java编码实现
1.数字证书简介 数字证书具备常规加密解密必要的信息,包含签名算法,可用于网络数据加密解密交互,标识网络用户(计算机)身份.数字证书为发布公钥提供了一种简便的途径,其数字证书则成为加密算法以及公钥的载 ...
- volatile用处说明
在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前 ...
- Java-struts2 之值栈问题
这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...
- python中文件类的应用
python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件: 1) os.mknod("test.txt") 创建空文件 2) open("tes ...
- hibernate - Initial SessionFactory creation failed.org.hibernate.HibernateException
对于数据库字段映射, 被映射的字段除了提供 getter方法之外, 还需要提供setter方法. 这也是java bean的一些规范. 例如, 题目中的异常 Initial SessionFactor ...
- oracle学习笔记3:基本的SQL语句
oracle基本的SQL语句和SQLSERVER基本一样,在这里只简单列出与SQLSERVER不一样的地方 1.select * from orderinfo where address = 'abc ...