视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
CodeforcesRound#254(Div.2)
2020-11-09 08:07:02 责编:小采
文档


Note In the first sample, there's only one way to pour, and the danger won't increase. In the second sample, no matter we pour the 1 st chemical first, or pour the 2 nd chemical first, the answer is always 2 . In the third sample, there ar

Note

In the first sample, there's only one way to pour, and the danger won't increase.

In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.

In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).

写给自己:水水的题目, 竟然WA,一直找不到错误的地方. 最后才发现思路和细节都没有错,只是答案输出的类型精度不够(应该用long long 的只用了int).多么痛的领悟.出来献丑,不忘耻辱.

分析: 把各种化学物品的反应关系画成图, 可知只要用任一种遍历图(这个图可能是非连通的)的方法(DFS,BFS,并查集均可)遍历它便可求解(存在一条边则danger翻倍).

如下使用"邻接矩阵表示的DFS":

#include
#include
#define maxn 55
int G[maxn][maxn],vis[maxn];
int n;
long long ans;
void DFS(int v)
{
 int w;
 vis[v]=1;
 for(w=1;w<=n;w++)
 if(G[v][w]!=0 && vis[w]==0)
 { ans=ans*2; DFS(w); }
}
void DFSTraverse()
{
 int i;
 for(i=1;i<=n;i++)
 if(vis[i]==0)
 DFS(i);
}
int main()
{
	int m;
	int a,b;
	scanf("%d%d",&n,&m);
	memset(G,0,sizeof(G));
	memset(vis,0,sizeof(vis));
	ans=1;
	while(m--)
 {
 scanf("%d%d",&a,&b);
 G[a][b]=G[b][a]=1;
 }
 DFSTraverse();
 printf("%lld\n",ans);
	return 0;
}

另外, 借鉴别人并查集的做法:

下载本文
显示全文
专题