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 ...
随机推荐
- HTML5 界面元素 Canvas 參考手冊
HTML5 界面元素 Canvas 參考手冊 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...
- android JNI 一维数组、二维数组的访问与使用
在JNI中访问JAVA类中的整型.浮点型.字符型的数据比较简单,举一个简单的例子,如下: //得到类名 jclass cls = (*env)->GetObjectClass(env, obj) ...
- 语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
语法错误: unexpected ''); ?></span></span></h2>' (T_CONSTANT_ENCAPSED_STRING), expe ...
- Struts2国际化-getText()方法
转自https://blog.csdn.net/qq_43560838/article/details/83747604 一:简单理解 国际化简称i18n,其来源是英文单词 international ...
- Tuple assignment
It is often useful to swap the values of two variables. With conventional assignments, you have to u ...
- 集合TreeSet的使用
集合中的TreeSet是集合体系结构中的底层实现,是Collection的孙子,Set的儿子.TreeSet除拥有父接口的特点外,还有其自身的特点.下面就看看TreeSet的排序是怎么实现的.从它的构 ...
- Mojo Associated Interfaces
Mojo Associated Interfaces yzshen@chromium.org 02/22/2017 Background Before associated interfaces ar ...
- WebRTC Native APIs
WebRTC Native APIs The WebRTC Native APIs implementation is based on W3C’s WebRTC 1.0: Real-time Com ...
- MNIST手写数字数据集
下载python源代码之后,使用: import input_data mnist = input_data.read_data_sets('MNIST_data/',one_hot=True) 下载 ...
- webstorm 添加 autoprefixer 工具为CSS加前缀
webstrom IDE 的 setting (快捷键 Ctrl + Alt + S) Tool -- External tool (绿色 + 添加) 3.填写 必要的项目 后 apply 备注:N ...