首页 > 代码库 > Codeforces Round #254(div2)B

Codeforces Round #254(div2)B

就是看无向图有几个连通块,答案就是2n-num

范围很小,就用矩阵来存图减少代码量。

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<set>#include<vector>#include<algorithm>#include<stack>#include<queue>using namespace std;#define INF 1000000000#define eps 1e-8#define pii pair<int,int>#define LL long long intint n,m,maps[55][55],a,b;int mark[55],num;void dfs(int x){    mark[x]=1;    for(int i=1;i<=n;i++)    {        if(mark[i]==0&&maps[x][i])        {            dfs(i);        }    }}int main(){    //freopen("in1.txt","r",stdin);    //freopen("out.txt","w",stdout);    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d",&a,&b);        maps[a][b]=maps[b][a]=1;    }    for(int i=1;i<=n;i++)    {        if(!mark[i])        {            num++;            dfs(i);        }    }    printf("%I64d\n",(LL)1<<(n-num));    //fclose(stdin);    //fclose(stdout);    return 0;}