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. MongoDB文档基本操作

    一.插入文档 使用insert()或save()方法向集合插入文档 >db.COLLECTION_NAME.insert(document) 详细用法可以参考MongoDB菜鸟教程 二.查找文档 ...

  2. (转) QImage总结

    嗯,这个QImage的问题研究好久了,有段时间没用,忘了,已经被两次问到了,突然有点解释不清楚,我汗颜,觉得有必要重新总结下了,不然无颜对自己了. 图像的数据是以字节为单位保存的,每一行的字节数必须是 ...

  3. JS 之完美运动框架 如何同时改变元素多个属性?

    正如改变一个属性的方法,可以使用已经写好的运动函数如move(obj,attr,target,fn);我们可能会想这样做, 调用两次运动函数,如同时改变宽和高,move(obj,'width',tar ...

  4. centos 6 安装docker

    安装 Fedora EPEL 1.yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarc ...

  5. webapi中Route标签定义可选参数

    Optional URI Parameters and Default Values You can make a URI parameter optional by adding a questio ...

  6. B - ACboy needs your help(动态规划,分组背包)

    B - ACboy needs your help Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  7. this is it

    hello everyone: 我是光哥.以后就在这里更新博客了.^_^ 主要更新一些学习记录,再加点别的吧 2015-8-21 许昌 申庄 吃饭去了,下午2:27的车去郑州!

  8. Servlet详解

    原文出处:http://blog.csdn.net/q547550831/article/details/50458456 Servlet详解 基本概述 Session在计算机中,尤其是在网络应用中, ...

  9. Calendar.getInstance()获取当天指定点上的时间[转载]

    ctoday.add(Calendar.DAY_OF_MONTH, 1); 明天时间 //获得当天0点时间public static int getTimesmorning(){Calendar ca ...

  10. 11--Python 备份文件程序

    最近看了下<A Byte of Python>, 看见一个非常有意思的程序,用python进行文件行备份的练习程序, 自己在机器上敲代码运行了一遍,结果出现了一个小问题,路径出错--&qu ...