栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3261 Connections in Galax...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3261 Connections in Galax...

#include<map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 50010;struct Node{ int mark; int x; int y; };Node node[MAXN];Node edge[MAXN];int n , m , q;int val[MAXN];int father[MAXN];int ans[MAXN];map<int , int>mp;void init(){ mp.clear(); for(int i = 0 ; i < n ; i++) father[i] = i;}int find(int x){ if(father[x] != x) father[x] = find(father[x]);  return father[x];}void Union(int x , int y){ int fx = find(x);  int fy = find(y);  if(fx != fy){ if(val[fx] > val[fy]) father[fy] = fx; else if(val[fx] < val[fy]) father[fx] = fy; else{ if(fx < fy)  father[fy] = fx; else father[fx] = fy; } }}void solve(){ for(int i = 0 ; i < m ; i++){ if(mp[edge[i].x*MAXN+edge[i].y]) continue; Union(edge[i].x , edge[i].y); } int pos = 0; for(int i = q-1 ; i >= 0 ; i--){ if(node[i].mark == 0){ int fx = find(node[i].x);  if(val[fx] > val[node[i].x]) ans[pos++] = fx; else ans[pos++] = -1; } else Union(node[i].x , node[i].y); } for(int i = pos-1 ; i >= 0 ; i--) printf("%dn" , ans[i]);}int main(){ int x , y; char str[10]; bool first = true;  while(scanf("%d" , &n) != EOF){ if(first) first = false; else puts(""); for(int i = 0 ; i < n ; i++)  scanf("%d" , &val[i]); init(); scanf("%d" , &m);  for(int i = 0 ; i < m ; i++){ scanf("%d%d" , &edge[i].x , &edge[i].y); if(edge[i].x > edge[i].y) swap(edge[i].x , edge[i].y); } scanf("%d" , &q);  for(int i = 0 ; i < q ; i++){ scanf("%s" , str);  if(str[0] == 'q'){ scanf("%d" , &node[i].x); node[i].mark = 0; } else{ scanf("%d%d" , &node[i].x , &node[i].y); if(node[i].x > node[i].y) swap(node[i].x , node[i].y); node[i].mark = 1; mp[node[i].x*MAXN+node[i].y] = 1; } } solve(); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号