hdu 4818 RP problem 【高斯消元】

 

RP problem

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 570    Accepted Submission(s): 154

 

 

Problem Description

  As an ACMer, you must have heard of the legend of Ren Pin (RP), which means a person’s lucky degree. Neither can RP be created, nor eliminated without foundation. It can only be transferred from one person to another. Moreover, everyone has his social circle. It’s guaranteed that each person has at least one friend and he cannot be the friend of himself. The relationship is directed. In RP system, one’s RP is transferred from himself to all of his friends uniformly every day. For example, A has three friends, B, C and D and his current RP is 1. In tomorrow, 1/3 of his RP will be transferred to B, 1/3 to C and 1/3 to D and RP from those treat him as friends will be transferred to him as well.

  On the other hand, the god in charge of the RP system is tired of the transferring RP from one person to another every day. Making the assumption that the total amount of RP is 1, he wants to know whether there is a stable distribution in RP system, so that by reallocating the RP of each person, he can lighten his workload. Stable distribution means if the structure of social network keeps unchanged, no matter how many days pass by, everyone’s RP keeps unchanged. You will see an example as follows. For the ease of presentation, let “X->Y” denotes X takes Y as his friend. There are only three persons A, B and C in the world with A->B, B->C, C->A and C->B. If the current RPs of A, B, C are 0.2, 0.4, 0.4 respectively, then the RPs will still be 0.2, 0.4, 0.4 in tomorrow. It’s obviously that the distribution keeps unchanged no matter how many days pass by, so (0.2, 0.4, 0.4) is a stable distribution. However, if the current RPs of A, B, C are 0.3, 0.3, 0.4, respectively, then the RPs will be 0.2, 0.5, 0.3 in tomorrow, so (0.3, 0.3, 0.4) is not a stable distribution. The god wonders, for a given a social network, how many stable distributions exist?

  Furthermore, if there is one and only one stable distribution, your friend A, who is lack of luck, wants to know whether he can increase his RP by making one more new friend. More specifically, letting RP(A, G) be the RP of A in the stable distribution for a network G, the RP of A after adding A->X to G is RP(A, G \/ {A->X}). Your friend A wants to know if there exists a person X, such that RP(A, G \/ {A->X}) is strictly larger than RP(A, G). For example, there are four persons A, B, C, D in the world with A->B, B->C, C->A, D->A. The only one stable distribution is (1/3, 1/3, 1/3, 0). If A->D is added to the network, the stable distribution will be (2/5, 1/5, 1/5, 1/5) and 2/5 > 1/3, so A can increase his RP by making friend with D. If there are multiple qualifying persons, your friend A wants to know which one can increase his RP to the maximum extent.

 

 

Input

The first line of input contains an integer T (T ≤ 50) indicating the number of datasets. Each dataset starts with two integers n and m (n ≤ 100, n ≤ m ≤ n*(n-1)), where n and m indicate the number of persons and the number of relationships, respectively. Next m lines describe the relationships in the social network. Each of these lines will contain two integers u and v (0 ≤ u, v < n), indicating u->v.

 

 

Output

For each test case, output a single line. If the number of stable distributions is infinite, print “INF”, otherwise, print the number of stable distributions. If there is only one stable distribution, print the person whom your friend A can increase the most RP by making friend with. If there exists multiple such persons, print the one with the smallest ID. However, if no person satisfies the requirement, print “-1”.

Your friend A’s ID is always (n-1).

 

 

Sample Input

 

3 4 4 0 1 1 0 2 3 3 2 3 3 0 1 1 2 2 0 3 3 0 1 1 0 2 1

 

 

Sample Output

 

INF 1 1 1 -1

 

解题思路:

这题枚举哪个人做好朋友再每次高斯消元会超时,但是每次选新的朋友在行列式中只改变最后一列的值,在阶梯化过程中前n-1步都是一样的,因此可以直接把枚举得到的新的列像增广矩阵那样加到原行列式的后面,然后一次阶梯化即可。

(这题wa了好久,原因是数组开小了QAQ...)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<queue>
#include<vector>
#include<list>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 205
double a[MAXN][MAXN];
int n,m;
int out[MAXN];
int g[MAXN][MAXN];
int gauss(int n,int m)
{
    for(int i=0;i<n;i++)
    {
        int max_r=i;
        for(int k=i+1;k<n;k++) if(fabs(a[k][i])>fabs(a[max_r][i])) max_r=k;

        if(fabs(a[max_r][i])<eps) return 0;

        if(max_r!=i) for(int j=0;j<=m;j++) swap(a[max_r][j],a[i][j]);

        for(int j=i+1;j<=m;j++) a[i][j]/=a[i][i];
        a[i][i]=1.0;

        for(int k=0;k<n;k++)
        if(fabs(a[k][i])<eps||i==k) continue;
        else
        {
            double f=a[k][i];
            for(int j=0;j<=m;j++) a[k][j]-=f*a[i][j];
        }
    }
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(out,0,sizeof out);
        memset(a,0,sizeof a);
        memset(g,0,sizeof g);
        scanf("%d%d",&n,&m);
        int u,v;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            if(u!=v) g[u][v]=1;
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            if(i!=j&&g[i][j]) out[i]++;

        for(int i=0;i<n;i++) if(out[i]) a[i][i]=-1.0;

        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(g[i][j]) a[j][i]=1.0/out[i];

        for(int i=0;i<n;i++)
        {
            if(g[n-1][i]||i==n-1) continue;
            a[i][n+i]=1.0/(out[n-1]+1);
            for(int j=0;j<n;j++)
                if(g[n-1][j]) a[j][i+n]=1.0/(out[n-1]+1);
        }

        for(int i=0;i<=2*n;i++) a[n-1][i]=1.0;

        int ans=gauss(n,2*n);

        if(!ans) {puts("INF");continue;}
        int p=-1;
        double Max=a[n-1][2*n];

        for(int i=0;i<n;i++)
        {
            if(g[n-1][i]||i==n-1) continue;
            double val=a[n-1][2*n]/a[n-1][n+i];
            if(val>Max) p=i,Max=val;
        }
        printf("1 %d\n",p);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值