Outing

Time Limit: 1000ms
Memory Limit: 524288KB

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的更多相关文章

  1. Gym - 100502G Outing (强连通缩点+树形依赖背包)

    题目链接 问题:有n个人,最多选k个,如果选了某个人就必须选他指定的另一个人,问最多能选多少个人. 将每个人所指定的人向他连一条单向边,则每一个点都有唯一的前驱,形成的图是个基环树森林,在同一个强连通 ...

  2. 公司outing选项

    Sign up:  2014 Summer Outing   请您从以下三个方案中选择您最感兴趣的一个项目, 如果您不能参加此次summer outing, 请选择"遗憾放弃"- ...

  3. hihocoder 1154 Spring Outing

    传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...

  4. Outing

    Outing 题目描述 Organising a group trip for the elderly can be a daunting task... Not least because of t ...

  5. 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 ...

  6. CodeForcesGym 100512D Dynamic LCA

    Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  7. CodeForcesGym 100517I IQ Test

    IQ Test Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Orig ...

  8. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  9. CodeForcesGym 100517H Hentium Scheduling

    Hentium Scheduling Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

随机推荐

  1. angularjs 缓存 $q

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  2. Java JNI 入门篇——传递数组与修改数组

    这里不在重复JavaJNI 的开发过程了,不熟悉的同学请参考:Java JNI HelloWorld 直接上主要代码: ArrayJNI.Java package com.example.jni; p ...

  3. HDU4825:Xor Sum 解题报告(0/1 Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数. 随后 Prometheus 将向 Ze ...

  4. OpenCV问题集锦,图片显示不出来的问题,cvWaitKey(0),不能读图片,未经处理的异常,等问题集合

    昨天根据uc伯克利的人工图像分割文件.seg,显示图像的时候调用了OpenCV的库函数,图片都能用imwrite写好,但是imshow死活显示不出来. 今天早上发现原来是imshow()后面应该加上: ...

  5. Java 类和对象7

    创建一个三角形类,成员变量三边,方法求周长,创建类主类A来测试它. public class sanjiaoxing { private double a; private double b; pri ...

  6. RSA加密的方式和解密方式

    RSAsecurity.java package com.mstf.rsa; import java.security.KeyFactory; import java.security.KeyPair ...

  7. Mac or windows eclipse配置tomcat

    1.选择window --> Preferences 首选项 2.选择server --> Runtime Environements --> Add 3.选择对应的tomcat版本 ...

  8. input的选中与否以及将input的value追加到一个数组里

    html布局 <div class="mask"> //每一个弹层都有一个隐藏的input <label> <input hidden="& ...

  9. phpstorm10安装并汉化

    一.下载phpstorm 下载地址:https://pan.baidu.com/s/1R64ZROVP1ljGbYfCwWjwxA 二.一直点击下一步安装即可 注意:第3步的时候选择一下支持的后缀 三 ...

  10. bzoj1604 牛的邻居 STL

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...