并查集

并查集是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题。一些常见的用途有求连通子图、求最小生成树的Kruskal算法和最近公共祖先等。。。

最简单的理解就是:朋友的朋友都是我的朋友。

本人写这个并查集,纯粹为了记忆的,可能比较简单。

若需详细一点的话,可参考大佬的博客:https://blog.csdn.net/u013486414/article/details/38682057;

并查集主要分为两步:路径压缩和并查集合并。下面是模板

路径压缩:

数组f初始化:
for(int i=1;i<=n;i++)  f[i]=i;///n为顶点个数,1到n为顶点编号
法1:
int getf(int x){
      if(f[x]==x) return x;
      else return getf(f[x]);
}
法2:
int getf(int x){///查找根节点
     int p=x,t;
     while(f[p]!=p) p=f[p];///返回根节点
     while(x!=p){///路径压缩
       t=f[x];
       f[x]=p;
       p=t;
}
   return x;
}

并查集合并:

法1:
int mergex(int x,int y){
      int fa,fb;
      fa=getf(x);
      fb=getf(y);
      if(fa!=fb){
         f[fa]=fb;///f[fb]=fa同样可以
         return 1;
}
      return 0;
}
法2:
void mergex(int x,int y){
       if((x=getf(x))==(y=getf(y)) return;
       if(a[x]>a[y])  f[y]=x;///数组a初始化为0,记录根节点个数,谁强谁作主。
       else{
          f[x]=y;
          if(a[x]==a[y]) a[y]++;
}
}

Description

这是一道模板题。

维护一个 n 点的无向图,支持:

  • 加入一条连接 u 和 v 的无向边

  • 查询 u 和 v 的连通性

由于本题数据较大,因此输出的时候采用特殊的输出方式:用 0 或 1 代表每个询问的答案,将每个询问的答案依次从左到右排列,把得到的串视为一个二进制数,输出这个二进制数 mod 998244353 的值。

Input

第一行包含两个整数 n, m,表示点的个数和操作的数目。

接下来 m 行每行包括三个整数 op, u, v 。

  • 如果 op=0,则表示加入一条连接 u 和 v 的无向边;

  • 如果 op=1,则表示查询 u 和 v 的连通性。

n ≤ 4000000 , m ≤ 8000000

Output

一行包括一个整数表示答案。

Sample Input

3 6
1 1 0
0 0 1
1 0 1
1 1 2
0 2 1
1 2 1

Sample Output

5

Hint

答案串为 101 。

#include 
#define Mod 998244353
#define ll long long
#include 
#include 
const ll maxn=4e6+10;
ll f[maxn];
ll n,m,u,v,sum;
int op;
void init()
{
    for(ll i=0;i

Description

Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths that currently provide a way to visit each of his N (5 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N). Each and every pasture is home to one cow. FJ plans to remove as many of the P (N-1 ≤ P ≤ 100,000) paths as possible while keeping the pastures connected. You must determine which N-1 paths to keep.

Bidirectional path j connects pastures Sj and Ej (1 ≤ Sj ≤ N; 1 ≤ Ej ≤ NSj ≠ Ej) and requires Lj (0 ≤ Lj ≤ 1,000) time to traverse. No pair of pastures is directly connected by more than one path.

The cows are sad that their transportation system is being reduced. You must visit each cow at least once every day to cheer her up. Every time you visit pasture i (even if you're just traveling through), you must talk to the cow for time Cj(1 ≤ Cj ≤ 1,000).

You will spend each night in the same pasture (which you will choose) until the cows have recovered from their sadness. You will end up talking to the cow in the sleeping pasture at least in the morning when you wake up and in the evening after you have returned to sleep.

Assuming that Farmer John follows your suggestions of which paths to keep and you pick the optimal pasture to sleep in, determine the minimal amount of time it will take you to visit each cow at least once in a day.

Input

* Line 1: Two space-separated integers: N and P

* Lines 2..N+1: Line i+1 contains a single integer: Cj

* Lines N+2..N+P+1: Line N+j+1 contains three space-separated integers: SjEj, and Lj

Output

* Line 1: A single integer, the total time it takes to visit all the cows (including the two visits to the cow in your sleeping-pasture)

Sample Input

5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12

Sample Output

176

/*从某根开始走,每走一条边就要消耗两边点权+边的边权*2。最后再加上一个根的点权,问最少花多少代价。*/
#include 
#include 
using namespace std;
int p[10010];
struct edge{
     int u,v,w;
     bool operator < (const edge &a)const{ return wa[v]) f[v]=u;
     else {
        f[u]=v;
        if(a[u]==a[v]) a[v]++;
     }
}
int main(){
     int n,m;
     scanf("%d%d",&n,&m);
            int ans=9999999;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i]);
             if(ans>p[i]) ans=p[i];
             f[i]=i;
        }
        int a,b,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            e[i].u=a;
            e[i].v=b;
            e[i].w=p[a]+p[b]+2*c;///两边点权+边的边权*2
        }
        sort(e+1,e+1+m);
        for(int i=1;i<=m;i++){
            if(getf(e[i].u)==getf(e[i].v))   continue;
           ans+=e[i].w;
           mergex(e[i].u,e[i].v);
        }

    printf("%d\n",ans);
     return 0;
}

 

你可能感兴趣的:(数据结构)