ZOJ 3710 Friends(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3710

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
integersn, 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 < nui ≠ 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

Author: ZHUANG, Junyuan

Contest: The 10th Zhejiang Provincial Collegiate Programming Contest

题意:

有 n 个人,其中一些人是朋友,如果某两个不是朋友的人之间满足有 k 个共同的朋友,那么这两个人也可认为是朋友,

给出 m 个初始关系,问最后会有多少个通过共同朋友而成为朋友的生成关系。

PS:

枚举两个不同的人(不是朋友)组合,遍历第三者,如果与两人都有朋友关系的人足够多(>=k),就添加关系并计数。

这样的遍历要一直走,直到遍历一次不生成新的关系为止。

代码如下:

#include <stdio.h>
#include <cstring>
int map[147][147];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int a, b;
		int i, j, h;
		int n, m, k;
		memset(map,0,sizeof(map));
		scanf("%d%d%d",&n,&m,&k);
		for(i = 1; i <= m; i++)
		{
			scanf("%d%d",&a,&b);
			map[a][b] = 1;
			map[b][a] = 1;
		}
		int ans = 0;
		for(int f = 0; ; f++)
		{
			int flag = 0;
			for(i = 0; i < n; i++)
			{
				for(j = i+1; j < n; j++)
				{
					if(map[i][j])
					{
						continue;
					}
					int cont = 0;
					for(h = 0; h < n; h++)
					{
						if(map[i][h] && map[j][h])
						{
							cont++;
						}
					}
					if(cont >= k)
					{
						flag = 1;
						//printf("  %d  %d\n",i,j);
						map[i][j] = 1;//xin peng you
						map[j][i] = 1;
						ans++;
					}
				}
			}
			if(flag == 0)
			{
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;

}
时间: 2024-10-04 07:16:56

ZOJ 3710 Friends(数学啊 )的相关文章

ZOJ 3710 friends

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100 + 1; bool g[maxn][maxn]; int main( ) { int ncase; scanf("%d", &ncase); while(ncase--) { int n, m,

F - Friends ZOJ - 3710(暴力)

题目链接:https://cn.vjudge.net/contest/280949#problem/F 题目大意:给你n个人,然后给你m个关系,每个关系输入t1, t2 .代表t1和t2是朋友关系(双向关系).然后输入一个k,代表两个人是亲密的朋友关系的话,就至少有k个共同的朋友,然后问你题目中这样的朋友有多少对? 具体思路:注意一个地方,朋友关系具有传递性,打个比方 t1和t2 变成了亲密的朋友,然后t0 本来和t1是朋友关系,但是和t2不是朋友关系,t1和t2成为朋友之后,t0也就和t2称为

HDU 4791 &amp;amp; ZOJ 3726 Alice&amp;#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

[数学+dfs] ZOJ 3753 Simple Equation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176 Simple Equation Time Limit: 2 Seconds      Memory Limit: 65536 KB There are many Equations. Some are difficult to solve, for example: an xn + an-1 xn-1 + .. + a0 = 0. In this probl

ZOJ 3407 Doraemon&#39;s Cake Machine [数学]

题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平面进行平切. 3.复制某块小蛋糕    这种操作只能在1和2所有操作都进行完才能进行. 求: 最少进行多少次复制操作可以将蛋糕分为一样的恰好n种.注意需要用恰好m次操作. 如果没有可行解输出-1. 思路: 假设进行三种操作的数目分别是xyz.然后连立两个式子把z消掉,get 2x(y+1)-(x+y

HDU 1988 &amp; ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1988 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1990 Problem Description The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As

HDU 1990 &amp; ZOJ 2992 Monkey Vines(数学啊)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1990 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1991 Problem Description Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and junipe

ZOJ 3230 Solving the Problems(数学 优先队列啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3230 Programming is fun, Aaron is addicted to it. In order to improve his programming skill, he decides to solve one programming problem per day. As you know, different problems have d