ZOJ 3710 Friends(暴力)

来源:互联网 发布:sql加一行合计 编辑:程序博客网 时间:2024/06/05 09:52

Description

Alice livesin the country where people like to make friends. The friendship isbidirectional and if any two person have no less than kfriends in common, they will become friends in severaldays. Currently, there are totally n peoplein the country, and m friendshipamong them. Assume that any new friendship is made only when they havesufficient friends in common mentioned above, you are to tell how many newfriendship are made after a sufficiently long time.

Input

Thereare multiple test cases.

Thefirst lien of the input contains an integer T (about 100) indicating the number of testcases. Then T casesfollow. 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 duplicatedfriendship) followed by mlines showingthe current friendship. The ith friendshipcontains two integers ui,vi (0 ≤ ui, vi < n, ui ≠ vi) indicating thereis friendship between person ui and vi.

Note: The edges in test data are generated randomly.

Output

For eachcase, 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个共同的朋友,问最后能有多少对新朋友。这题可以用图论解决,使用矩阵储存两个人之间的关系(1表示朋友关系,0表示不认识),然后遍历整个矩阵,寻找不认识的人的共同朋友,并判断他们是否能成为新朋友,若能,则关系改为1,再重新遍历矩阵。


#include <iostream>
using namespace std;


int main()
{
int T;
cin>>T;
while(T--)
{
int n,m,k;
int P[105][105]={0};
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
P[u][v]=1;
P[v][u]=1;
}   

int sum=0;
for(int i=0;i<n;i++)
   {
       for(int j=0;j<n;j++)
    {
    int cnt=0;
    if(i!=j&&P[i][j]==0)
    {
    for(int s=0;s<n;s++)
         if((P[i][s]==1)&&(P[j][s]==1))
       cnt++;
       if(cnt>=k)
   {
       sum++;
       P[i][j]=1;P[j][i]=1;
       i=-1;
       break;
   }
    }
    }
   }
     cout<<sum<<endl;
}
return 0;
}













0 0
原创粉丝点击