Friends (ZOJ - 3710)
Problem
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 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.
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
题解:暴力遍历所有朋友直到不再出现新的朋友关系就可以了。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
int vis[110][110]; //用来判断是否是朋友关系
int main()
{
int n,i,j,m,t,k,ans,x,y,l;
while(scanf("%d",&t) != EOF)
{
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
memset(vis,0,sizeof(vis));
for(i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
vis[x][y] = 1;
vis[y][x] = 1;
}
ans = 0;
m = 0;
while(1)
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i==j||vis[i][j]) continue; // 如果是自己或者已经是朋友关系就不用判断了
x = 0;
for(l=0; l<n; l++)
{
if(vis[i][l]&&vis[j][l])
x++;
}
if(x>=k)
{
ans++;
vis[i][j] = vis[j][i] = 1;
}
}
}
if(m==ans) break; // 如果和上次循环的结果一样,就说明不会再增加新的朋友关系了,跳出循环就可以了
m = ans;
}
printf("%d\n",ans);
}
}
return 0;
}
Friends (ZOJ - 3710)的更多相关文章
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- 2014 牡丹江现场赛 A.Average Score(zoj 3819) 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5373 题目意思: 有两个class:A 和 B,Bob 在 Clas ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
- POJ 1595 Prime Cuts (ZOJ 1312) 素数打表
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...
- POJ 2590 Steps (ZOJ 1871)
http://poj.org/problem?id=2590 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871 题目大 ...
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
- 2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)
Alice's Print Service Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is providing print ser ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- Applications (ZOJ 3705)
题解:就是题目有点小长而已,可能会不想读题,但是题意蛮好理解的,就是根据条件模拟,计算pts.(送给队友zm. qsh,你们不适合训练了.) #include <iostream> #in ...
随机推荐
- tuxedo10g3R的安装过程(原文url:http://www.cnblogs.com/xianfish/articles/1955244.html)
RHEL 5.1 32Bit 安装配置TUXEDO 10R3 [前言]: 目的: 在Linux上安装配置Tuxedo 10R3 环境: RHEL 5.1 32Bit [前提条件]: 下载Tuxed ...
- redis哈希表数据类型键的设置
命令名称:hset 语法:hset key field value 功能: 1)将哈希表key中的域field的值设为value. 2)如果key不存在,一个新的哈希表被创建并进行hset操作. 3) ...
- C# DataTable和List转换操作类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- C#项目中窗体的ShowDialog()和show()的区别
ShowDialog()弹出的窗体为模式化窗体: show()弹出的窗体为非模式化窗体: 模式化窗体与非模式化窗体的区别: 模式化窗体会使程序中断,直到关闭窗体: 打开窗体后不能替换到其他窗体: 子窗 ...
- 前端开发 Vue -1windows环境搭建Vue Node开发环境
解决几个疑问: 想学习下vue.js,我理解的它是一个前端的框架,主要作用是对数据的处理,和juqery类似,所以不太理解为什么要在nodejs中npm install vue呢?在html文件中引入 ...
- XML-RPC-1概述
XML-RPC是一个远程过程调用(远端程序呼叫)(remote procedure call,RPC)的分布式计算协议,通过XML将调用函数封装,并使用HTTP协议作为传送机制. 中文名 XML- ...
- 【vue开发】vue导出Excel表格教程&demo
前端工作量最多的就是需求,需求就是一直在变,比如当前端数据写完之后,需要用Excel把数据下载出来:再比如前端在没有数据库想写些demo玩时,也是很好的选择. 第一步安装依赖包,修改配置 1.装依赖: ...
- 批量更新sql
跨库 批量更新 UPDATE a.table_1upINNER JOIN ( SELECT user_id, user_org_company_id, FROM b.table_2) AS tmp O ...
- 小白_开始学Scrapy__原理
整体架构 引擎(Scrapy Engine),用来处理整个系统的数据流处理,触发事务. 调度器(Scheduler),用来接受引擎发过来的请求,压入队列中,并在引擎再次请求的时候返回. 下载器(Dow ...
- 【leetcode】339. Nested List Weight Sum
原题 Given a nested list of integers, return the sum of all integers in the list weighted by their dep ...