【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, ...
随机推荐
- Android的面孔_Actiyity
一.什么是Activity? 简单的说:Activity就是布满整个窗口或者悬浮于其他窗口上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...
- Git技巧总结分享
接触Git有很长一段时间了,从最初的不懂到逐渐熟悉运用,相比于SVN,更热衷于Git这一款强大的版本控制工具. 废话不多说,下面对Git做了一些技巧总结,在此分享下,希望能帮助到一些喜欢Git的朋友们 ...
- struts2验证框架1
<!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理.如果用户需要指定多个请求后缀,则多个后缀之间以英文逗 ...
- 学习node.js 第4篇 建立一个最小的web聊天系统
我们生活在一个实时的世界里,有什么比聊天更加实时吗?那就让我们先写一个基于TCP 的聊天服务器吧,并且支持Telnet 连接.这很容易,而且能够完全用Node来编写.首先,我们需要在Node 中包含T ...
- 如何设置word里的代码格式,使之有底纹的效果
1.测试平台:word2013 2.步骤:设计->底纹->填充->应用于->确定 3.效果:
- [py] os.system os.popen commands 执行shell
1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd) 2.popen可以保存命令结果 pwd=os.popen('pwd').r ...
- [原创]JavaScript继承详解
原文链接:http://www.cnblogs.com/sanshi/archive/2009/07/08/1519036.html 面向对象与基于对象 几乎每个开发人员都有面向对象语言(比如C++. ...
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点
public class UIHelper { /// <summary> /// 在Visual里找到想要的元素 /// childName可为空,不为空就按名字找 /// </s ...
- 解决装系统选中的磁盘采用的是GPT分区形式
今天给服务器重装系统碰到的问题,记录一下 当时是按正常的操作:到了装系统选盘的时候是找不到盘符的,加载了raid驱动,然后顺利找到盘符,然后格式化了以前的C盘, 结果无法选中格式化后的C盘,无法下一步 ...