zoj3710 friends(floyd变形)
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 thank friends in common, they will become friends in several days. Currently, there are totallyn 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. ThenT 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 integersui, vi (0 ≤ui, vi
<n, ui ≠ vi) indicating there is friendship between personui 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
大概思路:
1,数学方法?
2,二进制求交集+搜索+减枝?
3,暴力枚举?
大部分人用的floyd思想递推 :
i认识x,j认识x,那么i就有可能和j是朋友 ,找到i认识j的路径(x数)多等于k即可,如果可以认识,我们动态建路即可
对floyd的理解
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(...)...
其中k为最对循环次数,我们可以改成:
while(上一次有更新)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(...)...+标记更新
}
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
using namespace std;
bool map[101][101];
int n,m,k,ans;
void _update()
{
ans=0;
memset(map,false,sizeof(map));
}
void _in()
{
int x,y;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
map[x][y]=map[y][x]=true;
}
}
void _solve()
{
bool temp=true;
while(temp)
{
temp=false;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(map[i][j]) continue;
int cnt=0;
for(int f=0;f<n;f++)
if(map[i][f]&&map[j][f])
cnt++;
if(cnt>=k) {
ans++;
map[i][j]=map[j][i]=true;
temp=true;
}
}
}
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
_update();
_in();
_solve();
}
}
zoj3710 friends(floyd变形)的更多相关文章
- UVA10048 Audiophobia[Floyd变形]
UVA - 10048 Audiophobia Consider yourself lucky! Consider yourself lucky to be still breathing and h ...
- POJ2253——Frogger(Floyd变形)
Frogger DescriptionFreddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fi ...
- hdu 1596(Floyd 变形)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/ ...
- hdu 1217 (Floyd变形)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others) ...
- Frogger(floyd变形)
Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- UVa 10048 (Floyd变形) Audiophobia
题意: 给一个带权无向图,和一些询问,每次询问两个点之间最大权的最小路径. 分析: 紫书上的题解是错误的,应该是把原算法中的加号变成max即可.但推理过程还是类似的,如果理解了Floyd算法的话,这个 ...
- find the mincost route(floyd变形 无向图最小环)
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu1569find the safest road(floyd变形求最大安全值)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4034 Graph(Floyd变形——逆向判断)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4034 Problem Description Everyone knows how to calcu ...
随机推荐
- zookeeper原理介绍
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt354 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它 ...
- Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自动化发布和部署
写在前面 Docker一直很火热,一直想把原本的Jenkins自动部署工具搬到Docker上面,无奈今年一直忙于各种事情,迟迟未实施这个事情,正好迎来了dotnet core 2.0 的正式发布,升级 ...
- 如何解决xshell中无法输入中文的问题
自从安上了xshell以后,用着那叫一个顺手,美中不足的就是一直无法输入中文.不过,既然学习IT,就要习惯英文嘛~直到--我遇到了脚本,写好一个脚本,必要的注释是少不了的,但是作为一个英文渣渣,我真的 ...
- NullpointerException处理
毫无疑问,空指针NullpointerException是我们最常遇到异常,没有之一! 在刚进入编程职业时,我想,大部分刚进入的同学肯定会受到前辈们的叮咛:一定要防止空指针,这是个低级错误.你们不是? ...
- 使用BootStrap框架设置全局CSS样式
一.排版 标题 HTML 中的所有标题标签,<h1> 到 <h6> 均可使用.另外,还提供了 .h1 到 .h6 类,为的是给内联(inline)属性的文本赋予标题的样式. & ...
- 团队作业8——第二次项目冲刺(Beta阶段)--5.24 forth day
团队作业8--第二次项目冲刺(Beta阶段)--5.24 forth day Day four: 会议照片 项目进展 Beta冲刺的第四天,以下是今天具体任务安排: 队员 昨天已完成的任务 今日计划完 ...
- 201521123107 《Java程序设计》第1周学习总结
第1周学习总结 1.本周学习总结 本周我们正式开始了对一门新的编程语言java的学习.本周的主要内容是初步了解了java的发展过程,java具有简约且简单 .平台无关性等优点.java的3个关键的工具 ...
- Swing-布局管理器之GridLayout(网格布局)-入门
注:本文内容源自于三十一.Java图形化界面设计——布局管理器之GridLayout(网格布局),笔者在学习过程中根据自身理解修改了部分代码. 网格布局特点: l 使容器中的各组件呈M行×N列的网格 ...
- 201521123075 《Java程序设计》第6周学习总结
1. 本周学习总结 2. 书面作业 1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 答:第一,要覆盖clon ...
- 201521123089 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码1.1 com.parent包中Child.java文件能否编译通过?哪 ...