zoj3261变形并查集
需要变形的并查集,这题错了好久,一直没a掉,终于在重写第三次的时候a了
先保存数据,把不需要拆分的边合并,逆向计算,需要拆分时就合并,之前不知道为啥写搓了,tle好久
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 100000000
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define MIN(a,b) a<b ? a:b
#pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=; int father[N],power[N];
vector<pair<int,int> >vec;
vector<int>v[N];
stack<int>s; int Find(int x)
{
return x!=father[x] ? Find(x=father[x]) : x;
}
void Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(power[fx]>power[fy])father[fy]=fx;
else if(power[fx]<power[fy])father[fx]=fy;
else
{
if(fx<fy)father[fy]=fx;
else father[fx]=fy;
}
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int n,m,k,c=;
while(~scanf("%d",&n)){
if(c++)puts("");
for(int i=;i<n;i++)
{
scanf("%d",&power[i]);
v[i].clear();
father[i]=i;
}
vec.clear();
scanf("%d",&m);
for(int i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
v[a].push_back(b);
}
scanf("%d",&k);
while(k--){
char p[];
scanf("%s",&p);
if(p[]=='d')
{
int a,b;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
for(int i=;i<v[a].size();i++)
if(v[a][i]==b)
v[a][i]=-;
vec.push_back(make_pair(a,b));
}
else
{
int a;
scanf("%d",&a);
vec.push_back(make_pair(-,a));
}
}
for(int i=;i<n;i++)
for(int j=;j<v[i].size();j++)
if(v[i][j]!=-)
Merge(i,v[i][j]);
for(int i=vec.size()-;i>=;i--)
{
if(vec[i].first==-)
{
int ans=Find(vec[i].second);
if(power[ans]>power[vec[i].second])s.push(ans);
else s.push(-);
}
else Merge(vec[i].first,vec[i].second);
}
while(!s.empty()){
printf("%d\n",s.top());
s.pop();
}
}
return ;
}
zoj3261变形并查集的更多相关文章
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)
并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构, 可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...
- ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集
题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...
- ZOJ3261 Connections in Galaxy War —— 反向并查集
题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...
- B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~
B. Mr. Kitayuta's Colorful Graph -> Link <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...
- ACM_变形课(并查集)
变形课 Time Limit: 2000/1000ms (Java/Others) Problem Description: 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermio ...
- ZOJ-3261 Connections in Galaxy War 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...
- zoj3261 并查集离线处理
Connections in Galaxy War Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- zoj3261 带权并查集
题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现在发生战争,有一些联系通道会被摧毁,而一些星球会通过还没有被摧毁的联系通道直接或者间接联系能够联系到的武力值最高的星球求救,如果有多个武力值都 ...
随机推荐
- MP3 ID3信息编辑器(附源码)
练习WPF时写的编辑MP3 ID3信息的小工具,方便对歌曲的统一管理,同时增加了调用豆瓣音乐API获取专辑信息的功能:1.主界面: 2.没有ID3 信息的MP3歌曲: 3.点击自动获取,从豆瓣音乐获取 ...
- shiro框架的学习
1shiro框架是什么:是一个权限控制的框架2shiro框架有什么作用:权限管理,管理那些资源是否需要登录才能访问.控制某些资源需要那些权限才能访问3shiro框架怎样使用: 1在web.xml配置s ...
- poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4422 Accepted: 1482 D ...
- mysql 约束条件 primary key 主键
primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...
- Openstack(十三)部署管理服务horizon
13.1horizon介绍 horizon是openstack的管理其他组件的图形显示和操作界面,通过API和其他服务进行通讯,如镜像服务.计算服务和网络服务等结合使用,horizon基于python ...
- Parallel Decision Tree
Decision Tree such as C4.5 is easy to parallel. Following is an example. This is a non-parallel vers ...
- js 中的[] {}是什么意思
<div id="aa1">aaaaa</div><div id="aa2">bbbb</div><div ...
- oracle extract 函数简介
oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 //语法如下: EXTRACT ( { YEA ...
- 2017-2018-1 JaWorld 第四、五周作业
2017-2018-1 JaWorld 第四.五周作业 两周工作内容 小组讨论并确定最终的app雏形 合作完成需求说明书 工作分工 成员 分工 比例 陈是奇 1.引言 8% 马平川 2.1-2.5 产 ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Tenth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Tenth Week ...