ZOJ Problem Set - 3710 Friends

题目链接

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.

题目大意:共有n个人,某两个人是朋友这样的关系有m个, 如果两个人不是朋友,但是他们的共同好友有k个及以上,则他们为朋友。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<string>
using namespace std;
int mp[110][110];
int num, n, m, k, ans, T;
int main()
{
	cin >> T;
	while(T--) {
		memset(mp, 0, sizeof(mp));
		ans = 0;
		cin >> n >> m >> k;
		for(int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			mp[u][v] = 1;
			mp[v][u] = 1;  // 两个人是朋友则为1
		}
		bool ok = 1;
		while(ok) {  // 因为根据共同朋友的数量,朋友关系必须更新
			ok = 0;
			for(int i = 0; i < n - 1; i++) {
				for(int j = i + 1; j < n; j++) {
					if(!mp[i][j]) {
						num = 0;
						for(int q = 0; q < n; q++) {
							if(mp[i][q] == 1 && mp[q][j] == 1) 
								num++;
						}
						if(num >= k) {
							mp[i][j] = 1;
							mp[j][i] = 1;
							ans++;
							ok = 1;
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40844130/article/details/80098665