SRM691 Sunnygraphs2
Problem Statement
- Add a new isolated vertex number n.
- Choose a subset M of the original vertices.
- For each x in M, erase an edge between vertices x and a[x].
- For each x in M, add a new edge between vertices x and n.
Hero's goal is to create a final graph in which the vertices 0 through n-1 are all in the same connected component. (I.e., there must be a way to reach any of these vertices from any other of them by following one or more consecutive edges, possibly visiting vertex n along the way.) Note that Hero does not care whether vertex n is in the same component as the other vertices: both possibilities are fine. In step 2 of the above procedure Hero has 2^n possible subsets to choose from. A choice of M is good if it produces a graph with the desired property. Count how many of the 2^n possibilities are good choices. Return that count as a long.
Definition
- ClassSunnygraphs2
- Methodcount
- Parametersvector<int>
- Returnslong long
- Method signaturelong long count(vector<int> a)
Limits
- Time limit (s)2.000
- Memory limit (MB)256
Constraints
- a will contain n elements.
- n will be between 2 and 50, inclusive.
- Each element in a will be between 0 and n - 1, inclusive.
- For each i between 0 and n - 1 holds a[i] != i.
Test cases
- a{ 1, 0 }
Returns4
The original graph contained the vertices 0 and 1. This pair of vertices was connected by two edges. Next, Hero added a new vertex 2. Then he had to choose one of four possible subsets M:- If he chose M = {}, the resulting graph contained the edges 0-1 and 0-1. The vertices 0 and 1 were in the same component.
- If he chose M = {0}, the resulting graph contained the edges 0-1 and 0-2. The vertices 0 and 1 were in the same component.
- If he chose M = {1}, the resulting graph contained the edges 0-1 and 1-2. The vertices 0 and 1 were in the same component.
- Finally, if he chose M = {0, 1}, the resulting graph contained the edges 0-2 and 1-2. And again, the vertices 0 and 1 were in the same component. (In the resulting graph we can still go from vertex 0 to vertex 1, even though we have to go via vertex 2.)
As all four choices of M are good, the correct answer is 4.
- a{ 1, 0, 0 }
Returns7
Here, M = {2} is not a good choice. This choice produces a graph with edges 0-1, 0-1, and 2-3. In this graph vertex 2 is not in the same component as vertices 0 and 1. The other seven possible choices of M are all good.- a{ 2, 3, 0, 1 }
Returns9
- a{ 2, 3, 0, 1, 0 }
Returns18
- a{ 2, 3, 0, 1, 0, 4, 5, 2, 3 }
Returns288
- a{ 29, 34, 40, 17, 16, 12, 0, 40, 20, 35, 5, 13, 27, 7, 29, 13, 14, 39, 42, 9, 30, 38, 27, 40, 34, 33, 42, 20, 29, 42, 12, 29, 30, 21, 4, 5, 7, 25, 24, 17, 39, 32, 9 }
Returns6184752906240
"Watch out for integer overflow."- a{ 9, 2, 0, 43, 12, 14, 39, 25, 24, 3, 16, 17, 22, 0, 6, 21, 18, 29, 34, 35, 23, 43, 28, 28, 20, 11, 5, 12, 31, 24, 8, 13, 17, 10, 15, 9, 15, 26, 4, 13, 21, 27, 36, 39 }
Returns17317308137473
题意。。看了很久。
其实就是用一个点n来连接其他联通分量,使得标号0~n-1这些点再一个联通分量中。
为了简化题目,假设原图中联通分量>=2,我们可以先找出原图中的环,因为按照题目规则,只有两个环分别拓展出一条边连接点n,使得所有联通分量和为1个联通分量。
下面解释一下样例3.
如图0,2是一个环 1,3是一个环。现在借助5把{4,0,2}和{1,3}这两个联通分量连接起来。先不考虑点4.那么{0,2}中有3种方案可以选择,{1,3}中有3中方案可以选则所以一共有9中方案。接下来考虑点4,那么挂上点4后,答案也是9. 所以种答案是18.

