zoukankan      html  css  js  c++  java
  • 【并查集入门专题1】A+B+D 三道模板题 hdu1232 hdu1233 poj2524【并查集模板】

    ~~~~连续三道水题,也只能用提高手速这样理由来安慰自己了,不过,从d题数组超限自己改为边存边合并的方法看来还是有收获吧,只能这样强行“收获了”(泪目)
     
    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 

    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>
    #define N 1100
    
    int u[N],v[N];
    int f[N];
    
    int find(int u)
    {
        if(f[u] == u)
            return u;
        else
        {
            f[u] = find(f[u]);
            return f[u];
        }
    }
    
    void merge(int u,int v)
    {
        int t1,t2;
        t1 = find(u);
        t2 = find(v);
        if(t1 != t2)
            f[t1] = t2;
        return ;
    }
    
    int main()
    {
        int n,m;
        while(scanf("%d",&n),n!=0)
        {
            scanf("%d",&m);
            for(int i = 1; i <= n; i ++)
                f[i] = i;
            for(int i = 1; i <= m; i ++)
                scanf("%d%d",&u[i],&v[i]);
            for(int i = 1; i <= m; i ++)
                merge(u[i],v[i]);
            int count = 0;
            for(int i = 1; i <= n; i ++)
                if(f[i] == i)
                {
                    count ++;
                }
                    
            printf("%d
    ",count-1);
        }
        return 0;
    }
    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。 

    Input测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。 
    当N为0时,输入结束,该用例不被处理。 
    Output对每个测试用例,在1行里输出最小的公路总长度。 
    Sample Input

    3
    1 2 1
    1 3 2
    2 3 4
    4
    1 2 1
    1 3 4
    1 4 1
    2 3 3
    2 4 2
    3 4 5
    0

    Sample Output

    3
    5
    
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    #define N 110
    
    int n,f[N];
    struct node{
        int x,y,w;
    };
    node e[N*N];
    
    int cmp(node a,node b)
    {
        return a.w < b.w ;
    }
    
    int find(int u)
    {
        if(f[u] == u)
            return u;
        else
        {
            f[u] = find(f[u]);
            return f[u];
        }
    }
    
    int merge(int u,int v)
    {
        int t1,t2;
        t1 = find(u);
        t2 = find(v);
        if(t1!=t2)
        {
            f[t1] = t2;
            return 1;
        }
        return 0;
    }
    int main()
    {
        int sum,count;
        while(scanf("%d",&n),n!=0)
        {
            for(int i=1; i <= n; i ++)
                f[i] = i;
            int m = n*(n-1)/2;
            for(int i = 1; i <= m; i ++)
                scanf("%d%d%d",&e[i].x,&e[i].y ,&e[i].w);
            sort(e+1,e+1+m,cmp);
             sum = count = 0;
            for(int i =1; i <= m; i ++)
            {
                if(merge(e[i].x,e[i].y))
                {
                    sum += e[i].w ;
                    count ++;
                }
                if(count == n-1)
                    break;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in. 

    You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

    Input

    The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

    Output

    For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

    Sample Input

    10 9
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    10 4
    2 3
    4 5
    4 8
    5 8
    0 0
    

    Sample Output

    Case 1: 1
    Case 2: 7
    #include<stdio.h>
    #define  N 50010
    #define ll int
    int f[N];
    
    ll find(ll u)
    {
        if(f[u] ==u)
            return u;
        else
        {
            f[u] = find(f[u]);
            return f[u];
        }
    }
    
    void merge(ll u,ll v)
    {
        ll t1,t2;
        t1 = find(u);
        t2 = find(v);
        if(t1!=t2)
            f[t1] = t2;
        return;
    }
    int main()
    {
        int n,m,t1,t2;
        int t = 0;
        while(scanf("%d%d",&n,&m),(m+n)!=0)
        {
            t++;
            for(int i = 1; i <= n; i ++)
                f[i] = i;
            for(int i = 1; i <= m; i ++)
            {
                scanf("%d%d",&t1,&t2);
                merge(t1,t2);
            }
            int count = 0;
            for(int i = 1; i <= n; i ++)
                if(f[i]== i)
                    count ++;
            printf("Case %d: %d
    ",t,count);
        }
        return 0;
    }
  • 相关阅读:
    Python+Selenium隐式等待操作代码
    Python+Selenium显示等待操作代码
    Python+Selenium键盘的几种操作:send_keys(Keys.CONTROL,'a')
    Selenium find_element_by_css_selector定位输入框报selenium.common.exceptions.NoSuchElementException的解决方法
    Python+selenium 鼠标三种(双击/右击/悬浮)操作方式(附代码!!)
    Selenium之find_element_by_css_selector三种定位方法,附代码!
    《jmeter:菜鸟入门到进阶》系列
    软件测试最基础的的面试题目~
    .NET页面事件执行顺序
    如何在ashx处理页中获取Session值
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7368047.html
Copyright © 2011-2022 走看看