ZOJ 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.

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 ≤ mn×(n-1)/2, 0 ≤
kn, 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个朋友关系(双向),如果两个人有num个以上的(含num)个公共朋友,那么他们也是朋友,问可以新加几个朋友关系

思路: 暴力,枚举i j之间有多少个公共朋友

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8

#define fre(i,a,b)  for(i = a; i <= b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 1000000000
#define N 105

int  n,m,num,ans;
int a[N][N];

void solve()
{
	int i,j,cnt;
	ans=0;

	while(1)
	{
		  bool flag=false;

		  for(i=0;i<n;i++)
			for(j=0;j<n;j++)
			  {
			  	if(i==j) continue;
			  	if(a[i][j]) continue;

                int cnt=0;
			    for(int k=0;k<n;k++)
					if(a[i][k]&&a[k][j])
					 cnt++;

				if(cnt>=num)
				{
					a[i][j]=a[j][i]=1;
					ans++;
				    flag=true;
				}

			  }

	          if(!flag)  break;
	}
	printf("%d\n",ans);
}

int main()
{
	int i,j,t;
	ans=0;
	sf(t);
	while(t--)
	{
		sfff(n,m,num);
		mem(a,0);
		int s,e;

		while(m--)
		{
			sff(s,e);
			a[s][e]=a[e][s]=1;
		}

	  solve();

	}
	return 0;
}
时间: 2024-10-12 21:42:38

ZOJ Friends(暴力)的相关文章

ZOJ 3844 暴力

---恢复内容开始--- Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and 

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

[数据结构暴力] zoj 3749 Chameleon

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3749 Chameleon Time Limit: 6 Seconds      Memory Limit: 65536 KB Given n groups of integers(all the integers are distinct). You should answer Q queries in this problem. Each query con

暴力 ZOJ 1403 Safecracker

题目传送门 1 /* 2 暴力:纯暴力,在家水水 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <iostream> 8 #include <string> 9 #include <vector> 10 #include <cmath> 11 using namespace std; 12 13 const i

zoj 3818 Pretty Poem(暴力处理字符串)2014年牡丹江赛区网络赛

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

Zoj 3616 Choir III 【有想法的暴力】【容斥】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3616 题目大意:给出n, m, b, g (0 < n <= 100, 0 < m <= 2000, 0 < b, g <= m * n)这些数字,下面n行,每一行有2*m个数字,第一个数字表示一个同学对于合唱的贡献,第二个数字表示同学的性别,b和g表示在一个矩形内至少有的男生和女生的数量.求一个矩形使得这个矩形内的对合唱的贡献值最大并

ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles Time Limit: 2 Seconds      Memory Limit: 65536 KB We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example,

ZOJ 1610 Count the Colors(线段树lazy+暴力统计)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

ZOJ Seven-Segment Display 暴力dfs + 剪枝

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For exampl

zoj 2976 Light Bulbs(暴力枚举)

Light Bulbs Time Limit: 2 Seconds      Memory Limit: 65536 KB Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get married this year. He bought a new house for his family and hired a