Codeforces 第 254 轮(第 2 部分)
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<stdio.h> #include<string.h> #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; }
另外, 借鉴别人并查集的做法:

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

文章讨论了使用准备好的语句,输入验证和强密码策略确保针对SQL注入和蛮力攻击的MySQL。(159个字符)
