爱丽丝住在人们喜欢交朋友的国家。 友谊是双向的,如果任何两个人有不少k个朋友共同,他们将成为朋友在几天。 目前,全国共有n人,其中有友谊。 假设任何新的友谊只有当他们有足够的朋友共同提到上述,你是要告诉在一段足够长的时间后,有多少新的友谊。
maps[i][j]记录 i, j 是否已经是朋友
graph[i][j]统计 i, j共同的朋友
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#define rtl rt<<1
#define rtr rt<<1|1
typedef long long LL;
using namespace std;
const int MAX = 100+10;
const double eps = 1e-10;
const double PI = acos(-1.0);
int t, n, m, k, a, b, maps[MAX][MAX], ans, graph[MAX][MAX], cnt;
int main()
{
scanf("%d", &t);
while(t--)
{
ans = 0;
memset(maps, 0, sizeof(maps));
cin>>n>>m>>k;
for(int i = 0; i<m; ++i)
{
cin>>a>>b;
maps[b][a] = maps[a][b] = 1;
}
while(1)
{
cnt = 0;
memset(graph, 0, sizeof(graph));// 注意清0, 否则会重复统计
for(int i = 0; i<n-1; ++i)
{
for(int j = i+1; j<n; ++j)
{
if(maps[i][j]||maps[j][i])continue;
for(int kk = 0; kk<n; ++kk)
{
if(kk==i||kk==j)continue;
if(maps[i][kk]&&maps[kk][j])
{
graph[i][j]++;
graph[j][i]++;
}
}
if(graph[i][j]>=k||graph[j][i]>=k)
{
cnt++;
maps[i][j] = maps[j][i] = 1;
}
}
}
if(cnt==0)break;// 如果没有更新,退出循环
ans+=cnt;
}
printf("%d\n", ans);
}
return 0;
}