Problem Statement

Hero has just constructed a very specific graph. He started with n isolated vertices, labeled 0 through n-1. For each vertex i Hero then chose a vertex a[i] (other than i) and he added an edge that connected i and a[i]. This way he created a graph with n vertices and n edges. Note that if a[x]=y and a[y]=x, the vertices x and y were connected by two different edges. Hero now wants to perform the following procedure:
  1. Add a new isolated vertex number n.
  2. Choose a subset M of the original vertices.
  3. For each x in M, erase an edge between vertices x and a[x].
  4. 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)
(be sure your method is public)

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

  1.  
    • 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.

  2.  
    • 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.
  3.  
    • a{ 2, 3, 0, 1 }
     

    Returns9

  4.  
    • a{ 2, 3, 0, 1, 0 }
     

    Returns18

  5.  
    • a{ 2, 3, 0, 1, 0, 4, 5, 2, 3 }
     

    Returns288

  6.  
    • 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."
  7.  
    • 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的更多相关文章

  1. ACM学习历程—TopCoder SRM691 Div2

    这是我的第一次打TC,感觉打的一般般吧.不过TC的题目确实挺有意思的. 由于是用客户端打的,所以就不发题目地址了. 300分的题: 这题大意是有一段序列只包含+和数字0~9. 一段序列的操作是,从头扫 ...

随机推荐

  1. [Istio]流量管理API v1alpha3路由规则

    Istio提供一个API进行流量管理,该API允许用户将请求路由到特定版本的服务,为弹性测试注入延迟和失败,添加断路器等,所有这些功能都不必更改应用程序本身的代码.Istio 1.0中引入新的流量管理 ...

  2. [luoguP2704] 炮兵阵地(状压DP)

    传送门 可以事先把每一行的所有状态处理出来,发现每一行的状态数最多不超过60个 f[i][j][k]表示前i行,第i行为状态j,第i-1行为状态k的最优解 #include <vector> ...

  3. BZOJ 3175: [Tjoi2013]攻击装置

    捉水题真是捉上瘾了TUT Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y- ...

  4. [转]制作一个64M的U盘启动盘(mini linux + winpe +dos toolbox)

    自己动手定制winpe+各类dos工具箱U盘启动盘+minilinux 由于一个64M老U盘,没什么用,拿来发挥余热.如果U盘够大,可以使用功能更强大的mini linux和带更多工具的winpe.这 ...

  5. 使用Sencha Architect开发Sencha Touch应用的整理

    官网:http://www.sencha.com/ 其实官网上的文档都很清楚了,不过整理一下总比较好 第一步,软件准备 注: 以下软件的安装本着这样两条原则 一是不要安装在中文目录下 二是不要安装在带 ...

  6. 静态工具类中使用注解注入service实例

    一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package ...

  7. 【CF766D】Mahmoud and a Dictionary(并查集)

    题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反. 并且“相同”与“相反”有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系): ...

  8. UVA 10245 The Closest Pair Problem【分治】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=21269 题意: 求平面最近点对. 分析: 经典问题. n比 ...

  9. Html.EditorFor 加 htmlAttributes

    @Html.EditorFor(m => m.Name, new { htmlAttributes = new { @required = "true", @anotherA ...

  10. spring mvc get请求也可以接受DTO对象

    spring mvc get请求也可以接受DTO对象,比如:url上面你还是将参数&符号连接起来,并自动封装进一个DTO对象里. 只有@RequestBody注解spring mvc才会从ht ...