CodeForces
来源:互联网 发布:ppt图表数据修改不了 编辑:程序博客网 时间:2024/05/29 15:07
CodeForces - 445B 贪心+模拟
CodeForces - 445B
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
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).
题目大意:有n种化学物质,其中有m对化学物质两两互相反应。现在要把他们按照某种按照顺序加入试管,求危险程序最大是多少。(空试管的危险度为1,每次加入一种化学物质,如果能够发生反应,那么危险程度翻倍。)
这道题目居然是用并查集来做,我居然用了贪心+模拟。。。。
#include <bits/stdc++.h>using namespace std;#define LL long longstruct node{int x,y;node(){x = 0,y = 0;}};bool cmp(node a,node b){ return a.y>b.y;}int main(){ int n,m,a,b; while(~scanf("%d%d",&n,&m)) { node f[n+1]; int vis[n+1] = {0}; queue<int> qu; vector<int> v[n+1]; for(int i = 1;i <= m;i++) { scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); f[a].x = a,f[a].y++; f[b].x = b,f[b].y++; } LL sum = 1; sort(f+1,f+n+1,cmp); for(int i = 1;i <= n;i++) { if(vis[f[i].x] == 0) { qu.push(f[i].x); vis[f[i].x] = 1; while(!qu.empty()) { int cur = qu.front(); //cout<<cur<<" **"<<endl; qu.pop(); for(int j = 0;j < v[cur].size();j++) { int u = v[cur][j]; if(vis[u] == 0) { sum *= 2; qu.push(u); vis[u] = 1; } } } } } printf("%lld\n",sum); } return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Java基础
- android常用框架
- [leetcode]151. Reverse Words in a String@Java解题报告
- 学习android个人笔记一
- CentOS7上安装Docker以及简单使用-yellowcong
- CodeForces
- HDU-2159-FATE(二维完全背包)
- socket 数据预留和对齐(skb_reserve()、skb_put()、skb_push())
- inline-block IE兼容处理
- java常见的设计模式
- wireshark 抓取的tcp 数据报文
- C语言——实例005 输入三个整数x,y,z,请把这三个数由小到大输出。
- Linux报错ping: https://www.baidu.com: Name or service not known
- nginx搭建