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

4 5 3 5
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

Sold.
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

多组测试数据。第一行是4个整数N,M,C,T——计算机的数量1<=N=8000,电缆的数量0<=M<=100000,公司的数量1<=C<=100,电缆销售/购入的数量0<=T<=100000。
接下来M行,每行三个整数Sj1,Sj2,Kj,代表这条电缆连接的两个服务器Sj1,Sj2以及电缆所属的公司Kj。初始状态是遵守规则的。
接下来行,每行3个整数Si1,Si2,Ki,表示公司购入了连接S1,S2的电缆。
4个0标志着测试文件的结束。

Output

对于每个测试数据,输出行:
         “No Such Cable.” 如果这对服务器之前没有被一对电缆相连
         “Already owned.” 如果这对电缆本来就属于这家公司
         “Frobidden: monopoly.” 如果公司Ki已经有2条电缆与S1或S2相连
         “Forbidden: redundant.” 如果公司Ki公司购入这条电缆后,会在其网络中形成环
         “Sold” 操作成功
每组测试数据之后要输出一个空行

Sample Input

4 5 3 5
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

Sold.
Already owned.
Forbidden: monopoly.
Forbidden: redundant.
No such cable.
Already owned.

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的更多相关文章

  1. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  4. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  5. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  6. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  7. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  8. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  9. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  10. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. java14-4 Pattern和Matcher类的使用

     获取功能  Pattern和Matcher类的使用  模式和匹配器的基本使用顺序 import java.util.regex.Matcher; import java.util.regex.Pat ...

  2. xshell5 启动显示 mfc110.dll msvcp110.dll 未找到问题 解决办法

    1. 安装 Visual C++ Redistributable for Visual Studio 2012 x86版本 注意: 一定要安装x86版本.(xshell5是32位的程序) 微软的官方下 ...

  3. Saltstack-进阶篇

    查看minion端的文件内容 [root@linux-node2 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 1 ...

  4. velocity .vm

    关于.vm 后缀的文件,是velocity的文件.velocity是基于java的一种页面模板引擎,支持#if #else #foreach等写法的前台文件.$link.contextPath是该引擎 ...

  5. [VIM] 格式化代码

    快速使用vim格式化代码 在vim的编辑模式i下直接ESC退出道命令模式之后直接敲入如下命令: gg=G        将全部代码格式化 nG=mG    将第n行到第m行的代码格式化 注:如果ESC ...

  6. Eclipse系列: 在Eclipse中用TODO标签管理任务(Task)(ZZ)

    Elipse为Java项目的时候,有一个很人性化的"任务管理"功能,利用这个功能可以方便地将项目中一些需要处理的任务记录下来.先来看看"任务管理"是怎么使用的吧 ...

  7. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  8. Github克隆别人的库

    一. 首先在网站上进入别人的库(通过别人提供的链接或者自己在页面上查询),然后在右下方选择SSH,将链接复制下来. 二. 在你的电脑上新建一个与人家库名相同的文件夹,然后在文件夹上右击,在弹出菜单上选 ...

  9. 在coding上添加ssh-key

    第一步:检查有没有ssh-key 第二步:生成ssh-key 第三步:添加到coding上或者Github上. ls -al ~/.ssh ssh-keygen -t rsa -C "you ...

  10. IOS开发之——Masonry 只支持OC,暂不支持swift

    前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...