Outing

题目描述

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.
Given the preferences of all participants, find the maximum number of participants that can join.

输入

The first line of input contains two integers n and k (1 ≤ k ≤ n ≤ 1 000), where n denotes the total number of participants and k denotes the number of places on the bus.
The second line contains n integers x i for i = 1, 2, . . . , n, where 1 ≤ x i ≤ n. The meaning of x i is that the i-th participant will refuse to join the excursion unless the x i -th participant also joins.

输出

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.

样例输入

4 4
1 2 3 4

样例输出

4
分析:先求下强连通分量,然后图就变成了树,团或树指向团;
   然后对于树或团直接01背包,对于树指向团的可取min——max,背包可用差分优化;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,pre[maxn],link[maxn],sccno[maxn],dfs_clock,scc_cnt,cas,bel1[maxn],pk[maxn],dp[maxn],vis[maxn],sum[maxn];
vi e[maxn],to[maxn],bel[maxn];
stack<int>s;
void bfs(int p)
{
queue<int>q;
for(int x:bel[p])for(int y:to[x])if(sccno[y]!=p)q.push(y),pk[p]++,vis[sccno[y]]++;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int y:to[x])
{
q.push(y);
pk[p]++;
vis[sccno[y]]=;
}
}
}
void dfs(int u)
{
pre[u]=link[u]=++dfs_clock;
s.push(u);
for(int x:e[u])
{
if(!pre[x])
{
dfs(x);
link[u]=min(link[u],link[x]);
}
else if(!sccno[x])
{
link[u]=min(link[u],pre[x]);
}
}
if(link[u]==pre[u])
{
scc_cnt++;
while(true)
{
int x=s.top();
s.pop();
sccno[x]=scc_cnt;
bel[scc_cnt].pb(x);
bel1[scc_cnt]++;
if(x==u)break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=;
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++)
if(!pre[i])dfs(i);
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)
{
int a;
scanf("%d",&a);
e[i].pb(a),to[a].pb(i);
}
find_scc(n);
rep(i,,scc_cnt){pk[i]=bel1[i];if(bel1[i]!=)bfs(i);}
dp[]=;
rep(i,,scc_cnt)
{
if(vis[i])continue;
int x=bel1[i],y=pk[i];
rep(j,,m)sum[j]=sum[j-]+dp[j];
for(j=m;j>=;j--)
{
if(j<x)break;
else if(j>=x&&j<=y)dp[j]=;
else if(sum[j-x]-sum[j-y-])dp[j]=;
}
}
for(i=m;dp[i]==;i--);
printf("%d\n",i);
//system("Pause");
return ;
}

Outing的更多相关文章

  1. 公司outing选项

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

  2. hihocoder 1154 Spring Outing

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

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

  4. 题目3 : Spring Outing 微软2016校园招聘在线笔试第二场

    题目3 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outin ...

  5. CodeForcesGym 100502G Outing

    Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Origi ...

  6. BNUOJ-29358 Come to a spring outing 搜索,DP

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29358 状态虽然很多,但是非常稀疏,dfs搜索然后剪下枝.. 或者DP,f[i][j][k ...

  7. 【动态规划】【缩点】NCPC 2014 G Outing

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1793 题目大意: 一辆公交车,上面M个座位,N个人(M<=N<=1000) ...

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

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

  9. Codeforces Gym100502G:Outing(缩点+有依赖的树形背包)

    http://codeforces.com/gym/100502/attachments 题意:有n个点,容量为tol,接下来n个关系,表示选了第i个点,那么第xi个点就必须被选.问最多可以选多少个点 ...

随机推荐

  1. 3.通过js代码设置css样式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Date Math SimpleDateFormat 类

    Date  和 SimpleDateFormat /* Date 日期类 许多方法都被Calendar取代了 Date() 获取当前时间 使用概率最高 Calendar 类用常量获取当前时间 一般很少 ...

  3. 关于oracle数据库(1)

    兼容性的设置 cmd.exe是微软Windows系统的命令行程序,类似于微软的DOS操作系统.cmd.exe是一个16/32位的命令行程序,运行在Windows NT/2000/XP/2003/Vis ...

  4. mycat 配置全局表

    在已配置全局序列号的基础上 1.schema.xml <table name="hf_ctrlorder_split_config" primaryKey="HF_ ...

  5. input之placeholder与行高的问题

    我们实现一个输入框的视觉的时候为了保持其各种各样的兼容性: 1.鼠标要跟文字一样高度. 2.文字要居中对齐. 3.还要有placeholder 第一个目标,当实现一个高度为40像素的高度输入框时,为了 ...

  6. Python Tools

    [TOC] Python virtualenv.fabric 和 pip 是 pythoneer 的三大神器 pip pip pip是一个安装和管理Python包的工具,是easy_install的一 ...

  7. Load$$ execution region symbols

    6.3.3 Load$$ execution region symbols The linker generates Load$$ symbols for every execution region ...

  8. js键盘键值大全

    原文地址:http://blog.csdn.net/avenccssddnn/article/details/7950524 js键盘键值 keycode 8 = BackSpace BackSpac ...

  9. 面向对象的特性-利用prototype为类创建静态成员

    —————————————————————————— <script type="text/javascript">            //用function模拟一 ...

  10. linux fdisk

    fdisk 对硬盘及分区的操作,进入fdisk 对硬盘操作阶段 我们可以对硬盘进行分区操作,前提是您把fdisk -l 弄明白了:通过fdisk -l ,我们能找出机器中所有硬盘个数及设备名称:比如上 ...