BZOJ3081Strange Regulations

3081: [Cerc2011]Strange Regulations
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 25 Solved: 11
Description
在一個計算機網絡中,連接兩臺計算機的電纜屬於不同的公司。一項新的反壟斷法規定,一家公司連接同一臺計算機的電纜不能超過兩條。爲了避免資源浪費,另外一條法律規定,一家公司的電纜系統不能有冗餘,即去掉任意一個電纜之後,至少一對之前連通的計算機要斷開連接。由於這些公司不斷的銷售和購入電纜,要確定他們是否遵守這些規則十分的困難。你的任務是寫一個程序完成這個任務
Input
多組測試數據。第一行是4個整數N,M,C,T——計算機的數量1<=N=8000,電纜的數量0<=M<=100000,公司的數量1<=C<=100,電纜銷售/購入的數量0<=T<=100000。
接下來M行,每行三個整數Sj1,Sj2,Kj,代表這條電纜連接的兩個服務器Sj1,Sj2以及電纜所屬的公司Kj。初始狀態是遵守規則的。
接下來行,每行3個整數Si1,Si2,Ki,表示公司購入了連接S1,S2的電纜。
4個0標誌着測試文件的結束。
Output
對於每個測試數據,輸出行:
“No Such Cable.” 如果這對服務器之前沒有被一對電纜相連
“Already owned.” 如果這對電纜本來就屬於這家公司
“Frobidden: monopoly.” 如果公司Ki已經有2條電纜與S1或S2相連
“Forbidden: redundant.” 如果公司Ki公司購入這條電纜後,會在其網絡中形成環
“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
2 1 1 1
1 2 1
1 2 1
0 0 0 0
Sample Output
Sold.
Already owned.
Forbidden: monopoly.
Forbidden: redundant.
No such cable.

Already owned.
同BZOJ3651
多組數據要memset
附上本蒟蒻的代碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
#define MAXN 8010
int 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;
    while (n=read(),m=read(),colornum=read(),T=read())
      {
        if (!n && !m && !colornum && !T) break;
        memset(color,0,sizeof(color));
        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;
          }
        printf("\n");
      }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章