BZOJ3651网络通信

来源:互联网 发布:编写javascript的软件 编辑:程序博客网 时间:2024/04/28 23:11

3651: 网络通信
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 77 Solved: 54
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.
LCT模板题。。
LCT维护双连通分量模板。。
双倍经验:BZOJ3081
CA互测题。。
在考场上写了个200+的链剖,然后MLE。。
另一种TA爷神奇的做法是splay+启发式合并。。
对于No such cable和Already owned,map一下。。
如果为0——>No such cable
如果为颜色——>Already owned
然后增设变量degree,记录同种颜色边的度数。。
如果==2——>Forbidden: monopoly
然后维护下连通性。。
如果x与y连通——>Forbidden: redundant
如果都不是——>Sold
然后修改。。cut再link
附上本蒟蒻的代码:

#include<cstdio>#include<iostream>#include<cstring>#include<map>using namespace std;#define MAXN 8010int n,m,colornum,T;map<pair<int,int>,int>q;struct kx{    int father[MAXN],c[MAXN][2],st[MAXN],degree[MAXN];    bool rev[MAXN];    kx()      {        memset(father,0,sizeof(father));memset(c,0,sizeof(c));memset(st,0,sizeof(st));        memset(degree,0,sizeof(degree));memset(rev,false,sizeof(rev));      }    bool isroot(int x)      {        return c[father[x]][0]!=x && c[father[x]][1]!=x;      }    void rever(int x)      {        if (!x) return;        swap(c[x][0],c[x][1]),rev[x]^=1;      }    void rotate(int x)      {        int y=father[x],z=father[y],l,r;        if (c[y][0]==x) l=0;        else l=1;        r=l^1;        if (!isroot(y))          if (c[z][0]==y) c[z][0]=x;          else c[z][1]=x;        father[x]=z,father[y]=x,father[c[x][r]]=y,c[y][l]=c[x][r],c[x][r]=y;      }    void pushdown(int x)      {        int l=c[x][0],r=c[x][1];        if (rev[x]) rever(c[x][0]),rever(c[x][1]),rev[x]=0;      }    void splay(int x)      {        int i,top=0,y,z;        st[++top]=x;        for (i=x;!isroot(i);i=father[i]) st[++top]=father[i];        for (i=top;i;i--) pushdown(st[i]);        while (!isroot(x))          {            y=father[x],z=father[y];            if (!isroot(y))              if (c[y][0]==x^c[z][0]==y) rotate(x);              else rotate(y);            rotate(x);          }      }    int root(int x)      {        access(x),splay(x);        while (c[x][0]) x=c[x][0];        return x;      }    void access(int x)      {        int t;        for (t=0;x;t=x,x=father[x]) splay(x),c[x][1]=t;      }    void link(int x,int y)      {        degree[x]++,degree[y]++,access(x),splay(x),rever(x),father[x]=y,access(x);      }    void cut(int x,int y)      {        degree[x]--,degree[y]--,access(x),splay(x),rever(x),access(y),splay(y),c[y][0]=father[c[y][0]]=0;      }    bool ask(int x,int y)      {        return root(x)==root(y);      }}color[110];int read(){    int w=0,f=1; char ch=getchar();    while (ch<'0' || ch>'9')      {        if (ch=='-') f=-1;        ch=getchar();      }    while (ch>='0' && ch<='9')      w=w*10+ch-'0',ch=getchar();    return w*f;}int main(){    int i,x,y,w;    n=read(),m=read(),colornum=read(),T=read();    for (i=1;i<=m;i++)      {        x=read(),y=read(),w=read();        q[make_pair(min(x,y),max(x,y))]=w;        color[w].link(x,y);      }    while (T--)      {        x=read(),y=read(),w=read();        if (q[make_pair(min(x,y),max(x,y))]==0)           {            printf("No such cable.\n"); continue;          }        if (q[make_pair(min(x,y),max(x,y))]==w)          {            printf("Already owned.\n"); continue;          }        if (color[w].degree[x]==2 || color[w].degree[y]==2 || (x==y && color[w].degree[x]!=0))          {            printf("Forbidden: monopoly.\n"); continue;          }        if (color[w].ask(x,y)==1)          {            printf("Forbidden: redundant.\n"); continue;          }        printf("Sold.\n");        int s=q[make_pair(min(x,y),max(x,y))];        color[s].cut(x,y),color[w].link(x,y),q[make_pair(min(x,y),max(x,y))]=w;      }    return 0;}
0 0