同[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 : 网络通信的更多相关文章

  1. 【BZOJ3651】网络通信 LCT

    [BZOJ3651]网络通信 Description 有一个由M 条电缆连接的 N 个站点组成的网络.为了防止垄断,由 C 个公司控制所有的电缆,规定任何公司不能控制连接同一个站点的两条以上的电缆(可 ...

  2. 【BZOJ-3651&3081】网络通信&StrangeRegulations Link-Cut-Tree

    3651: 网络通信 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 61[Submit][Status][Discuss] D ...

  3. ZeroMQ实例-使用ZMQ(ZeroMQ)进行局域网内网络通信

    本文内容摘要:1)安装zeromq.2)实例说明使用zmq进行网络间的消息发送和接收 首先在机器中安装zmq库 步骤如下: 1)下载zeromq的源代码,ZeroMQ的官方网址:http://zero ...

  4. 基础笔记12(socket,url网络通信)

    进一步深入socket 1.网络通信条件: .IP地址,可用主机名. .传输数据时将不用的应用程序通过数字标识区分开来,这种标识称为逻辑端口,也称端口.(0-65535端口,一般系统预留0-1024) ...

  5. Windows Store App 网络通信 HttpWebRequest

    如果希望更好地控制HTTP请求,可以使用System.Net类库中的HttpWebRequest类,该类对HTTP协议进行了完整的封装,并且提供了很多对HTTP协议中的 Header.Content和 ...

  6. socket网络通信

    1.socket通常也称作"套接字",用于描述IP地址和端口.在internet上的主机一般运行了多个服务软件,同时提供几种服务,每种服务都打开一个socket,并绑定到一个端口上 ...

  7. 20145316&20145229实验五:网络通信

    20145316&20145229实验五:网络通信 结对伙伴:20145316 博客链接:http://www.cnblogs.com/xxy745214935/p/6130897.html

  8. 网络通信之Socket与LocalSocket的比较

    Socket与LocalSocket都可以实现网络通信,两个有什么区别呢? LocalSocket其通信方式与Socket差不多,只是LocalSocket没有跨越网络边界. 于是,思考到一个问题:a ...

  9. Android 网络通信API的选择和实现实例

    Android开发网络通信一开始的时候使用的是AsyncTask封装HttpClient,没有使用原生的HttpURLConnection就跳到了Volley,随着OkHttp的流行又开始迁移到OkH ...

随机推荐

  1. 内存不能为read修复方法:(转自:网上(忘记了))

    指令修复法!开始菜单,运行 ,输入cmd, 回车,在命令提示符下输入(复制即可) : for %1 in (%windir%\system32\*.ocx) do regsvr32.exe /s %1 ...

  2. 多层神经网络与C++实现

    BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...

  3. linux rsync +inotify 实现 实时同步

    前言:     rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rs ...

  4. 【云计算】Dockerfile示例模板

    Dockerfile FROM debian:jessie MAINTAINER "Konrad Kleine" USER root ####################### ...

  5. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  6. 不使用arc功能时的编译参数 –fno-objc-arc

    用一些老代码时,里面总有release调用,你需要用上这个标记,不使用arc功能编译代码.我总记不住具体写法,做个记录.

  7. codeforces 467C.George and Job 解题报告

    题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...

  8. 【python】时间性能分析

    参考: http://blog.jobbole.com/47619/ http://chenpeng.info/html/1754 1.计算整个程序运行时间,直接用linux的time命令即可 tim ...

  9. PO/VO/BO等对象模型

    PO :persistent object持久对象 1 .有时也被称为Data对象,对应数据库中的entity,可以简单认为一个PO对应数据库中的一条记录. 2 .在hibernate持久化框架中与i ...

  10. Android Design 4.4中文版发布

    “两年前的今天,我们发布了 Android Design 中文版(旧闻链接). 随着 Android 系统的发展,界面和设计语言都不断的发生变化.韶华易逝.光阴苒冉,Android 进化到了 4.4 ...