#include #include #include #include using namespace std; //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) #define debug(fmt, ...) struct edge { int a, b, company; inline edge() { } inline edge(int _a, int _b, int _company) : a(_a), b(_b), company(_company) { } inline bool operator<(const edge &e) const { return a != e.a ? a < e.a : b != e.b ? b < e.b : company < e.company; } }; static int owns[8001][101][2]; static int cnt_own(int srv, int com) { int ret = (owns[srv][com][0] != -1) + (owns[srv][com][1] != -1); debug("company %d has %d cables at server %d\n", com, ret, srv); return ret; } static bool add_own(int srv, int com, int eidx) { if(owns[srv][com][0] == -1) owns[srv][com][0] = eidx; else if(owns[srv][com][1] == -1) owns[srv][com][1] = eidx; else { debug("ERROR: can't add cable %d of company %d to server %d\n", eidx,com,srv); return false; } debug("added cable %d of company %d to server %d\n", eidx,com,srv); return true; } static bool del_own(int srv, int com, int eidx) { if(owns[srv][com][0] == eidx) { owns[srv][com][0] = -1; debug("removed cable %d of company %d from server %d\n", eidx,com,srv); return true; } if(owns[srv][com][1] == eidx) { owns[srv][com][1] = -1; debug("removed cable %d of company %d from server %d\n", eidx,com,srv); return true; } debug("ERROR: can't delete cable %d of company %d to server %d\n", eidx,com,srv); return false; } static void solve() { int n,m,c,t; scanf("%d %d %d %d",&n,&m,&c,&t); if(!n && !m && !c && !t) exit(0); for(int i=0; i<=n; i++) for(int j=0; j<=c; j++) owns[i][j][0] = owns[i][j][1] = -1; vector E(m); for(int i=0; i %d to company %d\n", a,b,k); int idx = lower_bound(E.begin(), E.end(), edge(a,b,-1)) - E.begin(); if(idx == m || E[idx].a != a || E[idx].b != b) { printf("No such cable.\n"); continue; } if(E[idx].company == k) { printf("Already owned.\n"); continue; } if(cnt_own(a, k) == 2 || cnt_own(b, k) == 2) { printf("Forbidden: monopoly.\n"); continue; } if(cnt_own(a, k) == 1 && cnt_own(b, k) == 1) { printf("Forbidden: redundant.\n"); continue; } del_own(a,E[idx].company,idx); del_own(b,E[idx].company,idx); add_own(a,k,idx); add_own(b,k,idx); E[idx].company = k; printf("Sold.\n"); } printf("\n"); } int main(void) { for(;;) solve(); }