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处,嘟嘟需要为供水公司找到一 ...
随机推荐
- PureMVC(JS版)源码解析(五):SimpleCommand类
之前我们对PureMVC中涉及到观察者模式的三个基本类(Notification/Observer/Notifier)进行了分析,接下来将对PureMVC源码中的其他类进行分析,首先我们讲 ...
- 字符串右移n位(C++实现)
字符串右移n位(C++实现): // ShiftNString.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <i ...
- sublime 2如何进入vim模式
点击菜单栏[Preferences]——[Settings - Defaults] 查找: "ignored_packages": ["Vintage"] 改为 ...
- ssh 无密码登录远程服务器
在讲下文之前,我都默许大家都已经生成了自己的ssh公钥和密钥,在自己的~/.ssh 目录下面,如果没有,请使用 ssh-keygen -t rsa -C "你的邮箱" 命令生成 1 ...
- IIS配置不正确可能导致“远程服务器返回错误: (404) 未找到"错误一例。
今天上传附件出现了下图所示的问题: 查找百度发现http://www.cnblogs.com/chuncn/archive/2009/09/08/1562759.html 文中提的比较靠谱. 但是,设 ...
- 对于百川SDK签名验证的问题
SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...
- Java 读取Properties 配置文件
方法一,使用 io 包中的 BufferedInputStream 以及 FileInputStream读入文件转成字符流,然后使用 lang 包中 的 Properties 的 load 方法进行读 ...
- SQL函数大全(字符串函数).
SQL Server 2005 函数大全 字符串函数 字符串函数 SubString在SQL和C#中不同, 一,select substring('abcde',-1,3) select LEN( ...
- github 上传或删除 文件 命令
git clone https://github.com/onionhacker/bananaproxy.git cd ~/../.. git config --global user.email & ...
- matlab中max的用法
C = max(A) 返回一个数组各不同维中的最大元素.如果A是一个向量,max(A)返回A中的最大元素.如果A是一个矩阵,max(A)将A的每一列作为一个向量,返回一行向量包含了每一列的最大元素. ...