如果0~n-1这些点原来在一个联通分量中。那么 我们还要加上空集这种情况。
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#define ll long long
using namespace std; class Sunnygraphs2 {
public:
int vis[]={};
int used[]={};
vector<int>edge[];
int num=;
void dfs(int u){
num++;used[u]=;
for(int i:edge[u])if(!used[i])dfs(i);
}
long long count(vector<int> a) {
int n=a.size();
int m=n;
for(int i=;i<n;i++){
edge[i].push_back(a[i]);
edge[a[i]].push_back(i);
}
dfs();
ll ans=;
int cnt=,mark=,cur;
for(int i=;i<n;i++){//找环
if(!vis[i]){
cur=i;cnt=mark=;
for(int j=;j<=;j++){
cur=a[cur];cnt++;//记录环中节点数
if(cur==i){
mark=;break;
}
}
if(mark){
cur=i;
for(int j=;j<=;j++){
vis[cur]=;
cur=a[cur];
}
ans*=(ll)pow(2ll,cnt)-;
m-=cnt;
}
}
}
ans*=(ll)pow(2ll,m);
if(num==n)ans++;//包含空集
return ans;
}
};
SRM691 Sunnygraphs2的更多相关文章
- ACM学习历程—TopCoder SRM691 Div2
这是我的第一次打TC,感觉打的一般般吧.不过TC的题目确实挺有意思的. 由于是用客户端打的,所以就不发题目地址了. 300分的题: 这题大意是有一段序列只包含+和数字0~9. 一段序列的操作是,从头扫 ...
随机推荐
- 了解DOM
DOM是为了方便处理层次型文档(如XML.HTML)的一种技术.DOM还提供了一套API,使开发人员可以用面向对象的方式来处理这些文档.对于XML文档来说,有专门的处理XML文档是XML DOM,一 ...
- 性能测试工具 - ab 简单应用
之前知道一般网站性能可以通过 LoadRunner, JMeter, QTP 等相应的软件进行测试, 印象中本科学习 “软件测试” 这门课程时安装并使用过, LoadRunner等不是一个小软件, 安 ...
- waiting TTFB 时间优化
百度百科解释:获取在接收到响应的首字节前花费的毫秒数. 根据chrome浏览器,具体请求链接的这个时间,对反应慢的页面进行优化.
- [Go]链表的相关知识
切片有着占用内存少喝创建便捷等特点,但它本质上还是数组.切片的一大好处是可以通过窗口快速地定位并获取或者修改底层数组中的元素.不过当删除切片中的元素的时候就没那么简单了.元素复制一般是免不了的,就算只 ...
- Linux 修改主机名
1 vi /etc/sysconfig/network 2 vi /etc/hosts 3 hostname xxx 4 Done! 退出重连后生效
- 亲历dataguard的一些经验问答题
问题1:是否log_archive_dest_n=service中进程使用lgwr时(如log_archive_dest_2='service=DBSTD LGWR SYNC'),备库就一定要建立st ...
- hdu 4421 和poj3678类似二级制操作(2-sat问题)
/* 题意:还是二进制异或,和poj3678类似 建边和poj3678一样 */ #include<stdio.h> #include<string.h> #include&l ...
- SHELL脚本运行的几种方法以及区别
#1 给脚本加上执行权限chmod u+x a.sh, 而后就可以直接用全路径来执行脚本了,比如当前文件夹下用./a.sh,如果如果脚本所在目录在PATH环境变量之中, 则直接用a.sh即可(这和运行 ...
- poj2117求割点后最多的块。
tarjan算法,枚举割点(注意此题无向图可能不连通),每个割点分割后最大块数+连通分量-1即可.开始老是TLE,后来比较了他人代码,只在vector<vector<int.>.&g ...
- python学习之-- IO多路复用 select模块
python I/O多路复用包括3个模块,上一篇已经说过概念,这里我使用的是select模块实现一个ftp并发 服务器端核心代码: import socket,select import queue, ...