Google Codejam 2016 Round1A Problem C BFFs 简单图论
链接
题意
n个小朋友要坐成一个圈。每个小朋友心中都有一个Best Friend Forever。要保证每个人的左右至少有一个是他的BFF,问最多能让多少人做成一个圈。
思路
n只有1000,每个点的出度也都是1。看上去就不需要套用某种算法。综合的考虑一下,如果有m个人的BFF关系正好形成一个环,那么他们就可以成为一种结果,并且这个环中不能有别的人插入了。另外一种情况就是一条链,在这条链中有至少两个点A和B的BFF关系是相互的,A侧的人的关系依次指向A,B侧依次指向B,那么这条链也是可以成为结果,并且可以有多条这样的链构成最够的答案。
所以方法就是先求环的最大值,再累加出链的最大值。
代码
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 1005
using namespace std;
bool vis[MAXN][MAXN], mp[MAXN];
int cnt[MAXN];
int a[MAXN], d[MAXN];
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T;
scanf("%d", &T);
int cas = 0;
while (T--){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
}
memset(cnt, 0, sizeof(cnt));
memset(d, 0, sizeof(d));
int ans = 0;
for (int i = 1; i <= n; ++i){
memset(mp, 0, sizeof(mp));
int x = i;
mp[x] = true;
while (true){
x = a[x];
if (mp[x]) break;
mp[x] = true;
++cnt[i];
}
if (x == i){
ans = max(ans, cnt[i] + 1);
}
}
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
if (a[a[j]] == j) continue;
d[a[j]] = max(d[a[j]], d[j] + 1);
}
}
int res = 0;
for (int i = 1; i <= n; ++i){
if (a[a[i]] != i) continue;
res += d[i] + d[a[i]] + 2;
}
res >>= 1;
ans = max(ans, res);
printf("Case #%d: %d\n", ++cas, ans);
}
}
Google Codejam 2016 Round1A Problem C BFFs 简单图论的更多相关文章
- Google CodeJam 2016 round3-A.Teaching Assistant
题目描述: 原题是纯英文,大意是:你每天可以选择一门课去学习,选题和提交答案.题目为Coding或者Jamming.选的题目如果和老师选的一致,提交答案也匹配,最后可以得10分,若选题不一致只能得5分 ...
- Google图片加载库Glide的简单封装GlideUtils
Google图片加载库Glide的简单封装GlideUtils 因为项目里用的Glide的地方比较多,所有简单的封装了以下,其实也没什么,就是写了个工具类,但是还是要把基础说下 Glide的Githu ...
- 回顾Google IO 2016 -Keynote【图解】
Google IO大会倒计时进行中~~ 两名演奏者在使用高空“古筝”. 最后5秒倒计时~~~~全场轰动~ 倒计时结束,IO大会正式开始.屏幕中,一个人把纯白的唱片放入唱片机中. 然后欢快的音乐响起,台 ...
- cdoj1580 简单图论问题
地址:http://acm.uestc.edu.cn/#/problem/show/1580 题目: 简单图论问题 Time Limit: 3000/1000MS (Java/Others) ...
- lh的简单图论
lh的简单图论 http://10.64.70.166/problem/1112 lh的简单图论 Description 众所周知,集训队的lh同学txdy,有一天他在写着代码,突然哼起了rapr ...
- 2249: Altruistic Amphibians 01背包的应用 + lh的简单图论 图转树求lca
第一个 写了两个比较简单的数论题目,就是整除理论的两个题目,第一个题目比较蠢,第二个稍微要动一点脑筋 Codeforces Round #347 (Div. 2) – A. Complicated G ...
- [Google Codejam] Round 1A 2016 - The Last Word
[Problem Description] Problem On the game show The Last Word, the host begins a round by showing the ...
- Google Code Jam 资格赛: Problem A. Magic Trick
Note: To advance to the next rounds, you will need to score 25 points. Solving just this problem wil ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
随机推荐
- CentOS进入图形界面
CentOS进入图形界面 学习了: http://www.centoscn.com/CentosBug/osbug/2014/0831/3620.html http://bbs.csdn.net/to ...
- JQuery与CSS之图片上放置button
position:relative日常应用的时候通常是设置给position:absolute;的父层的, 父层position:relative; 子层position:absolute;的话, 就 ...
- hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心
将兔子的血量从小到大排序,箭的威力也从小到大排序, 对于每仅仅兔子将威力大于血量的箭增加队列,写个优先队列使得出来数位价钱最少.. #include<stdio.h> #include&l ...
- 基于MySQL元数据的Hive的安装和简单測试
引言: Hive是一种强大的数据仓库查询语言,类似SQL,本文将介绍怎样搭建Hive的开发測试环境. 1. 什么是Hive? hive是基于Hadoop的一个数据仓库工具,能够将结构化的数据文件映射为 ...
- ubuntu 休眠之后蓝牙鼠标无效果。
ubuntu链接蓝牙鼠标之后.左上角蓝牙标志左下角应该有一个锁的标志. 可是休眠之后,蓝牙鼠标失效,锁没有了,点击按键,出来锁之后,立即消失. 运行两次例如以下命令能够解决: sudo hciconf ...
- 苹果要求全部新app以及版本号更新必须支持iOS 8 SDK和64-bit
2014年10月20日.苹果官方公布了一则新闻,新闻内容例如以下: Starting February 1, 2015, new iOS apps uploaded to the App Store ...
- jmeter脚本编写之五类常见请求编写
1.普通post请求 2.普通json请求 3.带query參数的json请求 4.xml请求 5.上传请求 starting (Windows系统 点击 F12 调出开发人员工具,选择Network ...
- Calender
public static void main(String[] args) { // TODO 自动生成的方法存根 Calendar c = new GregorianCalendar(); c., ...
- BZOJ 2179 FFT模板
思路:FFT板子题 //By SiriusRen #include <cstdio> #include <complex> using namespace std; typed ...
- spark thrift server configuration
# MainApplicationProperties # --master yarn --deploy-mode client 下的配置, client 模式表示,driver 是在本地机器上跑的, ...