Friends

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 ≤ mn×(n-1)/2, 0 ≤ kn, 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 < n, ui ≠ 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

3
4 4 2
0 1
0 2
1 3
2 3
5 5 2
0 1
1 2
2 3
3 4
4 0
5 6 2
0 1
1 2
2 3
3 4
4 0
2 0

 

Sample Output

2
0
4
题意:n个人,m种关系,如果两个人之间至少有k个相同的朋友,那么这两个人可称为新的朋友,即形成一种新的关系,问经过很长一段时间后,能够增加多少种新的关系
题解:图论,暴力解决。统计两个人之间的共同的朋友个数再判断即可
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,m,kk;
int p[100][100];
int main()
{
 int test,i,j,a,b,k,cnt,mm,ss;
 scanf("%d",&test);
 while(test--)
 {
       memset(p,0,sizeof(p));
    scanf("%d %d %d",&n,&m,&kk);
    for(i=0;i<m;i++)
    {
           scanf("%d %d",&a,&b);
     p[a][b]=1;
     p[b][a]=1;
    }
    ss=0;
    while(1)
    {
        cnt=0;
     for(i=0;i<n-1;i++)
     {
      for(j=i+1;j<n;j++)
      {
       mm=0;
       if(p[i][j]==0)
       {
        for(k=0;k<n;k++)
        {
         if(p[i][k]&&p[k][j])  mm++;
        }
        if(mm>=kk)
        {
         p[i][j]=1;
         p[j][i]=1;
         cnt+=1;
        }
       }
      }
     }
     ss+=cnt;
     if(cnt==0) break;
  }
  printf("%d\n",ss);
 }
 return 0;
}
posted @ 2013-10-04 15:55  forevermemory  阅读(208)  评论(0编辑  收藏  举报