【BZOJ3651】网络通信

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

这里写图片描述

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#define MAXN 800010
#define GET (ch>='0'&&ch<='9')
#define num(x,c) (x+n*c)
using namespace std;
int n,m,c,T;
void in(int &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
int sta[MAXN],top;
int cnt[8010][110];
map<int,int> col[MAXN];
struct splay
{
    int ch[2],fa,val;
    bool rev;
}tree[MAXN];
void push_down(int x)
{
    if (tree[x].rev)
    {
        tree[tree[x].ch[0]].rev^=1;tree[tree[x].ch[1]].rev^=1;
        swap(tree[x].ch[0],tree[x].ch[1]);tree[x].rev^=1;
    }
}
inline bool is_root(int x)  {   return tree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;    }
inline void rot(int x)
{
    int y=tree[x].fa,z=tree[y].fa,l=(tree[y].ch[1]==x),r=l^1;
    if (!is_root(y))    tree[z].ch[tree[z].ch[1]==y]=x;
    tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z;
    tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;
}
void Splay(int x)
{
    top=0;sta[++top]=x;
    for (int i=x;!is_root(i);i=tree[i].fa)  sta[++top]=tree[i].fa;
    while (top) push_down(sta[top--]);
    while (!is_root(x))
    {
        int y=tree[x].fa,z=tree[y].fa;
        if (!is_root(y))
        {
            if ((tree[y].ch[0]==x)^(tree[z].ch[0]==y))  rot(x);
            else    rot(y);
        }
        rot(x);
    }
}
inline void access(int x)   {   for (int i=0;x;i=x,x=tree[x].fa)    Splay(x),tree[x].ch[1]=i;   }
inline int find(int x)  {   access(x);Splay(x);for (;tree[x].ch[0];x=tree[x].ch[0]);return x;   }
inline void make_root(int x)    {   access(x);Splay(x);tree[x].rev^=1;  }
inline void link(int x,int y)   {   make_root(x);tree[x].fa=y;  }
inline void split(int x,int y)  {   make_root(x);access(y);Splay(y);    }
inline void cut(int x,int y)    {   make_root(x);access(y);Splay(y);tree[x].fa=tree[y].ch[0]=0; }
void modify(int x,int y,int c)
{
    if (x>y) swap(x,y);
    int t=col[x][y];
    if (!t) {   puts("No such cable.");return;  }
    if ((--t)==c)   {   puts("Already owned.");return;  }
    if (cnt[x][c]==2||cnt[y][c]==2) {   puts("Forbidden: monopoly.");return;    }
    int u=num(x,t),v=num(y,t),a=num(x,c),b=num(y,c);
    if (find(a)==find(b))   {   puts("Forbidden: redundant.");return;   }
    cut(u,v);link(a,b);cnt[x][t]--;cnt[y][t]--;cnt[x][c]++;cnt[y][c]++;col[x][y]=c+1;
    puts("Sold.");
}
int main()
{
    in(n);in(m);in(c);in(T);int u,v,w,t1,t2;
    while (m--)
    {
        in(u);in(v);in(w);w--;
        if (u>v) swap(u,v);col[u][v]=w+1;
        t1=num(u,w);t2=num(v,w);link(t1,t2);
        cnt[u][w]++;cnt[v][w]++;
    }
    while (T--) in(u),in(v),in(w),w--,modify(u,v,w);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值