ZOJ 3710 Friends【floyd思想递推】

来源:互联网 发布:cd.java.tedu.cn v 编辑:程序博客网 时间:2024/06/08 16:09

Friends

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and m friendship among them. Assume that any new friendship is made only when they have sufficient friends in common mentioned above, you are to tell how many new friendship are made after a sufficiently long time.

Input

There are multiple test cases.

The first lien of the input contains an integer T (about 100) indicating the number of test cases. Then T cases follow. For each case, the first line contains three integers n, m, k (1 ≤ n≤ 100, 0 ≤ m ≤ n×(n-1)/2, 0 ≤ k ≤ n, there will be no duplicated friendship) followed by m lines showing the current friendship. The ith friendship contains two integers ui, vi (0 ≤ ui, vi <nui ≠ vi) indicating there is friendship between person ui and vi.

Note: The edges in test data are generated randomly.

Output

For each case, print one line containing the answer.

Sample Input

34 4 20 10 21 32 35 5 20 11 22 33 44 05 6 20 11 22 33 44 02 0

Sample Output

204


思路:如i认识j,j认识k,那么i就有可能认识 k,那么i想认识k要有什么样的条件呢?找到i认识k的路径数大等于k即可,如果可以认识,我们动态建路即可。

AC代码:

#include<stdio.h>#include<string.h>using namespace std;int map[1005][1005];int main(){    int t;    while(~scanf("%d",&t))    {        while(t--)        {            memset(map,0,sizeof(map));            int n,m,k;            scanf("%d%d%d",&n,&m,&k);            for(int i=0; i<m; i++)            {                int x,y;                scanf("%d%d",&x,&y);                map[x][y]=1;                map[y][x]=1;            }            int output=0;            int ok=1;            while(ok)            {                ok=0;                for(int i=0; i<n; i++)                {                    for(int j=i+1; j<n; j++)                    {                        if(map[i][j]==1)continue;                        int cont=0;                        for(int l=0; l<n; l++)                        {                            if(map[i][l]==1&&map[l][j]==1)                            {                                cont++;                            }                        }                        if(cont>=k)                        {                            output++;                            map[i][j]=1;                            map[j][i]=1;                            ok=1;                        }                    }                }            }            printf("%d\n",output);        }    }}










1 0
原创粉丝点击