Friends
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
题意:有n个人,m条关系,任意两人的共同朋友数不少于k,则两人成为朋友。问最多形成几条新关系?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m,u,v;
unsigned k;
cin>>n>>m>>k;
vector <int>rela[128];
for(int i=0; i<m; i++)
{
cin>>u>>v;
rela[u].push_back(v);
rela[v].push_back(u);
}
int sum=0;
bool F = true;
while(F)
{
F = false;
for(int i=0; i<n; i++)
{
if(rela[i].size()<k)
continue;
for(int j=0; j<n; j++)
{
if(i==j)
continue;
if(rela[j].size()<k)
continue;
vector <int>::iterator flag = find(rela[i].begin(),rela[i].end(),j);
if(flag!=rela[i].end())
continue;
unsigned s1=0;
unsigned s2=0;
unsigned Count=0;
for(; s1<rela[i].size(); s1++)
{
if(rela[i][s1]==j)
continue;
for(s2=0; s2<rela[j].size(); s2++)
{
if(rela[j][s2]==i)
continue;
if(rela[i][s1]==rela[j][s2])
{
Count++;
break;
}
}
}
if(Count>=k)
{
sum++;
rela[i].push_back(j);
sort(rela[i].begin(),rela[i].end());
rela[j].push_back(i);
sort(rela[j].begin(),rela[j].end());
F = true;
}
}
}
}
cout<<sum<<endl;
}
return 0;
}