CodeForcesGym 100502G Outing
Outing
This problem will be judged on CodeForcesGym. Original ID: 100502G
64-bit integer IO format: %I64d Java class name: (Any)
Organising a group trip for the elderly can be a daunting task... Not least because of the fussy participants, each of whom will only make the trip on condition that some other participant also comes.
After some effort,you have taken from each of your participants a number, indicating that this participant will refuse to join the excursion unless the participant with that number also joins– the less choosy simply give their own number. This would be easy enough to resolve (just send all of them) but the bus you are going to use during the trip has only a fixed number of places.
Task
Given the preferences of all participants, find the maximum number of participants that can join.
Input
The first line of input contains two integers n and k (1 ≤ k ≤ n ≤ 1000), where n denotes the total number of participants and k denotes the number of places on the bus. The second line contains n integers xi for i = 1,2,...,n, where 1 ≤ xi ≤ n. The meaningof xi is that the i-th participant will refuse to join the excursion unless the xi-th participant also joins.
Output
Output one integer: the maximum number of participants that can join the excursion, so that all the participants’ preferences are obeyed and the capacity of the bus is not exceeded.
Sample Input 1
4 4
1 2 3 4
Sample Output 1
4
Sample Input 2
12 3
2 3 4 5 6 7 4 7 8 8 12 12
Sample Output 2
2
Sample Input 3
5 4
2 3 1 5 4
Sample Output 3
3
解题:强连通缩点+虚拟根节点+树形dp
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],low[maxn],dfn[maxn],belong[maxn],rs[maxn],tot,idx,scc;
bool instack[maxn],connect[maxn][maxn],dp[maxn][maxn];
int root,n,m,ind[maxn];
vector<int>g[maxn];
stack<int>stk;
void tarjan(int u) {
dfn[u] = low[u] = ++idx;
instack[u] = true;
stk.push(u);
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to);
low[u] = min(low[u],low[e[i].to]);
} else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
}
if(low[u] == dfn[u]) {
int v;
scc++;
do {
instack[v = stk.top()] = false;
belong[v] = scc;
rs[scc]++;
stk.pop();
} while(v != u);
}
}
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void init() {
while(!stk.empty()) stk.pop();
for(int i = ; i < maxn; ++i) {
dfn[i] = low[i] = ;
rs[i] = belong[i] = ;
instack[i] = false;
head[i] = -;
memset(dp[i],false,sizeof dp[i]);
memset(connect[i],false,sizeof connect[i]);
g[i].clear();
ind[i] = ;
}
tot = idx = scc = ;
rs[root = ] = ;
}
void dfs(int u) {
dp[u][] = dp[u][rs[u]] = true;
for(int i = g[u].size()-; i >= ; --i) {
int v = g[u][i];
dfs(v);
for(int j = m; j >= rs[u]; --j) {
if(dp[u][j]) continue;
for(int k = ; k <= j - rs[u]; ++k)
if(dp[v][k] && dp[u][j - k]) {
dp[u][j] = true;
break;
}
}
}
}
int main() {
int u;
while(~scanf("%d %d",&n,&m)) {
init();
for(int i = ; i <= n; ++i) {
scanf("%d",&u);
add(u,i);
}
for(int i = ; i <= n; ++i)
if(!dfn[i]) tarjan(i);
for(int i = ; i <= n; ++i) {
for(int j = head[i]; ~j; j = e[j].next) {
if(belong[i] == belong[e[j].to]) continue;
if(connect[belong[i]][belong[e[j].to]]) continue;
g[belong[i]].push_back(belong[e[j].to]);
ind[belong[e[j].to]]++;
connect[belong[i]][belong[e[j].to]] = true;
}
}
for(int i = ; i <= scc; ++i)
if(!ind[i]) g[root].push_back(i);
dfs(root);
int ret = ;
for(int i = m; i >= ; --i)
if(dp[root][i]) {
ret = i;
break;
}
printf("%d\n",ret);
}
return ;
}
CodeForcesGym 100502G Outing的更多相关文章
- Gym - 100502G Outing (强连通缩点+树形依赖背包)
题目链接 问题:有n个人,最多选k个,如果选了某个人就必须选他指定的另一个人,问最多能选多少个人. 将每个人所指定的人向他连一条单向边,则每一个点都有唯一的前驱,形成的图是个基环树森林,在同一个强连通 ...
- 公司outing选项
Sign up: 2014 Summer Outing 请您从以下三个方案中选择您最感兴趣的一个项目, 如果您不能参加此次summer outing, 请选择"遗憾放弃"- ...
- hihocoder 1154 Spring Outing
传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...
- Outing
Outing 题目描述 Organising a group trip for the elderly can be a daunting task... Not least because of t ...
- CSU - 1580 NCPC2014 Outing(树形依赖+分组背包)
Outing Input Output Sample Input 4 4 1 2 3 4 Sample Output 4 分组背包: for 所有的组k for v=V..0 for 所有的i属于组k ...
- CodeForcesGym 100512D Dynamic LCA
Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...
- CodeForcesGym 100517I IQ Test
IQ Test Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Orig ...
- CodeForcesGym 100517B Bubble Sort
Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...
- CodeForcesGym 100517H Hentium Scheduling
Hentium Scheduling Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...
随机推荐
- Compile OpenCASCADE7.3 with VS2008
Compile OpenCASCADE7.3 with VS2008 eryar@163.com 概述 在OpenCASCADE的源码文件夹中有个adm文件夹,里面提供了各个平台中编译源码的项目文件. ...
- JAVA学习第四十六课 — 其它对象API(二)Date类 & Calendar类(重点掌握)
Date类(重点) 开发时,会时常遇见时间显示的情况,所以必须熟练Date的应用 <span style="font-family:KaiTi_GB2312;font-size:18p ...
- django 笔记17 ModelForm
Model 数据库操作 验证 验证 Form -class LoginForm(Form): email = fields.EmailField() is_valid 每一个字段进行正则(字段内置正则 ...
- java 类和对象1
编写一个Java应用程序,该程序中有3个类:Lader.Circle和主类A.具体要求如下:Lader类具有类型为double的上底.下底.高.面积属性,具有返回面积的功能,包括一个构造方法对上底.下 ...
- Android项目实战(五十五):部分机型点击home再点图标进入程序不保留再之前界面的问题
解决办法: 1.在基类Activity中 添加方法 @Override public boolean moveTaskToBack(boolean nonRoot) { return super.mo ...
- PostgreSQL相关总结
源码安装PostgreSQL总结 简明安装步骤(其中prefix指定PostgreSQL的安装目录,该目录与数据目录pgdata和PostgreSQL的源代码包目录均无关) yum -y instal ...
- PUBG
题目描述 最近,喜爱ACM的PBY同学沉迷吃鸡,无法自拔,于是又来到了熟悉的ERANGEL.经过一番搜寻,PBY同学准备动身前往安全区,但是,地图中埋伏了许多LYB,PBY的枪法很差,希望你能够帮他找 ...
- bzoj 2259: [Oibh]新型计算机 最短路 建模
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- 搭建Lvs负载均衡群集
一.Lvs详解 lvs内核模型 1.模型分析 用户访问的数据可以进入调度器 匹配调度器分配的虚拟IP|IP+端口(路由走向) 根据调度器的算法确定匹配的服务器 2.调度条件:基于IP.基于端口.基于内 ...
- sql server 2000 自动收缩数据库大小
转载.......http://mars968.blog.163.com/blog/static/7400033200941642356258/ SQLServer2000压缩日志及数据库文件 ...