需要变形的并查集,这题错了好久,一直没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. Code Forces 652C Foe Pairs

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  3. ArrayList遍历的三种方式 array arrayList转换

    ArrayList遍历的三种方式 - 呵呵静 - 博客园 https://www.cnblogs.com/mjyung/p/6725182.html

  4. Django - Ajax - 参数

    一.Jquery实现Ajax url   type   data   success   error  complete  statusCode {% load staticfiles %} < ...

  5. Day01 html详解

      day01 html详解   1.html的简介     1.1 什么是html?         - HyperText Markup Language:超文本标记语言,网页语言         ...

  6. 【Python】接口自动化测试-Fidder的使用(未完待续……)

    一.fidder一些一定需要掌握的知识. 1.工具简介 2.清屏操作(1中提到了,这里再着重说明下): 3.get和post请求参数相关: 4.会话框(Fidder左侧区域内容解析): 5.Reque ...

  7. Notes模板说明

    模板标题 文件名 模板名 用途 1 活动趋势 Activity.ntf StdActivityTrendsDatabase 记录和报告统计信息,这些统计信息描述用户(客户机)对数据库所在的 Domin ...

  8. PAT 1111 Online Map[Dijkstra][dfs]

    1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...

  9. java操作Word总结

    import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Varia ...

  10. Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表,以及同步和异步执行模式)

    系列文章导航 Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表) Adobe AIR中使用Flex连接Sqlite数据库(2)(添加,删除,修改以及语句参数) Adobe ...