BZOJ_P3651 网络通信(LinkCutTree)

来源:互联网 发布:开淘宝网店程序 编辑:程序博客网 时间:2024/05/16 18:46

BZOJ传送门

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 71 Solved: 52
[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

Sol:
比较的水,一种颜色一颗LCT,可见写结构的函数的优越感QuQ (其实并不用LCT)

#include<cstdio>#include<algorithm>#include<vector>using namespace std;#define C 102#define N 8005#define M 100005#define lc(o) ch[o][0]#define rc(o) ch[o][1]inline int in(int x=0,char ch=getchar(),int v=1){    while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v;}struct LinkCutTree{    int f[N],ch[N][2],rev[N];    bool isrt(int o){return (!f[o])||(o!=lc(f[o])&&o!=rc(f[o]));}    inline void Updata(int o){if(rev[o]){rev[o]^=1;swap(lc(o),rc(o));rev[lc(o)]^=1,rev[rc(o)]^=1;}}    inline void Push(int o){if(!isrt(o)) Push(f[o]);Updata(o);}    inline void Rot(int o){        int p=f[o],k=f[p],d=lc(p)==o;        if(!isrt(p)) ch[k][rc(k)==p]=o;        f[ch[o][d]]=p;f[o]=k;        ch[p][d^1]=ch[o][d],ch[o][d]=p,f[p]=o;    }    inline void Splay(int o){        int p,k;Push(o);        while(!isrt(o)){            p=f[o],k=f[p];            if(isrt(p)) Rot(o);            else if((rc(p)==o)==(rc(k)==p)) Rot(p),Rot(o);            else Rot(o),Rot(o);        }    }    inline void Access(int o,int x=0){for(;o;o=f[x=o]) Splay(o),rc(o)=x;}    inline void Makert(int o){Access(o),Splay(o),rev[o]^=1;}    inline void Link(int u,int v){Makert(u),f[u]=v;}    inline void Cut(int u,int v){Makert(u),Access(v),Splay(v),f[lc(v)]=0,lc(v)=0;}    inline bool Check1(int u,int v){        Makert(u),Access(v),Splay(v);        for(;u;u=f[u]) if(u==v) return 0;return 1;}}c[C];int n,m,cr,t,cnt;short s[N][C];struct Edge{int u,v,w;}edge[M];vector<int> g[N];inline void Add_Edge(int u,int v,int w){    edge[++cnt]=(Edge){u,v,w};s[u][w]++,s[v][w]++;    g[u].push_back(cnt),g[v].push_back(cnt);}inline int Check(int u,int v){    if(g[u].size()>g[v].size()) swap(u,v);    for(int i=0,lim=g[u].size();i<lim;i++) if(edge[g[u][i]].u==v||edge[g[u][i]].v==v) return g[u][i];    return -1;}void solve(){    n=in(),m=in(),cr=in(),t=in();int u,v,w,w1;    for(int i=1;i<=m;i++){        u=in(),v=in(),w=in();        Add_Edge(u,v,w);c[w].Link(u,v);    }    while(t--){        u=in(),v=in(),w=in();        w1=Check(u,v);        if(w1==-1){puts("No such cable.");continue;}        if(edge[w1].w==w){puts("Already owned.");continue;}        if(s[u][w]>1||s[v][w]>1){puts("Forbidden: monopoly.");continue;}        if(!c[w].Check1(u,v)){puts("Forbidden: redundant.");continue;};        c[edge[w1].w].Cut(u,v);c[w].Link(u,v);puts("Sold.");        s[u][edge[w1].w]--,s[v][edge[w1].w]--;        s[u][w]++,s[v][w]++,edge[w1].w=w;    }}int main(){    solve();    return 0;}
0 0