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 ...
随机推荐
- hdoj--1556--Color the ball(模拟&&树状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 11.字符,字符常见开发,_itoa函数
各种字符所占字节 wchar_t wch = L'我'; //占4个字节 char ch;//占1个字节 printf("%d\n", sizeof("A")) ...
- spring boot 集成 mybatis,数据库为mysql
导入mven工程即可运行,方法不描述了,具体见 https://github.com/davidwang456/spring-boot-mybatis-demo
- ListView优化-ViewHolder缓存
安卓开发中ListView控件是一个使用频率相当的高级控件,通常用于展示一系列相似度极高的数据,当数据量极大或布局相当复杂时,ListView的性能优化就显得非常重要.所以在开发中不但功能上要满足,而 ...
- js thousand separator and change td content
js thousand seprator and change TD content // integer function addCommas(n){ })/; return String(n).r ...
- vuejs on
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Bayes++ Library入门学习之熟悉class-Bayesian_filter_base(1)
在对Bayes++库的名称空间有了一个大概的了解之后,我们开始学习该名称空间下的第一个子类Bayesian_filter::Bayes_filter_base. 该类与其子类的继承关系图如下图所示. ...
- [笔记-图论]Dijkstra
用于求正权有向图 上的 单源最短路 优化后时间复杂度O(mlogn) 模板 // Dijkstra // to get the minumum distance with no negtive way ...
- HAOI树上染色
Description : 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后, ...
- Raspberry Pi - 调整你的SD卡分割区的大小
在使用Win32DiskImager为一张空白的SD卡刷入新的Rasbian系统后,卡上的可用剩余空间并不大, 本人有一张8G的SD卡,但是刷入4.1的Rasbian后,用df -h查看,根目录下的空 ...