Masud Rana - UVa 11600 dp 并查集
2014-12-03 22:26
393 查看
Masud Rana, A Daring Spy Of Bangladesh Counter Intelligence. He is in a new mission. There is a total n cities in Bangladesh. Each city is connected to all other by bidirectional roads. So there are total n * (n-1) / 2 bidirectional roads. Many of the roads
are under control of evil powers. City a is safely reachable from city b, if there is a path from a to b containing only roads which are not under control of evil powers. There are m roads which are safe from evil powers. The mission of Masud Rana is to destroy
the evil powers of some roads, and make sure that every city is safely reachable from all other.
Masud Rana chose a new strategy for this special mission. Every morning he selects a random city other than the city he stays in at that moment, and visit that city by direct connecting road, in the time of his visit by the road he destroys all evil power
of that road if exists any, and makes that road safe. After reaching new city, he stays there till next morning. In the next morning he checks whether all cities are safely reachable from all others. If he is already done his mission ends, otherwise he repeats
same strategy.
Let us number the cities by 1, 2, ... , n. Masud Rana is in city 1 when he starts his mission.
What is the expected number of days to finish the mission for Masud Rana.
Input
Input will starts with an integer T(T ≤ 100) which denotes the number of test case. Each case starts with two integer N(N ≤ 1 ≤ 30) and M(0 ≤ M ≤ N*(N-1)/2). Each of the next lines contains two integers a and b (1 ≤ a, b ≤ N) which means road connecting
city a and b is safe.
Output
You have to output the expected number of days required for Masud Rana. Print the case number followed by the output. Look at the sample in/out for exact format. Upto 1E-6 error in your output will be acceptable.
题意:有n个城市,每个两个城市之间都有一条路,有些路是被邪恶势力控制的。现在有m条路是安全的。一个人,他的目的是使得每两个城市之间都存在只走安全的路的路径。他每天随机选取一个其他的城市,然后从最直接的路走到这个城市,并且使得这条路安全。问他完成任务的期望天数。
思路:首先将一些连接到一起的城市看做一个小集合,用每个小集合中城市的数目做哈希,比如第二组样例差1个有1个节点的集合,和1个有两个节点的集合,设dp[3]为这个答案,dp[2]为差一个有两个节点的集合,dp[1]为差一个有一个节点的集合,概率的求法是dp[3]=2/3*dp[1]+1/3*dp[2]+0/3*dp[3]+1。
AC代码如下:
are under control of evil powers. City a is safely reachable from city b, if there is a path from a to b containing only roads which are not under control of evil powers. There are m roads which are safe from evil powers. The mission of Masud Rana is to destroy
the evil powers of some roads, and make sure that every city is safely reachable from all other.
Masud Rana chose a new strategy for this special mission. Every morning he selects a random city other than the city he stays in at that moment, and visit that city by direct connecting road, in the time of his visit by the road he destroys all evil power
of that road if exists any, and makes that road safe. After reaching new city, he stays there till next morning. In the next morning he checks whether all cities are safely reachable from all others. If he is already done his mission ends, otherwise he repeats
same strategy.
Let us number the cities by 1, 2, ... , n. Masud Rana is in city 1 when he starts his mission.
What is the expected number of days to finish the mission for Masud Rana.
Input
Input will starts with an integer T(T ≤ 100) which denotes the number of test case. Each case starts with two integer N(N ≤ 1 ≤ 30) and M(0 ≤ M ≤ N*(N-1)/2). Each of the next lines contains two integers a and b (1 ≤ a, b ≤ N) which means road connecting
city a and b is safe.
Output
You have to output the expected number of days required for Masud Rana. Print the case number followed by the output. Look at the sample in/out for exact format. Upto 1E-6 error in your output will be acceptable.
2 3 1 2 3 4 1 2 3 | Case 1: 1.0 Case 2: 3.5 |
思路:首先将一些连接到一起的城市看做一个小集合,用每个小集合中城市的数目做哈希,比如第二组样例差1个有1个节点的集合,和1个有两个节点的集合,设dp[3]为这个答案,dp[2]为差一个有两个节点的集合,dp[1]为差一个有一个节点的集合,概率的求法是dp[3]=2/3*dp[1]+1/3*dp[2]+0/3*dp[3]+1。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; typedef unsigned long long ll; ll Hash[35],MOD=1e9+7; int n,p[35],sum[35],num[35]; struct Node { int num[31]; double p; }; map<ll,Node> match; int find(int x) { return x==p[x] ? x : p[x]=find(p[x]); } void Union(int u,int v) { u=find(u); v=find(v); if(u!=v) { p[u]=v; sum[v]+=sum[u]; } } double solve(ll S) { if(match[S].num[0]==1) return match[S].p; match[S].num[0]=1; int i,j,f=0; ll k; double ret=0,ans; for(i=1;i<=30;i++) f+=match[S].num[i]*i; if(f==0) { match[S].p=0; return 0; } for(i=1;i<=30;i++) if(match[S].num[i]>0) { k=S-Hash[i]; if(match[k].num[0]==0) { for(j=1;j<=30;j++) match[k].num[j]=match[S].num[j]; match[k].num[i]--; } ret+=1.0*match[S].num[i]*i/(n-1)*solve(k); } ans=(ret+1)*(n-1)/f; match[S].p=ans; return ans; } int main() { int T,t,m,i,j,k,u,v; ll ret; double ans; Hash[1]=1; for(i=2;i<=30;i++) Hash[i]=Hash[i-1]*MOD; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d",&n,&m); match.clear(); match[0].num[0]=1;match[0].p=0; for(i=1;i<=n;i++) { p[i]=i; sum[i]=1; } for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); Union(u,v); } memset(num,0,sizeof(num)); for(i=1;i<=n;i++) find(i); for(i=1;i<=n;i++) if(i==p[i]) num[sum[i]]++; if(sum[p[1]]==n) { printf("Case %d: 0.000000\n",t); continue; } num[sum[p[1]]]--; ret=0; for(i=1;i<=30;i++) ret+=num[i]*Hash[i]; for(i=1;i<=30;i++) match[ret].num[i]=num[i]; ans=solve(ret); printf("Case %d: %.6f\n",t,ans); } }
相关文章推荐
- UVA 11600 Masud Rana 并查集+状压概率dp
- UVA 11600-Masud Rana(状压,概率dp)
- UVA 11600 Masud Rana(概率dp)
- uva11600 - Masud Rana 状态压缩 期望
- Uva 11600 期望DP
- UVA 11600 - Masud Rana(状态压缩DP+记忆化搜索)
- uva 11600(期望dp)
- uva 11600 期望dp 状压dp
- uva11600 状压期望dp
- UVA 11600 Masud Rana
- UVA - 11600(dp,并未真正解决)
- uva 11600 - Masud Rana(记忆化搜索)
- Uva 861 (little bishop)搜索,棋盘多项式,dp
- UVa 10564 Paths through the Hourglass(DP)
- UVa 10817 Headmaster's Headache (状压DP)
- uva 11825 黑客的攻击 Hackers' Crackdown 集合dp+我的优化 非常好的好题
- uva 11367 (Dijkstra+DP)
- UVALive 3644 X-Plosives (并查集)
- Uva 11825 - Hackers’ Crackdown 状态压缩DP
- UVA - 11427 Expect the Expected (DP+概率)