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 <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
思路:如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);
}
}
}
本文探讨了一个社交网络模型,在该模型中,当两个人拥有至少k个共同朋友时,他们就会成为朋友。文章提供了一种算法来计算在一个给定的社交网络中,经过足够长的时间后能形成多少新的友谊。
5381

被折叠的 条评论
为什么被折叠?



