嘟嘟嘟




今天复习lct,趁着还年轻多写点数据结构。




首先不得不吐槽一下,题面好长啊……




通过观察发现,\(c \leqslant 10\)。那么就可以暴力的建10棵lct。

接下来说下具体做法:

1.修改点权

在\(c\)棵lct上都改一遍。

2.修改边的颜色。

设原来的颜色为\(i\),改成\(j\)。那么相当于在第\(i\)棵lct上断边,在第\(j\)棵lct上连边。

为了记录每一条边的颜色,我开了一个map<pair<int, int>, int>。这样也能很方便的判断这条边是否存在了。

同时再维护一个cnt数组,cnt[x][i]表示节点\(x\)颜色为\(i\)的出边有多少条,这样Error 1就可以判断了。

至于如何判断Error 2,其实就是判断在第\(j\)棵lct上\(x\)和\(y\)是否联通。

3.查询

没啥好说的。




写起来似乎也不难,记得splay的数组开的是两维就行了。

坑点在于可能会改成相同的颜色,要特判。我虽然想到了,但是忘了这样还得输出Success.,而不是continue……

改完后又因为后面没加“.”调了半天……咋这么zz呢……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m, c, K;
int cnt[maxn][12];
#define pr pair<int, int>
#define mp make_pair
map<pr, int> Map; #define ls t[col][now].ch[0]
#define rs t[col][now].ch[1]
struct Tree
{
int ch[2], fa;
int val, Max, rev;
}t[12][maxn]; In void change(int now, int col)
{
swap(ls, rs); t[col][now].rev ^= 1;
}
In void pushdown(int now, int col)
{
if(t[col][now].rev)
{
if(ls) change(ls, col);
if(rs) change(rs, col);
t[col][now].rev = 0;
}
}
In void pushup(int now, int col)
{
t[col][now].Max = max(max(t[col][ls].Max, t[col][rs].Max), t[col][now].val);
}
In bool n_root(int now, int col)
{
return t[col][t[col][now].fa].ch[0] == now || t[col][t[col][now].fa].ch[1] == now;
}
In void rotate(int x, int col)
{
int y = t[col][x].fa, z = t[col][y].fa, k = (t[col][y].ch[1] == x);
if(n_root(y, col)) t[col][z].ch[t[col][z].ch[1] == y] = x; t[col][x].fa = z;
t[col][y].ch[k] = t[col][x].ch[k ^ 1]; t[col][t[col][y].ch[k]].fa = y;
t[col][x].ch[k ^ 1] = y; t[col][y].fa = x;
pushup(y, col), pushup(x, col);
}
int st[maxn], top = 0;
In void splay(int x, int col)
{
int y = x;
st[top = 1] = y;
while(n_root(y, col)) y = t[col][y].fa, st[++top] = y;
while(top) pushdown(st[top--], col);
while(n_root(x, col))
{
int y = t[col][x].fa, z = t[col][y].fa;
if(n_root(y, col)) rotate(((t[col][z].ch[0] == y) ^ (t[col][y].ch[0] == x)) ? x : y, col);
rotate(x, col);
}
}
In void access(int x, int col)
{
int y = 0;
while(x)
{
splay(x, col); t[col][x].ch[1] = y;
pushup(x, col);
y = x; x = t[col][x].fa;
}
}
In void make_root(int x, int col)
{
access(x, col); splay(x, col);
change(x, col);
}
In int find_root(int x, int col)
{
access(x, col); splay(x, col);
while(t[col][x].ch[0]) pushdown(x, col), x = t[col][x].ch[0];
return x;
}
In void split(int x, int y, int col)
{
make_root(x, col); access(y, col);
splay(y, col);
}
In bool Link(int x, int y, int col)
{
make_root(x, col);
if(find_root(y, col) == x) return 0;
t[col][x].fa = y;
++cnt[x][col], ++cnt[y][col];
return 1;
}
In void Cut(int x, int y, int col)
{
make_root(x, col);
if(find_root(y, col) == x && t[col][x].fa == y && !t[col][x].ch[1])
t[col][x].fa = t[col][y].ch[0] = 0, pushup(y, col);
--cnt[x][col], --cnt[y][col];
}
In int query(int x, int y, int col)
{
make_root(x, col);
if(find_root(y, col) ^ x) return -1;
access(y, col), splay(y, col);
return t[col][y].Max;
} int main()
{
//freopen("ha.in", "r", stdin);
//freopen("ha.out", "w", stdout);
n = read(), m = read(), c = read(), K = read();
for(int i = 1; i <= n; ++i)
{
int x = read();
for(int j = 0; j < c; ++j) t[j][i].val = t[j][i].Max = x;
}
for(int i = 1; i <= m; ++i)
{
int x = read(), y = read(), w = read();
if(x > y) swap(x, y);
Map[mp(x, y)] = w;
Link(x, y, w);
}
for(int i = 1; i <= K; ++i)
{
int op = read();
if(op == 0)
{
int x = read(), y = read();
for(int j = 0; j < c; ++j) splay(x, j), t[j][x].val = t[j][x].Max = y;
}
else if(op == 1)
{
int x = read(), y = read(), w = read();
if(x == y) {continue;}
if(x > y) swap(x, y);
if(!Map.count(mp(x, y))) puts("No such edge.");
else
{
int col = Map[mp(x, y)];
if(col == w) puts("Success.");
else if(cnt[x][w] >= 2 || cnt[y][w] >= 2) puts("Error 1.");
else if(!Link(x, y, w)) puts("Error 2.");
else
{
puts("Success.");
Cut(x, y, col);
Map[mp(x, y)] = w;
}
}
}
else
{
int w = read(), x = read(), y = read();
write(query(x, y, w)), enter;
}
}
return 0;
}

