【BZOJ1018】[SHOI2008]堵塞的交通
【BZOJ1018】[SHOI2008]堵塞的交通
题面
洛谷
题解
菊队讲要用线段树维护连通性,但是好像没人写
解法一
将所有的加边删边离线,然后以最近删除时间为边权,$LCT$维护最大生成树即可
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 100005;
const int INF = 1e9;
struct Node { int fa, ch[2], v, d; bool rev; } t[MAX_N << 2];
int stk[MAX_N << 2], top;
void pushup(int x) {
t[x].d = x;
if (t[t[x].d].v > t[t[t[x].ch[0]].d].v) t[x].d = t[t[x].ch[0]].d;
if (t[t[x].d].v > t[t[t[x].ch[1]].d].v) t[x].d = t[t[x].ch[1]].d;
}
void pushrev(int x) {
t[x].rev ^= 1;
swap(t[x].ch[0], t[x].ch[1]);
}
void pushdown(int x) {
if (!t[x].rev) return ;
if (t[x].ch[0]) pushrev(t[x].ch[0]);
if (t[x].ch[1]) pushrev(t[x].ch[1]);
t[x].rev = 0;
}
int get(int x) { return t[t[x].fa].ch[1] == x; }
bool isroot(int x) { return (t[t[x].fa].ch[1] != x) && (t[t[x].fa].ch[0] != x); }
void rotate(int x) {
int k = get(x), y = t[x].fa, z = t[y].fa;
if (!isroot(y)) t[z].ch[get(y)] = x;
t[x].fa = z;
t[t[x].ch[k ^ 1]].fa = y, t[y].ch[k] = t[x].ch[k ^ 1];
t[x].ch[k ^ 1] = y, t[y].fa = x;
pushup(y), pushup(x);
}
void splay(int x) {
stk[top = 1] = x;
for (int i = x; !isroot(i); i = t[i].fa) stk[++top] = t[i].fa;
for (int i = top; i; i--) pushdown(stk[i]);
while (!isroot(x)) {
int y = t[x].fa;
if (!isroot(y)) (get(x) ^ get(y)) ? rotate(x) : rotate(y);
rotate(x);
}
}
void access(int x) { for (int y = 0; x; y = x, x = t[x].fa) splay(x), t[x].ch[1] = y, pushup(x); }
int findroot(int x) { access(x); splay(x); while (t[x].ch[0]) pushdown(x), x = t[x].ch[0]; return x; }
void makeroot(int x) { access(x); splay(x); pushrev(x); }
void split(int x, int y) { makeroot(x); access(y); splay(y); }
void link(int x, int y) { makeroot(x); t[x].fa = y; }
void cut(int x, int y) { split(x, y); t[y].ch[0] = t[x].fa = 0, pushup(y); }
int N, C, tot;
struct Opt { int t, op, x1, x2, del; } a[MAX_N << 1];
map<pair<int, int>, int> mp;
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
freopen("cpp.out", "w", stdout);
#endif
C = gi();
for ( ; ; ) {
char ch[10]; scanf("%s", ch);
if (ch[0] == 'E') break;
int op, r1 = gi() - 1, c1 = gi(), r2 = gi() - 1, c2 = gi(), x1 = r1 * C + c1, x2 = r2 * C + c2;
if (x1 > x2) swap(x1, x2);
if (ch[0] == 'O') op = 0;
if (ch[0] == 'C') op = 1;
if (ch[0] == 'A') op = 2;
++N;
if (op == 0) mp[pair<int, int>(x1, x2)] = N, a[N].del = INF;
if (op == 1) a[mp[pair<int, int>(x1, x2)]].del = N;
a[N].t = N, a[N].op = op, a[N].x1 = x1, a[N].x2 = x2;
}
tot = 2 * C;
for (int i = 0; i <= tot; i++) t[i].v = INF;
for (int i = 1; i <= N; i++) {
int x1 = a[i].x1, x2 = a[i].x2, del = a[i].del;
if (a[i].op == 0) {
if (findroot(x1) == findroot(x2)) {
split(x1, x2); int d = t[x2].d;
if (t[d].v >= del) continue;
else cut(x1, d), cut(x2, d);
t[++tot].v = del;
link(x1, tot), link(x2, tot);
}
else t[++tot].v = del, link(x1, tot), link(x2, tot);
}
if (a[i].op == 1)
if (findroot(x1) == findroot(x2)) {
split(x1, x2);
int d = t[x2].d;
if (t[d].v > a[i].t) continue;
cut(x1, d), cut(x2, d);
}
if (a[i].op == 2) {
if (findroot(x1) == findroot(x2)) puts("Y");
else puts("N");
}
}
return 0;
}
解法二
没打,但是可以参考这篇文章
【BZOJ1018】[SHOI2008]堵塞的交通的更多相关文章
- [bzoj1018][SHOI2008]堵塞的交通traffic_线段树
bzoj-1018 SHOI-2008 堵塞的交通traffic 参考博客:https://www.cnblogs.com/MashiroSky/p/5973686.html 题目大意:有一天,由于某 ...
- bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic
http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 ...
- [BZOJ1018][SHOI2008]堵塞的交通traffic 线段树维护连通性
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MB Submit: 3795 Solved: 1253 [Sub ...
- BZOJ1018 [SHOI2008]堵塞的交通traffic
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- BZOJ1018[SHOI2008]堵塞的交通——线段树
题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路,所以总 ...
- [bzoj1018] [SHOI2008]堵塞的交通
题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个22行CC列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路,所 ...
- 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic
本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...
- 【线段树】bzoj1018 [SHOI2008]堵塞的交通traffic
线段树的每个叶子节点存一列. 每个节点维护六个域,分别是左上左下.左上右上.左上右下.左下右上.左下右下.右上右下在区间内部的连通性,不考虑绕出去的情况. 初始每个叶子的左上左下.右上右下是连通的. ...
- BZOJ1018 SHOI2008堵塞的交通(线段树)
动态图的连通性当然是可以用LCT维护的.但这相当的不优美,毕竟这样做没有用到任何该图的性质,LCT自带的大常数也会使其跑得非常慢. 考虑用线段树维护区间左右端四个点之间各自的连通性(仅经过该区间内路径 ...
- Bzoj1018[SHOI2008]堵塞的交通traffic(线段树)
这题需要维护连通性,看到有连接删除,很容易直接就想LCT了.然而这题点数20w操作10w,LCT卡常估计过不去.看到这个东西只有两行,考虑能否用魔改后的线性数据结构去维护.我想到了线段树. 考虑如果两 ...
随机推荐
- Java中对文件的序列化和反序列化
public class ObjectSaver { public static void main(String[] args) throws Exception { /*其中的 D:\\objec ...
- Semaphore-信号灯机制
我们创建一个可扩展大小的线程池,并且需要在线程池内同时让有限数目的线程并发运行时,就需要用到Semaphore(信号灯机制),Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目 ...
- [翻译] OrigamiEngine
OrigamiEngine https://github.com/ap4y/OrigamiEngine Lightweight iOS and OSX audio engine with opus, ...
- Linux 系统的网络配置文件
系统的网络配置文件 方式一: 界面操作 setup -->界面配置网络,网关等 方式二: 修改配置文件 # 修改配置 vim /etc/sysconfig/network-scripts/ifc ...
- jQuery插件实例二:年华时代插件ReturnTop回到首页
这个插件功能在于当网页内容高度很高时,方便用户快速回到顶部.核心在于对屏幕高度的获取,定时器的使用,在引用代码后,只使用$.nhsd.returnTop();即可实现效果 效果图: 代码: ; fun ...
- IntelliJ IDEA 13.0.2 快捷设置
1.IDEA修改快捷键. 首先打开 File ->Settings -> KeyMap 然后点击 copy 然后就可以自己修改了. 2.全文搜索 位置:Edit-Find-Find ...
- 访问一个HTTPS的网站的大致流程
浏览器向服务器发送请求,请求中包括浏览器支持的协议,并附带一个随机数. 服务器收到请求后,选择某种非对称加密算法,把数字证书签名公钥.身份信息发送给浏览器,同时也附带一个随机数. 浏览器收到后,验证证 ...
- 026.2 网络编程 UDP聊天
实现,通过socket对象 ##############################################################需求建立UDP发送端:###思路:1.建立可以实 ...
- idea html热部署
IDEA SpringBoot 热部署+html修改无需make自动刷新 收藏 HeyS1 发表于 3个月前 阅读 310 收藏 2 点赞 0 评论 0 特邀IBM大咖的主题沙龙,5月20日·北京·3 ...
- 【错误记录】flask mysql 死锁
最近使用flask-sqlalchemy时,进行测试的时候发现日志中打印出了MySql死锁错误,查看Mysql日志发现是因为有俩条sql出现了死锁: Deadlock found when tryin ...