ZOJ 3261 Connections in Galaxy War 并查集
来源:互联网 发布:西恩潘 知乎 编辑:程序博客网 时间:2024/06/06 00:29
将操作序列反向,用并查集维护集合里最强的星球。
--------------
const int maxn=11000;const int maxm=22000;const int maxq=55000;int n,m,Q;int p[maxn];int a[maxn];int id[maxn];int pa[maxn];void makeset(int n){ for (int i=0;i<=n;i++) pa[i]=i;}int findset(int x){ if (x!=pa[x]) pa[x]=findset(pa[x]); return pa[x];}void unionset(int x,int y){ x=findset(x); y=findset(y); if (x!=y) { pa[x]=y; if (a[x]>a[y]){ a[y]=a[x]; id[y]=id[x]; } else if (a[y]==a[x]&&id[x]<id[y]){ id[y]=id[x]; } }}struct Dat{ int tp,x,y;};Dat q[maxq];Dat e[maxm];vector<int>ans;set< PII >seto;char s[10];int main(){ int cas=0; while (~scanf("%d",&n)){ if (cas) printf("\n"); cas++; rst(q); rst(e); ans.clear(); seto.clear(); makeset(n); for (int i=0;i<n;i++){ scanf("%d",&p[i]); a[i]=p[i]; id[i]=i; } scanf("%d",&m); for (int i=0;i<m;i++) scanf("%d%d",&e[i].x,&e[i].y); scanf("%d",&Q); for (int i=0;i<Q;i++){ scanf("%s",s); if (s[0]=='d'){ q[i].tp=0; scanf("%d%d",&q[i].x,&q[i].y); seto.insert(make_pair(q[i].x,q[i].y)); seto.insert(make_pair(q[i].y,q[i].x)); } else if (s[0]=='q'){ q[i].tp=1; scanf("%d",&q[i].x); } } for (int i=0;i<m;i++){ if (seto.find(make_pair(e[i].x,e[i].y))==seto.end()){ unionset(e[i].x,e[i].y); } } for (int i=Q-1;i>=0;i--){ if (q[i].tp){ int fa=findset(q[i].x); if (a[fa]>p[q[i].x]) ans.push_back(id[fa]);// printf("%d\n",id[fa]); else ans.push_back(-1);// printf("-1\n"); } else{ unionset(q[i].x,q[i].y); } } for (int i=sz(ans)-1;i>=0;i--){ printf("%d\n",ans[i]); } //printf("\n"); }return 0;}
--------------
0 0
- zoj 3261 Connections in Galaxy War(逆向并查集)
- ZOJ 3261 Connections in Galaxy War(并查集)
- ZOJ 3261 Connections in Galaxy War【并查集】
- ZOJ 3261 Connections in Galaxy War (并查集)
- zoj 3261 - Connections in Galaxy War(并查集)
- ZOJ 3261 Connections in Galaxy War 并查集
- Zoj 3261 Connections in Galaxy War【逆序并查集】
- ZOJ-3261-Connections in Galaxy War [逆向并查集]
- Zoj 3563 Connections in Galaxy War 逆向并查集
- zoj Connections in Galaxy War(逆向并查集)
- [并查集+路径压缩]zoj 3261:Connections in Galaxy War
- ZOJ Problem Set - 3261 Connections in Galaxy War(并查集)
- Zoj 3261 Connections in Galaxy War (逆向并查集)
- zoj 3261 Connections in Galaxy War 删边并查集模板
- ZOJ 3261Connections in Galaxy War 反向用并查集
- zoj 3261 Connections in Galaxy War(并查集+离线逆向操作)
- ZOJ-3261(Connections in Galaxy War)——并查集
- ZOJ 3261 Connections in Galaxy War (离线处理+逆向并查集)
- 《汇编语言》王爽版重点摘要-----第五章
- Android NDK开发(2)----- JNI多线程
- jstat命令用法
- STL之map容器
- HDU4442
- ZOJ 3261 Connections in Galaxy War 并查集
- LPC1788 EMWIN
- RLwrap的安装和配置
- HDU 1157(水题)
- replace into用法
- 华为2011软件开发校园测试机试题--身份证号码合法性判断
- 工欲善其事,必先利其器 – 网络抓包
- osg每一练——start 序
- Android NDK开发(1)----- Java与C互相调用实例详解