同[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. UINavigation拖动翻页

    #import <UIKit/UIKit.h> #import "ViewController.h" //window窗口 #define WINDOW [[UIApp ...

  2. HDU1879 kruscal 继续畅通工程

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. HDOJ 1203 I NEED A OFFER!(01背包)

    10397507 2014-03-25 23:30:21 Accepted 1203 0MS 480K 428 B C++ 泽泽 题目链接:http://acm.hdu.edu.cn/showprob ...

  4. django-cms 代码研究(三)插件(plugs in)

    插件(plugs in) djangocms支持的插件有: http://docs.django-cms.org/en/latest/basic_reference/plugin_reference. ...

  5. PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)

    PHP面向对象三大特点学习 学习目标:充分理解抽象.封装.继承.多态   面象对向的三大特点:封装性.继承性.多态性 首先简单理解一下抽象:我们在前面定义一个类的时候,实际上就是把一类事物共有的属性和 ...

  6. 【云计算】Docker云平台—Docker基础

    Docker云平台系列共三讲,此为第一讲:Docker基础 参考资料: Docker官方文档:https://docs.docker.com/ Docker从入门到实践:https://yeasy.g ...

  7. Python发布Django项目的pyc版脚本

    import os import sys from py_compile import compile #print "argvs:",sys.argv if len(sys.ar ...

  8. html css js

    html 回顾 字体:font 属性: color: 颜色 size: 字号 表格:table 标签: tr:表格中的行 td: 单元行中的单元格 th:通常使用在table中的第一行, 成为表头, ...

  9. HDU1130 卡特兰数

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. DOS 循环读取txt每一行内容

    在命令行窗口中输入: for /f %i in (f:\mydata.txt) do echo %i 如果要是写成批处理文件run.bat for /f %%i in (f:\mydata.txt) ...