需要变形的并查集,这题错了好久,一直没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变形并查集的更多相关文章

  1. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  2. 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)

    并查集专题训练地址,注册登录了才能看到题目 并查集是一个树形的数据结构,  可以用来处理集合的问题, 也可以用来维护动态连通性,或者元素之间关系的传递(关系必须具有传递性才能有并查集来维护,因为并查集 ...

  3. ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集

    题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...

  4. ZOJ3261 Connections in Galaxy War —— 反向并查集

    题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...

  5. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

  6. ACM_变形课(并查集)

    变形课 Time Limit: 2000/1000ms (Java/Others) Problem Description: 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermio ...

  7. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  8. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  9. zoj3261 带权并查集

    题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现在发生战争,有一些联系通道会被摧毁,而一些星球会通过还没有被摧毁的联系通道直接或者间接联系能够联系到的武力值最高的星球求救,如果有多个武力值都 ...

随机推荐

  1. gcc windows版本

    MingW 分 32位和64位版本:下载地址分别如下: http://sourceforge.net/projects/mingw/ http://sourceforge.net/projects/m ...

  2. PL/SQL编程基础(三):数据类型划分

    数据类型划分 在Oracle之中所提供的数据类型,一共分为四类: 标量类型(SCALAR,或称基本数据类型) 用于保存单个值,例如:字符串.数字.日期.布尔: 标量类型只是作为单一类型的数据存在,有的 ...

  3. beans有无状态

    Spring Bean Scopes https://www.tutorialspoint.com/spring/spring_bean_scopes.htm When defining a < ...

  4. SHFileOperation的用法

    //删除文件或者文件夹bool DeleteFile(char * lpszPath){SHFILEOPSTRUCT FileOp={0};FileOp.fFlags = FOF_ALLOWUNDO ...

  5. 【我的Android进阶之旅】解决AndroidStudio编译时报错:Timeout waiting to lock artifact cache .

    1. 错误描述 今天在Android Studio中,使用gradle命令的时候,出现了如下所示的错误: D:\GitLab Source\XTCLint>gradlew clean uploa ...

  6. 安装Anaconda3

    wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh bash Anaconda3-5.0.1-Linux-x8 ...

  7. Disruptor的伪共享解决方案

    1.术语 术语 英文单词 描述 内存屏障 Memory Barriers 是一组处理器指令,用于实现对内存操作的顺序限制. In the Java Memory Model a volatile fi ...

  8. appfog 添加数据库支持

    1.PhpMyAdmin与app 在同一应用 1.cd进入应用所在的文件夹,输入 git clone git://github.com/appfog/af-php-myadmin.git 2.进入本地 ...

  9. mysql实现开窗函数、Mysql实现分析函数

    关键字:mysql实现开窗函数.Mysql实现分析函数.利用变量实现窗口函数 注意,变量是从左到右顺序执行的 --测试数据 CREATE TABLE `tem` ( `id` ) NOT NULL A ...

  10. 7.如何将python脚本打包为exe形式

    先安装pyinstaller,pip install pyinstaller 然后 pyinstaller -F combine.py打包即可