Connections in Galaxy War (逆向并查集)

本文介绍了一种用于解决图中节点连接问题的算法。通过使用并查集和映射表等数据结构,该算法能够有效地处理节点间的连接和查询操作。文章详细解释了算法的实现过程,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Connections in Galaxy War

  ZOJ - 3261 



从这里学到了这些东西。。


map<int,string>mp[10];   这地方是可以这样用的。。这样就把原本是二维的数组变为了1维,下面也能很好地体现这个例子
    mp[1][2]="adsf";


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

const int MAXN=10010;
int F[MAXN];
int p[MAXN];
int val[MAXN];//最大值的下标
int num[MAXN];//最大值
int find(int x)
{
    if(F[x]==-1)return x;
    return F[x]=find(F[x]);
}
void bing(int u,int v)
{
    int t1=find(u),t2=find(v);
    if(t1!=t2)
    {
        F[t1]=t2;
        if(num[t1]>num[t2])
        {
            num[t2]=num[t1];
            val[t2]=val[t1];
        }
        else if(num[t1]==num[t2] && val[t2]>val[t1])
            val[t2]=val[t1];
    }
}
map<int,int>mp[MAXN];//这个map很好,用一维来判断了某条边是否存在。
struct Edge
{
    int u,v;
}edge[20010];
bool used[20010];
struct Node
{
    int op;
    int u,v;
}node[50010];
int ans[50010];
char str[20];
int main()
{
    int n;
    int Q;
    int m;
    int u,v;
    bool first=true;
    while(scanf("%d",&n)==1)
    {
        if(first)first=false;
        else printf("\n");
        memset(F,-1,sizeof(F));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&p[i]);
            val[i]=i;
            num[i]=p[i];
            mp[i].clear();
        }

        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            if(u>v)swap(u,v);
            mp[u][v]=i;
            edge[i].u=u;
            edge[i].v=v;
            used[i]=false;
        }
        scanf("%d",&Q);
        for(int i=0;i<Q;i++)
        {
            scanf("%s",&str);
            if(str[0]=='q')
            {
                node[i].op=0;
                scanf("%d",&node[i].u);
            }
            else
            {
                node[i].op=1;
                scanf("%d%d",&u,&v);
                if(u>v)swap(u,v);
                node[i].u=u;
                node[i].v=v;
                int tmp=mp[u][v];
                used[tmp]=true;
            }
        }
        for(int i=0;i<m;i++)
          if(!used[i])
          {
              bing(edge[i].u,edge[i].v);
          }
        int cnt=0;
        for(int i=Q-1;i>=0;i--)
        {
            if(node[i].op==0)
            {
                u=node[i].u;
                int t1=find(u);
                if(num[t1]>p[u])ans[cnt++]=val[t1];
                else ans[cnt++]=-1;
            }
            else
            {
                bing(node[i].u,node[i].v);
            }
        }
        for(int i=cnt-1;i>=0;i--)printf("%d\n",ans[i]);
    }
    return 0;
}




### 并查集算法实现集合问题 以下是基于 JavaScript 的并查集算法代码示例,该代码可以用来解决与集合分组、合并和查询相关的问题: #### 初始化并查集类 ```javascript class UnionFind { constructor(n) { this.parent = Array.from({ length: n }, (_, i) => i); // 初始时每个节点的父节点都是自己 this.rank = Array(n).fill(0); // 用于优化树的高度 } find(x) { // 查找操作:找到 x 所属集合的根节点,并进行路径压缩 if (this.parent[x] !== x) { this.parent[x] = this.find(this.parent[x]); } return this.parent[x]; } union(x, y) { // 合并操作:将两个元素所在的集合合并 let rootX = this.find(x); let rootY = this.find(y); if (rootX === rootY) return; // 如果已经在同一个集合中,则无需处理 if (this.rank[rootX] > this.rank[rootY]) { this.parent[rootY] = rootX; } else if (this.rank[rootX] < this.rank[rootY]) { this.parent[rootX] = rootY; } else { this.parent[rootY] = rootX; this.rank[rootX]++; } } countSets() { // 统计当前有多少个独立的集合 const roots = new Set(); for (let i = 0; i < this.parent.length; i++) { roots.add(this.find(i)); } return roots.size; } } ``` 上述代码实现了并查集的核心功能 `find` 和 `union` 方法。其中 `find` 方法负责查找某个元素所属集合的代表元(即根节点),并通过路径压缩优化性能;而 `union` 方法则负责将两个不同集合中的元素合并成一个集合。 #### 使用场景举例 假设我们需要解决一个问题:给定一组城市之间的连接关系,判断这些城市被划分为多少个连通区域。可以通过如下方式调用上面定义好的 `UnionFind` 类来解决问题[^2]: ```javascript function connectedCities(connections, numCities) { const uf = new UnionFind(numCities); // 将所有相连的城市加入同一集合 connections.forEach(([cityA, cityB]) => { uf.union(cityA - 1, cityB - 1); // 假设城市编号从1开始,因此减去1适配数组索引 }); // 返回最终的集合数量 return uf.countSets(); } // 测试数据 const citiesConnections = [ [1, 2], [2, 3], [4, 5] ]; console.log(connectedCities(citiesConnections, 5)); // 输出应为 2 ``` 此函数接收两参数——城市的连接列表以及总共有几个城市。它会创建一个新的并查集实例并将每一对已知相互连接的城市放入相同的集合里。最后计算剩余的不同集合数目作为结果返回。 ### 总结 通过以上方法能够有效利用并查集这一高效工具快速求解涉及动态变化集合归属情况下的各类复杂度较高的组合型难题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值