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 ...
随机推荐
- iOS开发- Images can’t contain alpha channels or transparencies 解决的方法
新版的iTunesConnect真是各种问题阿... 蛋疼要命. 上传介绍图片(屏幕截图)的时候 , 遇到了这个问题 Images can't contain alpha channels or tr ...
- rest_framework-认证-总结完结篇
执行过程 APIView() Ruquest() Authentication() OrderView()APIView() def duspatch: self.initial(request) d ...
- 从Git里拉取远程的所有分支
从Git里拉取远程的所有分支 git branch -r | grep -v '\->' | while read remote; do git branch --track "${r ...
- Dictionaries
A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...
- Spring 4 CustomEditorConfigurer Example--转
原文地址:http://howtodoinjava.com/spring/spring-core/registering-built-in-property-editors-in-spring-4-c ...
- ASP.NET Identity 角色管理(Roles)
当我们使用ASP.NET 4.5创建模板项目时,会发现模板只提供了ApplicationUserManager用于用户的登录注册.修改.设置等,而没有提供与用户角色相关的代码,对此就需要我们自己手动的 ...
- Adobe 2015 CC update (Windows/Mac OS) 独立升级包下载 Adobe Photoshop CC (Windows 32bit)
Adobe CC 2015 Product Updates/Downloads for Windows ** = To access these updates, please first follo ...
- win10 的MQTT + apache-apollo服务器使用
我的使用环境是windows10 2.下载文件目录(注意:开始看教程说直接打开bin目录下的apollo.cmd文件,闪退,原因是没有java_home环境,必须添加java环境): 3.安装好jav ...
- 维生素C主要生理功能
维C是:维生素C又叫抗坏血酸,是一种水溶性维生素. 维生素C主要生理功能 1. 促进骨胶原的生物合成.利于组织创伤口的更快愈合: 维生素C在体内参与多种反应,如参与氧化还原过程,在生物氧化和还原作用以 ...
- 记intel杯比赛中各种bug与debug【其一】:安装intel caffe
因为intel杯创新软件比赛过程中,并没有任何记录.现在用一点时间把全过程重演一次用作记录. 学习 pytorch 一段时间后,intel比赛突然不让用 pytoch 了,于是打算转战intel ca ...