Transformers [and query set], Transformers

Source: Internet
Author: User

Transformers [and query set], Transformers

Transformers
Description
One day Stubird bought a transformer, and the clerk said that this transformer is not the same as other ones that lie to a child. This transformer has n parts that can be connected to each other to form a robot. Of course, they can also be deformed. But one day, The tesseract's energy suddenly disappears, and parts are scattered all over The place. Of course, some parts are still connected. Now you only need to connect all the parts, and they can be changed back to their original state. For example, if there are four parts, 1 and 2 are connected, and 3 and 4 are connected, you only need to connect 1 and 3 (, 2, 3, or 2, 4) to change it back to its original state. He now asked how many connections do you need at least to change it back to its original state?

Input
The first line is a T, indicating that there is a T test sample followed by n and m (n <= 10 ^ 5, 0 <= m <= 10 ^ 5), n indicates the number of parts, m indicates how many parts are still connected to the following m rows. Each line u, v indicates the component u, and v indicates the connection. (1 <= u, v <= n)

Output
Minimum number of connections

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

Question: How many non-intersecting sets can be judged?

#include <stdio.h>#include <iostream>  #include <stdio.h>  #include <math.h>  #include <algorithm>  #include <string.h>  using namespace std;int p[30005];int rankk[30005];int findd(int x){    if (x == p[x])        return x;    else return  p[x] = findd(p[x]);}void un(int x, int y){    int a, b;    a = findd(x);    b = findd(y);    if (a == b)        return;    p[b] = a;}int main(){    int n, m, t, a, b;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&m);        for (int i = 1; i<=n; i++)        {            p[i] = i;            rankk[i] = 0;        }        for (int i = 0; i<m; i++)        {            scanf("%d%d",&a,&b);            un(a,b);        }        int ans = 0;        int f1 = findd(1);        for (int i = 2; i <= n; i++)        {            int fi = findd(i);            if (fi != f1)            {                p[fi] = f1;                ans++;            }        }        printf("%d\n", ans);    }}

Or

#include <stdio.h>#include <iostream>  #include <stdio.h>  #include <math.h>  #include <algorithm>  #include <string.h>  using namespace std;int p[30005];int rankk[30005];int vis[30000];int findd(int x){    if (x == p[x])        return x;    else return  p[x] = findd(p[x]);}void un(int x, int y){    int a, b;    a = findd(x);    b = findd(y);    if (a == b)        return;    p[b] = a;}int main(){    int n, m, t, a, b;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&m);        for (int i = 1; i<=n; i++)        {            p[i] = i;            rankk[i] = 0;            vis[i] = 0;        }        for (int i = 0; i<m; i++)        {            scanf("%d%d",&a,&b);            un(a,b);        }        int ans = 0;        for (int i = 1; i <= n; i++)        {            int fa = findd(i);            if (!vis[fa])            {                vis[fa] = 1;                ans++;            }        }        printf("%d\n", ans-1);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.