SRM691 Sunnygraphs2
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:
- 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.
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
-
- a{ 1, 0 }
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.)
-
- a{ 1, 0, 0 }
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 }
-
- a{ 2, 3, 0, 1, 0 }
-
- a{ 2, 3, 0, 1, 0, 4, 5, 2, 3 }
-
- 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 }
"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 }
题意。。看了很久。
其实就是用一个点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[60]={0}; int used[60]={0}; vector<int>edge[60]; int num=0; void dfs(int u){ num++;used[u]=1; 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=0;i<n;i++){ edge[i].push_back(a[i]); edge[a[i]].push_back(i); } dfs(0); ll ans=1; int cnt=0,mark=0,cur; for(int i=0;i<n;i++){//找环 if(!vis[i]){ cur=i;cnt=mark=0; for(int j=1;j<=50;j++){ cur=a[cur];cnt++;//记录环中节点数 if(cur==i){ mark=1;break; } } if(mark){ cur=i; for(int j=1;j<=50;j++){ vis[cur]=1; cur=a[cur]; } ans*=(ll)pow(2ll,cnt)-1; m-=cnt; } } } ans*=(ll)pow(2ll,m); if(num==n)ans++;//包含空集 return ans; } };
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: