Outing
Outing
题目描述
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 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.
输出
样例输入
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的更多相关文章
- 公司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 ...
- 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 ...
- 题目3 : Spring Outing 微软2016校园招聘在线笔试第二场
题目3 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outin ...
- CodeForcesGym 100502G Outing
Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Origi ...
- BNUOJ-29358 Come to a spring outing 搜索,DP
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29358 状态虽然很多,但是非常稀疏,dfs搜索然后剪下枝.. 或者DP,f[i][j][k ...
- 【动态规划】【缩点】NCPC 2014 G Outing
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1793 题目大意: 一辆公交车,上面M个座位,N个人(M<=N<=1000) ...
- Gym - 100502G Outing (强连通缩点+树形依赖背包)
题目链接 问题:有n个人,最多选k个,如果选了某个人就必须选他指定的另一个人,问最多能选多少个人. 将每个人所指定的人向他连一条单向边,则每一个点都有唯一的前驱,形成的图是个基环树森林,在同一个强连通 ...
- Codeforces Gym100502G:Outing(缩点+有依赖的树形背包)
http://codeforces.com/gym/100502/attachments 题意:有n个点,容量为tol,接下来n个关系,表示选了第i个点,那么第xi个点就必须被选.问最多可以选多少个点 ...
随机推荐
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...
- java 读取excel(Map结构)xls
package com.sun.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import jav ...
- hdu 5664 Lady CA and the graph(树的点分治+容斥)
题意: 给你一个有n个点的树,给定根,叫你找第k大的特殊链 .特殊的链的定义:u,v之间的路径,经过题给的根节点. 题解:(来自BC官方题解) 对于求第k大的问题,我们可以通过在外层套一个二分,将其转 ...
- chapter5 函数
在lua中,函数是语句和表达式体现的主要机制.函数可以完成某些特定的任务.计算和返回执行结果. 前者当成一个语句,后者当成一个表达式: *,/) a = ) + ) print(os.date()) ...
- Threading
new System.Threading.Thread(new System.Threading.ThreadStart(ReadState)).Start();
- CURL访问url显示响应时间
curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.baidu.com 时间指 ...
- python paramiko模拟ssh登录,实现sftp上传或者下载文件
Python Paramiko模块的安装与使用详解 paramiko是短链接,不是持续链接,只能执行你设定的shell命令,可以加分号执行两次命令. http://www.111cn.net/phpe ...
- Jquery实现表格的分页
功能描述 该分页功能不是一次将所有记录都加载出来,然后在点击按钮翻页的时候,通过设置每一条记录的display属性展示或隐藏实现分页的效果.由于后台的获取数据的接口已实现分页的功能,所以在点击翻页按钮 ...
- GRPC: set up..
get the grpc source file.. git clone https://github.com/grpc/grpc git submodule update --init --recu ...
- 设计模式--静态工厂设计模式在android中的使用
今天看到这篇文章:http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html public c ...