Masud Rana - UVa 11600 dp 并查集

此博客介绍了一个关于Masud Rana的任务模拟问题,涉及图论和动态规划的知识,目的是计算使所有城市间都有安全路径所需的期望天数。

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.

 

2

3 1

2 3

4 1

2 3

 

Case 1: 1.0

Case 2: 3.5

 



题意:有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代码如下:

#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);
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值