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.
<h4< dd="">For each case, print one line containing the answer.
<h4< dd="">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<h4< dd="">
2 0 4
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; int vis[109][109]; vector<int>a[109]; int is_NewFriend(int u,int v,int k){ int i,j; int commond = 0; for(i = 0; i < a[u].size(); i++){ for(j = 0; j < a[v].size(); j++){ if(a[u][i] == a[v][j]){ commond++; break; } } if(commond >= k){ return 1; } } if(k == 0)return 1;//注意!!!!!当k=0并且u没有朋友,这个时候应该也返回1 return 0; } int main(){ int t; cin >> t; while(t--){ int n,m,k; cin >> n >> m >> k; int i,j; memset(vis,0,sizeof(vis)); for(i = 0; i < 109; i++){ a[i].clear(); } for(i = 0; i < m; i++){ int u,v; cin >> u >> v; vis[u][v] = 1; vis[v][u] = 1; a[u].push_back(v); a[v].push_back(u); } int sum = 0; while(1){ int flag = 0; for(i = 0; i < n; i++){ for(j = i+1; j < n; j++){ if(!vis[i][j]){ if(is_NewFriend(i,j,k)){ a[i].push_back(j); a[j].push_back(i); vis[i][j] = 1; vis[j][i] = 1; sum++; flag = 1; } } } } if(!flag) break; } cout << sum << endl; } return 0; }
本文介绍了一个算法问题,探讨在一个社交网络中如何根据共同好友数量预测可能产生的新友谊关系。问题输入包括人口数、当前友谊关系数及共同好友阈值,通过分析这些数据,输出最终形成的新增友谊关系总数。

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



