【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, ...
随机推荐
- css3高级运动keyframes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# lambda表达式及初始化器
using System;using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1d { c ...
- EasyUI Tree判断节点是否是叶
方法1: $('#domaincatalog').tree('isLeaf', node.target); 返回true或false ,true表示是叶节点, false即不是 方法2:官方文档中: ...
- C#实现对指定文件夹中文件按修改时间排序
string path = "~/Document/Introduction/团队管理制度/"; DirectoryInfo dirinfo = new Di ...
- python 操作注册表
import win32api import win32con keyname = r'Software\Microsoft\Internet Explorer\Main' page = 'www.l ...
- TDD(测试驱动开发)培训录(转)
本文转载自:http://www.cnblogs.com/whitewolf/p/4205761.html 最近也在了解TDD,发现这篇文章不错,特此转载一下. TDD(测试驱动开发)培训录 2015 ...
- Matlab txt内容替换函数 fgetl fseek
Data Import and Export :Low-Level File I/O the contents of the file: 16 5 9 4 2 ...
- [CareerCup] 4.9 All Paths Sum 所有路径和
4.9 You are given a binary tree in which each node contains a value. Design an algorithm to print al ...
- 20135316王剑桥 linux第十二周课实验笔记
第十二章并发编程 1.如果逻辑控制流在时间上重叠,那么它们就是并发的.这种现象,称为并发(concurrency). 2.为了允许服务器同时为大量客户端服务,比较好的方法是:创建并发服务器,为每个客户 ...
- Opencv step by step - 基本数据类型
CvArr,CvMat,IplImage这三者是继承的关系. 打开opencv 3.0的源码: cvArr /* CvArr* is used to pass arbitrary * array-l ...