Codeforces Round #254 (Div. 2) B (445B)DZY Loves Chemistry
推理可得终于结果为2的(n-可分组合数)次方。
问题是怎么求出可分组合数,深搜就可以,当然并查集也能够。
AC代码例如以下:
深搜代码!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define M 100005
#define ll long long
using namespace std; int a,b,c[205][205],vis[205];
int n,m; void dfs(int x)
{
int i;
vis[x]=1;
for(i=1;i<=n;i++)
{
if(!vis[i]&&c[x][i])
dfs(i);
}
} int main()
{ int i,j;
int sum,ans=0;
cin>>n>>m;
memset(c,0,sizeof c);
memset(vis,0,sizeof vis);
for(i=0;i<m;i++)
{
cin>>a>>b;
c[a][b]=1;
c[b][a]=1;//将a,b关联,能够用容器。但我认为不是必需
}
if(m==0)
cout<<"1"<<endl;
else{
for(j=1;j<=n;j++)
{
if(!vis[j])
{dfs(j);ans++;}//从一点搜起。记录组合数
}
cout<<(1LL<<(n-ans))<<endl;
} return 0;
}
并查集代码!!
!
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; int f[50005],vis[50005];
int find (int x)
{
if(f[x]==x)
return f[x];
else return find(f[x]);
} int main()
{
int n,m;
int i,j;
int a,b,c; long long ans;
cin>>n>>m;
memset(vis,0,sizeof vis);
ans=0;
for(i=1;i<=n;i++)
f[i]=i;
for(i=1;i<=m;i++)
{
cin>>a>>b;
a=find (a);
b=find (b);
f[a]=b;
}
for(i=1;i<=n;i++)
{
c=find(i);
if(!vis[c])
{ans++;vis[c]=1;}
}
cout<<(1LL<<(n-ans))<<endl;
return 0;
}
Codeforces Round #254 (Div. 2) B (445B)DZY Loves Chemistry的更多相关文章
- Codeforces Round #254 (Div. 2)D(预计)
D. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #309 (Div. 1) A(组合数学)
题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...
- Codeforces Round #392(Div 2) 758F(数论)
题目大意 求从l到r的整数中长度为n的等比数列个数,公比可以为分数 首先n=1的时候,直接输出r-l+1即可 n=2的时候,就是C(n, 2)*2 考虑n>2的情况 不妨设公比为p/q(p和q互 ...
- Codeforces Round #532 (Div. 2)- B(思维)
Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...
- Codeforces Round #597 (Div. 2)D(最小生成树)
/*每个点自己建立一座发电站相当于向超级源点连一条长度为c[i]的边,连电线即为(k[i]+k[j])*两点间曼哈顿距离,跑最小生成树(prim适用于稠密图,kruscal适用于稀疏图)*/ #def ...
- Codeforces Round #327 (Div. 2)B(逻辑)
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #207 (Div. 1)B(数学)
数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...
- Hot Days Codeforces Round #132 (Div. 2) D(贪婪)
Description The official capital and the cultural capital of Berland are connected by a single road ...
随机推荐
- 结构型设计模式之享元模式(Flyweight)
结构 意图 运用共享技术有效地支持大量细粒度的对象. 适用性 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态, ...
- 【IDEA】IDEA断点调试与清除断点
有时候我们必须启动debug模式来进行调试,在IDEA中断点调试与Eclipse大致相同: 1.以debug模式启动服务器: 2.在需要打断点的那一行前面点击一下标记上红点则是有断点,再次点击可以清除 ...
- 转 Join的实现原理及优化思路
前言 前面我们已经了解了MySQLQueryOptimizer的工作原理,学习了Query优化的基本原则和思路,理解了索引选择的技巧,这一节我们将围绕Query语句中使用非常频繁,且随时可能存在性能隐 ...
- 在C中就是字符'\r';换行是<lf>,是字符'\n'。
在C中就是字符'\r':换行是<lf>,是字符'\n'.
- python 666
运行下面代码会输出什么? __builtins__.我们来运行下这行代码看看="666" #!/usr/bin/env python # encoding: utf-8 def _ ...
- Jquery操作基本筛选过滤器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Laravel5.1忽略Csrf验证的方法
在/App/Http/middleware/VerifyCsrfToken.php 文件的protected $except里面加入路由地址
- adb install 安卓apk的包
➜ ~ adb install /Users/jkr/Downloads/QYVideoClient-debug.apk /Users/jkr/Downloads/QYVideoClient-deb ...
- Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 最小生成树【p2121】 拆地毯
题目描述--->p2121 拆地毯 分析 这题为什么是最大生成树. 先来bb两句 题目为拆地毯,让我们剩下k个地毯. 题目想要我们求得最大的美丽度. 且要求我们 保留的地毯构成的图中,任意可互相 ...