1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <bits/stdc++.h> #define il inline
int n,m,q; char z[9];
il int Id(int x,int y){if (x>y) std::swap(x,y); return x?(x<2?2+y:3+y):y;}
namespace Sub1 { const int N=105; int b[N],g[N][N]; struct node{int x,y; il friend bool operator < (node x,node y){return g[x.x][x.y]>g[y.x][y.y];}}; std::priority_queue<node> h[6];
il void main() { int u,v,w; node x; while (m--) scanf("%d%d%d",&u,&v,&w),(!g[u][v]||g[u][v]>w)&&(g[u][v]=g[v][u]=w); for (u=1; u<=n; u++) for (v=1; v<=n; v++) if (g[u][v]) h[0].push({u,v}); for (scanf("%d",&q); q; q--) if (scanf("%s",z),z[0]=='M') { scanf("%d",&u),b[u]=z[4]-65; for (v=1; v<=n; v++) if (g[u][v]) h[Id(b[u],b[v])].push({u,v}); } else { for (u=Id(z[3]-65,z[4]-65); h[u].size(); h[u].pop()) if (x=h[u].top(),Id(b[x.x],b[x.y])==u){printf("%d\n",g[x.x][x.y]); break;} if (!h[u].size()) puts("No Found!"); } } }
namespace Sub2 { const int N=1e5+5,M=10*N;
int b[N],to[M],nx[M],wt[M],hd[N],sze=1; std::multiset<int> S[6]; bool o[N],O[M]; struct TREE { int fa[N],fv[N]; std::multiset<int> s[N][3];
il void Dfs(int u) { o[u]=1; int i,v; for (i=hd[u]; i; i=nx[i]) if (!o[v=to[i]]&&!O[i>>1]) O[i>>1]=1,fa[v]=u,fv[v]=wt[i],Dfs(v); }
il void Init() { memset(o,0,n+1); int i; for (i=1; i<=n; i++) if (!fa[i]) Dfs(i); for (i=1; i<=n; i++) if (fa[i]) s[fa[i]][0].insert(fv[i]); for (i=1; i<=n; i++) if (s[i][0].size()) S[0].insert(*s[i][0].begin()); }
il void Chg(int c,int x) { if (b[x]==c) return; int i,j; if (fa[x]) { for (i=0; i<3; i++) if (s[fa[x]][i].size()) j=Id(b[fa[x]],i),S[j].erase(S[j].find(*s[fa[x]][i].begin())); s[fa[x]][b[x]].erase(s[fa[x]][b[x]].find(fv[x])),s[fa[x]][c].insert(fv[x]); for (i=0; i<3; i++) if (s[fa[x]][i].size()) S[Id(b[fa[x]],i)].insert(*s[fa[x]][i].begin()); } for (i=0; i<3; i++) if (s[x][i].size()) j=Id(b[x],i),S[j].erase(S[j].find(*s[x][i].begin())),S[Id(c,i)].insert(*s[x][i].begin()); } }T[3];
il void Add(int u,int v,int w){to[++sze]=v,nx[sze]=hd[u],wt[sze]=w,hd[u]=sze;}
il void main() { int u,v,w; while (m--) scanf("%d%d%d",&u,&v,&w),Add(u,v,w),Add(v,u,w); for (w=0; w<3; w++) T[w].Init(); for (scanf("%d",&q); q; q--) if (scanf("%s",z),z[0]=='M') scanf("%d",&u),w=z[4]-65,T[0].Chg(w,u),T[1].Chg(w,u),T[2].Chg(w,u),b[u]=w; else S[w=Id(z[3]-65,z[4]-65)].size()?(printf("%d\n",*S[w].begin())):(puts("No Found!")); } }
int main() { scanf("%d%d",&n,&m);
if (n<101) Sub1::main(); else Sub2::main();
return 0; }
|