【BZOJ-3651&3081】网络通信&StrangeRegulations Link-Cut-Tree
3651: 网络通信
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 90 Solved: 61
[Submit][Status][Discuss]
Description
有一个由M 条电缆连接的 N 个站点组成的网络。为了防止垄断,由 C 个公司控制所有的电缆,规定任何公司不能控制连接同一个站点的两条以上的电缆(可以控制两条)。同时规定,每个公司不能有多余的电缆,所谓的多余,是指属于同一个公司的电缆不能形成环。
在运作过程中,不同公司之间会进行电缆买卖。请你写一个程序判断买卖是否合法。
Input
输入第一行有4个由空格隔开的整数 N,M,C和 T。N(1≤N≤ 8 000)表示站点数,M(0≤M≤100 000)表示连接站点的电缆数。C(1≤C≤ 100)表表示公司数量,T 表示电缆买卖次
数。后面有M行,每行三个整数Sj1,Sj2和Kj,表示连接站点Sj1和Sj2(1≤Sj1< Sj2 ≤ n)的电缆属于Kj(1≤Kj≤C)公司拥有,任意两个站点只有一条直接相连的电缆,输入状态合法。最后T(0≤T≤100 000)行,每行三个整数 Si1, Si2和 Ki,表示 Ki公司想购买站点Si1和Si2之间的电缆。
Output
输出共 T行,表示处理的结果,有以下几种可能的结果:
1、“No such cable.” 两个站点间没有电缆。
2、 “Already owned.” 电缆己经是 Ki 公司控制。
3、 “Forbidden: monopoly.” Ki 公司己经控制了两条连接 Si1 或 Si2 的电缆。
4、 “Forbidden: redundant.” Ki 公司控制的线路会出现环。
5、 “Sold.” 可以买卖。
Sample Input
1 2 1
2 3 1
3 4 2
1 4 2
1 3 3
1 2 3
1 2 3
1 4 3
2 3 3
2 4 3
Sample Output
Already owned.
Forbidden: monopoly.
Forbidden: redundant.
No such cable.
HINT
Source
3081: [Cerc2011]Strange Regulations
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 37 Solved: 14
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 1
2 3 1
3 4 2
1 4 2
1 3 3
1 2 3
1 2 3
1 4 3
2 3 3
2 4 3
2 1 1 1
1 2 1
1 2 1
0 0 0 0
Sample Output
Already owned.
Forbidden: monopoly.
Forbidden: redundant.
No such cable.
HINT
Source
Solution
P3651:
裸LCT,多了一点点处理,我们近似把题意理解为染色
首先用map记录每个边的颜色情况,再用一个数组记录每个点的每种颜色的度,最后每个颜色开一个n个点的LCT维护即可
那么讨论一下即可:
利用map里的信息可以得到Already owned.或No such cable.
利用记录度的数组可以得到Forbidden: monopoly.
设更改前的颜色为c1,更改后的为c2,如果c2中两点在同一个联通块中,则为Forbidden: redundant.
否则为Sold.并cut掉c1中的,link起来c2中的即可
要注意的是,把LCT写到struct里,要用构造函数进行初始化!
P3081:
3651原题,多组数据,注意每次处理即可
Code
p3651
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define N 8010
int n,m,c,t; int colout[N][];
map<int,int>color[N];
struct LCTnode{
int fa[N],son[N][],s[N],val[N],size[N];bool rev[N];
LCTnode(){memset(fa,,sizeof(fa));memset(son,,sizeof(son));memset(s,,sizeof(s));
memset(val,,sizeof(val));memset(size,,sizeof(size));memset(rev,,sizeof(rev));}
inline bool is_root(int x){return !fa[x]||son[fa[x]][]!=x&& son[fa[x]][]!=x;}
inline void rev1(int x){if (!x)return; swap(son[x][],son[x][]);rev[x]^=;}
void pb(int x){if (rev[x]) rev1(son[x][]),rev1(son[x][]),rev[x]=;}
inline void update(int x){size[x]=size[son[x][]]++size[son[x][]];}
inline void rotate(int x)
{
int y=fa[x],w=son[y][]==x;son[y][w]=son[x][w^];
if (son[x][w^]) fa[son[x][w^]]=y;
if (fa[y])
{
int z=fa[y];
if (son[z][]==y) son[z][]=x;
else if (son[z][]==y) son[z][]=x;
}
fa[x]=fa[y]; fa[y]=x; son[x][w^]=y; update(y);
}
inline void splay(int x)
{
int top=,i=x,y; s[]=i;
while (!is_root(i)) s[++top]=i=fa[i];
while (top) pb(s[top--]);
while (!is_root(x))
{
y=fa[x];
if (!is_root(y))
if ((son[fa[y]][]==y)^(son[y][]==x)) rotate(x);else rotate(y);
rotate(x);
}
update(x);
}
inline void access(int x){for (int y=; x; y=x,x=fa[x])splay(x),son[x][]=y,update(x);}
inline void makeroot(int x){access(x),splay(x),rev1(x);}
inline void link(int x,int y){makeroot(x); fa[x]=y; access(x);}
inline void cutf(int x){access(x),splay(x);fa[son[x][]]=; son[x][]=; update(x);}
inline void cut(int x,int y){makeroot(x); cutf(y);}
inline void split(int x,int y){makeroot(x);access(y);splay(y);}
inline int find(int x){access(x);splay(x);while (son[x][])x=son[x][];return x;}
}col[];
//Link Cut Tree
int main()
{
n=read(),m=read(),c=read(),t=read();
for (int u,v,w,i=; i<=m; i++)
{
u=read(),v=read(),w=read(); if(u>v)swap(u,v);
color[u][v]=w; colout[u][w]++; colout[v][w]++;
col[w].link(u,v);
}
for (int x,y,z,i=; i<=t; i++)
{
x=read(),y=read(),z=read(); if (x>y)swap(x,y);
if (color[x][y]==z) {puts("Already owned.");continue;}
if (!color[x][y]) {puts("No such cable.");continue;}
if (colout[x][z]== || colout[y][z]==) {puts("Forbidden: monopoly.");continue;}
if (col[z].find(x)==col[z].find(y)) {puts("Forbidden: redundant.");continue;}
puts("Sold."); col[color[x][y]].cut(x,y); col[z].link(x,y);
colout[x][color[x][y]]--; colout[y][color[x][y]]--;
colout[x][z]++;colout[y][z]++;color[x][y]=z;
}
return ;
}
Creation August长者的模拟题...但是沙茶没调出来,大概是因为没有想到用map之类的去辅助处理,只想着一味的用LCT去解决全部问题...
还差的远呢!
【BZOJ-3651&3081】网络通信&StrangeRegulations Link-Cut-Tree的更多相关文章
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
随机推荐
- java 12-2 String和StringBuffer之间的转换
为什么我们要讲解类之间的转换: A -- B的转换 我们把A转换为B,其实是为了使用B的功能. B -- A的转换 我们可能要的结果是A类型,所以还得转回来. String和StringBuffer的 ...
- 判断变量是否为json对象
var m ={a:'A'}; if(typeof m == 'object' && JSON.stringify(m).indexOf('{') == 0){//判断变量m是不是js ...
- 事故记录-过多进程致使CPU卡死
早上到公司,发现邮箱内有报警信息显示CPU和IO使用都已超标.报警内容如下:主机: test-server-192.168.1.18 时间: 2015.11.15 15:25:17状态: PROBLE ...
- Dottrace跟踪代码执行时间
当自己程序遇到性能问题,比如请求反应缓慢,怎么分析是哪里出了问题呢?dottrace可以帮助.net程序跟踪出代码里每个方法的执行时间,这样让我们更清晰的看出是哪里执行时间过长,然后再分析应该怎样解决 ...
- Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
我用过的编辑器不少,真不少- 但却没有哪款让我特别心仪的,直到我遇到了 Sublime Text 2 !如果说“神器”是我能给予一款软件最高的评价,那么我很乐意为它封上这么一个称号.它小巧绿色且速度非 ...
- OpenShift
一步一脚印 停停走走,回头看看 博客园 首页 新随笔 联系 订阅 管理 随笔 - 24 文章 - 8 评论 - 2 调戏OpenShift:一个免费能干的云平台 一.前因后果 以前为了搞微信的 ...
- 流程引擎Activiti系列:如何将kft-activiti-demo-no-maven改用mysql数据库
kft-activiti-demo-no-maven这个工程默认使用h2数据库,这是一个内存数据库,每次启动之后都要重新对数据库做初始化,很麻烦,所以决定改用mysql,主要做3件事情: 1)在mys ...
- ubuntu16.04下安装jdk和android studio
1首先要在JDK官网下载对应的Linux的JDK版本.进入该网站后,先选择Accept License Agreement然后即可下载.本人的Linux系统为ubuntukylin 16.04 64 ...
- [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构
10.2 How would you design the data structures for a very large social network like Facebook or Linke ...
- vs2010 “SetScrollSizes”: 找不到标识符
SetScrollSizes是CScrollView的成员函数,你的view类必须从CScrollView派生