并查集题目集合

畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 64641    Accepted Submission(s): 34587


Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
 

Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
 

Sample Input
 
  
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
 

Sample Output
 
  
1 0 2 998
代码:
#include<stdio.h>
const int MAXN=1010;
int F[MAXN];

int find(int t)
{
 if(F[t]!=t)
 {
   F[t]=find(F[t]);
 }
 return F[t];
}

void mix(int a,int b)
{
    int t1=find(a);
    int t2=find(b);
    if(t1!=t2) F[t1]=t2;
}

int main()
{
    int n,m;

    while(scanf("%d",&n),n)
    {
        scanf("%d",&m);
        for(int i=1;i<=n;i++) F[i]=i;
        int a,b;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            mix(a,b);
        }
        int res=0;
        for(int i=1;i<=n;i++)
          if(F[i]==i) res++;
        printf("%d\n",res-1);///n个点需要n-1条路
    }
    return 0;
}

E. Cyclic Components
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the number of connected components which are cycles.

Here are some definitions of graph theory.

An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.

Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.

A connected component is a cycle if and only if its vertices can be reordered in such a way that:

  • the first vertex is connected with the second vertex by an edge,
  • the second vertex is connected with the third vertex by an edge,
  • ...
  • the last vertex is connected with the first vertex by an edge,
  • all the described edges of a cycle are distinct.

A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.

There are 66 connected components, 22 of them are cycles: [7,10,16][7,10,16] and [5,11,9,15][5,11,9,15].
Input

The first line contains two integer numbers nn and mm (1n21051≤n≤2⋅1050m21050≤m≤2⋅105) — number of vertices and edges.

The following mm lines contains edges: edge ii is given as a pair of vertices viviuiui (1vi,uin1≤vi,ui≤nuiviui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,uivi,ui) there no other pairs (vi,uivi,ui) and (ui,viui,vi) in the list of edges.

Output

Print one integer — the number of connected components which are also cycles.

Examples
input
Copy
5 4
1 2
3 4
5 4
3 5
output
Copy
1
input
Copy
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
output
Copy
2
Note

In the first example only component [3,4,5][3,4,5] is also a cycle.

The illustration above corresponds to the second example.


AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <stack>
#include <queue>
using namespace std;///找出有多少个环
#define ll long long///算出每个顶点的度,只有度等于2的情况下才能成为环。如果两个顶点的度都为2,使用并查集,如果父节点相同,答案加一,否则联立。
int f[200010];
int sum[200010];
int ans;

struct edge
{
    int u,v;
};

int find(int x)///find()朴素递归算法
{
    if(f[x] != x)
    {
        f[x] = find(f[x]);
    }
    return f[x];
}


void unio(int x,int y)
{
    int f1 = find(x);
    int f2 = find(y);
    //cout<<"f("<<x<<")"<<"="<<f[x]<<" "<<"f("<<y<<")"<<"="<<f[y]<<endl;
    if(f1 != f2)
        f[f1] = f2;
    else
        ans++;
}

int main()
{
    int n,m;
    while(cin >> n >> m)
    {
        memset(sum,0,sizeof(sum));
        for(int i = 1;i <= n;i++)
            f[i] = i;
        ans = 0;
        struct edge e[m];
        for(int i = 0;i < m;i++)
        {
            cin >> e[i].u >> e[i].v;
            sum[e[i].u]++;
            sum[e[i].v]++;
        }
        for(int i = 0;i < m;i++)
        {
            if(sum[e[i].u] == 2 && sum[e[i].v] == 2)///如果度为2就和并,最好头尾相连的时候可以判断是否ans+1
                unio(e[i].u,e[i].v);
        }
        cout << ans <<endl;

    }
    return 0;
}

hdu1272:

小希的迷宫

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60597    Accepted Submission(s): 19036


Problem Description
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。

 

Input
输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。
 

Output
对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。
 

Sample Input
 
  
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
 

Sample Output
 
  
Yes Yes No

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int vis[102400];
int f[102400],flag;

int find(int x)
{
 if(f[x]!=x)
   f[x]=find(f[x]);
 return f[x];
}

void mix(int a,int b)
{
 int fx=find(a),fy=find(b);
 if(fx!=fy)
  f[fx]=fy;
 else flag=1;///如果fx==fy,则这两个点有共同祖先,成环!
}

int main()
{
 int n,m,k=0,Max=1;
 for(int i=1;i<=100001;i++)
   f[i]=i;
 while(scanf("%d%d",&n,&m)&&n!=-1||m!=-1)
 {
  if(n==0&&m==0)
  {
    for(int i=1;i<=Max;i++)///判断所有的边是否连在一起
    {
     if(vis[i]&&f[i]==i)
      k++;
    }
    if(k>1)flag=1;
    if(!flag)printf("Yes\n");
    else printf("No\n");
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=Max;i++)
     f[i]=i;
    flag=0;
    k=0;
  }
  else{
   Max=max(Max,max(n,m));
   mix(n,m);
   vis[n]=vis[m]=1;   ///标记
  }
 }
 return 0;
}


转载于:https://www.cnblogs.com/wangtao971115/p/10358383.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值