[ZJOI2012]网络的更多相关文章

  1. 洛谷 P2173 [ZJOI2012]网络 解题报告

    P2173 [ZJOI2012]网络 题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环, ...

  2. bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...

  3. 【刷题】BZOJ 2816 [ZJOI2012]网络

    Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...

  4. bzoj2816 [ZJOI2012]网络

    Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf 正解:$link-cut \ tree$. $LCT$板子题,直接维护 ...

  5. bzoj 2816: [ZJOI2012]网络(splay)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2816 [题意] 给定一个无向图,满足条件:从一个节点出发的同色边不超过2条,且不存在同 ...

  6. BZOJ.2816.[ZJOI2012]网络(LCT)

    题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...

  7. Luogu 2173 [ZJOI2012]网络 - LCT

    Solution $LCT$ 直接上$QuQ$ 注意$cut$ 完 需要 $d[u + c * N]--$ 再  $link$,  不然会输出Error 1的哦 Code #include<cs ...

  8. bzoj千题计划223:bzoj2816: [ZJOI2012]网络

    http://www.lydsy.com/JudgeOnline/problem.php?id=2816 每种颜色搞一个LCT 判断u v之间有边直接相连: 如果u和v之间有边相连,那么他们的深度相差 ...

  9. ZJOI2012 网络——LCT相关题目

    有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这个图上,你 ...

随机推荐

  1. vim编辑器的设置

    1.vim编辑器设置分为两种设置,临时设置和永久设置 2.临时设置开启和关闭高亮模式(目前高亮模式是开启的) etc/ man.config vim man.config 在文本编辑器中命令行模式下输 ...

  2. xshell工具source导入几个G的数据库

    直奔主题 xshell工具source导入几个G的数据库 1.先把sql文件通过ftp或者winscp上传到服务器对应站点根目录,如图所示 2.进入xshell界面,进入数据库之前一定设定编码,否者会 ...

  3. js (jQuery)分组数据

    function getobjArr (data) { var result = []; data.HELMET.system = '系统分类' // console.log(data) $.each ...

  4. CentOS7 离线安装MySQL

    1.删除原有的mariadb 不然安装报错 rpm -qa|grep mariadb rpm -e --nodeps mariadb-libs 2. 下载RPM安装包 在https://dev.mys ...

  5. Java Filter防止sql注入攻击

    原理,过滤所有请求中含有非法的字符,例如:, & < select delete 等关键字,黑客可以利用这些字符进行注入攻击,原理是后台实现使用拼接字符串,案例:某个网站的登入验证的SQ ...

  6. 理解PeopleSoft集成代理(Integration Broker)-第1部分

    PeopleSoft 集成代理对于那些刚开始开发PeopleSoft的工程师来说是模糊的,因此,本文的目的是帮助哪些想要了解Peoplesoft集成代理的人. 介绍PeopleSoft集成代理 peo ...

  7. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  8. 浅谈Kotlin(三):类

    浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 前言: 已经学习了前 ...

  9. java面试整理(会持续更新..)

    本人出道至今,经历了大大小小百余场战斗,,,下面整理的面试题有些有答案,有些没答案,那个谁说过:"要抱着怀疑的态度去编程,所以,即便有答案,也不一定正确,即便我本地正确,但是由于屏幕前的你和 ...

  10. SQL中常用系统函数

    --1 CONVERT(数据类型,表达式),CAST( 表达式 AS 数据类型) 转变数据类型--将数字转化为字符串SELECT CONVERT(varchar(2),12)+CONVERT(varc ...