您的位置:首页 > 其它

周赛2-CodeForces 445B-E 题

2015-08-08 15:50 281 查看


Description

DZY loves chemistry, and he enjoys mixing chemicals.

DZY has n chemicals, and
m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.

Let's consider the danger of a test tube. Danger of an empty test tube is
1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by
2. Otherwise the danger remains as it is.

Find the maximum possible danger after pouring all the chemicals one by one in optimal order.

Input

The first line contains two space-separated integers n and
m

.

Each of the next m lines contains two space-separated integers
xi and
yi(1 ≤ xi < yi ≤ n). These integers mean
that the chemical xi will react with the chemical
yi. Each pair of chemicals will appear at most once in the input.

Consider all the chemicals numbered from 1 to
n in some order.

Output

Print a single integer — the maximum possible danger.

Sample Input

Input
1 0


Output
1


Input
2 11 2


Output
2


Input
3 21 22 3


Output
4


题意: 看化学反应的危险程度,初始时候为1,加一种反应就乘于2,用并查集就过了,当时一想,是不是多种反应就取乘于2啊,按照这个思路写了下,是错了,后来想到可能会成环,成环的话就多去乘了个2啊

并查集 A的代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int ls[100];
int fd(int x)
{
    return ls[x] == x ? x:ls[x]=fd(ls[x]);
}
void cb(int x,int y)
{
    int xx = fd(x);
    int yy = fd(y);
    if(xx != yy)
        ls[xx]=yy;
}
int main()
{
    int n, m;
    while(~scanf("%d%d",&n,&m))
    {
        int i, j;
        long long k = 1;
        for(i = 0; i <= n; i++)
            ls[i] = i;
        for(i = 0; i < m; i++)
        {
            int u, v;
            scanf("%d%d",&u,&v);
            cb(u,v);
        }
        int c = 0;
        for( i = 1; i<= n; i++)
        {
            if(ls[i] == i)
                c++;
        }
        for(i = 0; i < n-c; i++)
            k=k*2;
       printf("%lld\n",k);
    }
    return 0;
}


下面这个是乘2的那个Wa的代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int map[155][155];
int main()
{
    int n, m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,0,sizeof(map));
        int i, j;
        long long k = 1;
        for(i = 0; i < m; i++)
        {
            int u, v;
            scanf("%d%d",&u,&v);
            if(!map[u][v])
            {
                k=k*2;
                map[u][v] = 1;
                map[v][u] = 1;
            }
        }
       printf("%lld\n",k);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: