BZOJ3651 : 网络通信
同[ZJOI2012]网络,把每个点拆成C个点然后用LCT维护。
#include<cstdio>
#include<map>
#define P make_pair
#define N 800002
using namespace std;
int f[N],d[8002][102],son[N][2],a[N],n;bool rev[N];
map<int,int>co[N];
inline int id(int x,int c){return x+n*c;}
inline void swap(int&a,int&b){int c=a;a=b;b=c;}
inline bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;}
inline void reverse(int x){swap(son[x][0],son[x][1]),rev[x]^=1;}
inline void pb(int x){if(rev[x])reverse(son[x][0]),reverse(son[x][1]),rev[x]=0;}
inline void rotate(int x){
int y=f[x],w=(son[y][1]==x);
son[y][w]=son[x][w^1];
if(son[x][w^1])f[son[x][w^1]]=y;
if(f[y]){
int z=f[y];
if(son[z][0]==y)son[z][0]=x;
if(son[z][1]==y)son[z][1]=x;
}
f[x]=f[y];son[x][w^1]=y;f[y]=x;
}
inline void splay(int x){
int s=1,i=x,y;a[1]=i;
while(!isroot(i))a[++s]=i=f[i];
while(s)pb(a[s--]);
while(!isroot(x)){
y=f[x];
if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
rotate(x);
}
}
inline void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y;}
inline int root(int x){access(x);splay(x);while(son[x][0])x=son[x][0];return x;}
inline void makeroot(int x){access(x);splay(x);reverse(x);}
inline void link(int x,int y){makeroot(x);f[x]=y;access(x);}
inline void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;}
inline void cut(int x,int y){makeroot(x);cutf(y);}
inline void change(int x,int y,int c){
if(x>y)swap(x,y);
int c0=co[x][y];
if(!c0){puts("No such cable.");return;}
c0--;
if(c0==c){puts("Already owned.");return;}
if(d[x][c]==2||d[y][c]==2){puts("Forbidden: monopoly.");return;}
int x0=id(x,c0),y0=id(y,c0),x1=id(x,c),y1=id(y,c);
if(root(x1)==root(y1)){puts("Forbidden: redundant.");return;}
cut(x0,y0);link(x1,y1);
d[x][c0]--,d[y][c0]--;
d[x][c]++,d[y][c]++;
co[x][y]=c+1;
puts("Sold.");
}
inline void read(int&a){char ch;a=0;while(!(((ch=getchar())>='0')&&(ch<='9')));a*=10,a+=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))(a*=10)+=ch-'0';}
int m,c,k,x,y,u,v,w;
int main(){
read(n),read(m),read(c),read(k);
while(m--){
read(u),read(v),read(w);w--;if(u>v)swap(u,v);
co[u][v]=w+1;
x=id(u,w),y=id(v,w);
d[u][w]++,d[v][w]++;
link(x,y);
}
while(k--)read(u),read(v),read(w),change(u,v,w-1);
return 0;
}
BZOJ3651 : 网络通信的更多相关文章
- 【BZOJ3651】网络通信 LCT
[BZOJ3651]网络通信 Description 有一个由M 条电缆连接的 N 个站点组成的网络.为了防止垄断,由 C 个公司控制所有的电缆,规定任何公司不能控制连接同一个站点的两条以上的电缆(可 ...
- 【BZOJ-3651&3081】网络通信&StrangeRegulations Link-Cut-Tree
3651: 网络通信 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 90 Solved: 61[Submit][Status][Discuss] D ...
- ZeroMQ实例-使用ZMQ(ZeroMQ)进行局域网内网络通信
本文内容摘要:1)安装zeromq.2)实例说明使用zmq进行网络间的消息发送和接收 首先在机器中安装zmq库 步骤如下: 1)下载zeromq的源代码,ZeroMQ的官方网址:http://zero ...
- 基础笔记12(socket,url网络通信)
进一步深入socket 1.网络通信条件: .IP地址,可用主机名. .传输数据时将不用的应用程序通过数字标识区分开来,这种标识称为逻辑端口,也称端口.(0-65535端口,一般系统预留0-1024) ...
- Windows Store App 网络通信 HttpWebRequest
如果希望更好地控制HTTP请求,可以使用System.Net类库中的HttpWebRequest类,该类对HTTP协议进行了完整的封装,并且提供了很多对HTTP协议中的 Header.Content和 ...
- socket网络通信
1.socket通常也称作"套接字",用于描述IP地址和端口.在internet上的主机一般运行了多个服务软件,同时提供几种服务,每种服务都打开一个socket,并绑定到一个端口上 ...
- 20145316&20145229实验五:网络通信
20145316&20145229实验五:网络通信 结对伙伴:20145316 博客链接:http://www.cnblogs.com/xxy745214935/p/6130897.html
- 网络通信之Socket与LocalSocket的比较
Socket与LocalSocket都可以实现网络通信,两个有什么区别呢? LocalSocket其通信方式与Socket差不多,只是LocalSocket没有跨越网络边界. 于是,思考到一个问题:a ...
- Android 网络通信API的选择和实现实例
Android开发网络通信一开始的时候使用的是AsyncTask封装HttpClient,没有使用原生的HttpURLConnection就跳到了Volley,随着OkHttp的流行又开始迁移到OkH ...
随机推荐
- C#结构体和类的区别
1.不能在结构体中定义默认构造方法: 2.在结构体中的非默认构造方法中,必须对结构体中所有的字段进行初始化,否则将报错. 3.在类中声明字段的同时,可以初始化,字段的值.在结构体中不可以. 4.结构体 ...
- 将linux用在开发环境中
我是如何将linux用在开发环境中的 1.为什么不直接安装Linux在主机 一直想深入学习一下linux的使用,于是将家里的笔记本装了linux系统,但是要将自己的系统打造一个适合开发的环境确实是一件 ...
- 2015安徽省赛 C.LU的困惑
题目描述 Master LU 非常喜欢数学,现在有个问题:在二维空间上一共有n个点,LU每连接两个点,就会确定一条直线,对应有一个斜率.现在LU把平面内所有点中任意两点连线,得到的斜率放入一个集合中( ...
- 试试markdown
看看有没有wrapper... list first list second list third list fourth list in list 1 list list list > < ...
- Serialize and Deserialize Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place. Clarification What is rotated arr ...
- iOS UITableView 的beginUpdates和endUpdates
在官方文档中是这样介绍beginUpdates的 Call this method if you want subsequent insertions, deletion, and selection ...
- Linux下配置Hadoop 1.2.1
首先要下载hadoop的包,版本选择1.2.1的,下载地址为:http://mirrors.cnnic.cn/apache/hadoop/common/hadoop-1.2.1/ 这里可以下载hado ...
- Windows下配置Apache服务器并支持php
php环境的配置相对来说比较繁琐,网上教程大部分都是放一起说,总体感觉比较乱,其实Apache是一款通用的服务器软件,可以用来配置支持静态页面,php.Python.Java甚至asp等服务端语言,要 ...
- js将map转换成数组
/** * map转数组. * * @param {Map}map * map对象 * @return 数组 */ Share.map2Ary = function(map) { var list = ...