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.
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 ithfriendship 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.
For each case, print one line containing the answer.
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
2 04
思路:找到一点,向外扩散,找到不相连的一点,穷举一下两个点的连得边数,判断是否大于k。
代码:
#include<iostream> #include<algorithm> #include<string.h> #include<set> using namespace std; int main() { int T; cin>>T; int map[105][105];//存放关系的矩阵 while(T--) { memset(map,0,sizeof(map)); int n,m,k; cin>>n>>m>>k; for(int i=0; i<m; i++) { int u,v; cin>>u>>v; map[u][v]=1; map[v][u]=1; } bool ok=1; int num=0; while(ok) //纯暴解法,直至不能产生新边结束 { ok=0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) if(!map[i][j]) //遍历不相连的2点 { int tot=0;//计算公共相连的点数 for(int kk=0; kk<n; kk++) if(map[i][kk] && map[j][kk]) tot++; if(tot>=k) //如果不小于k就相连 { num++; map[i][j]=map[j][i]=1; ok=1; } } } } cout<<num<<'\n'; } return 0; }