CSU 1580 Outing 强连通+背包

题目链接:点击打开链接

给定n个人,车的载人量m

下面给出a[i]数组

想要邀请i上车,必须先邀请a[i]上车

问:最多能邀请到多少人。

观察得到,这是一个有向图,按照i->a[i]建边后得到的图是类似于树形,但链的尾部是一个简单环。

如下:

5 2
2 3 4 1 4

则我们必须先同时邀请1234,才能邀请5.

所以建立一个反图(即边的方向相反),然后强连通缩点一下,这样就得到了一个森林(多个树的图)。

且对于一个树,只有根节点是需要同时邀请的(因为根节点是个环,子节点都是单个点),而子节点是可以单个邀请,邀请任意数量的。

所以实际上对于一个树我们只需要关心这个树的根节点本身的点数siz[root]  以及   根节点子树的点数son[root](不包括根节点本身)

dfs一下处理出siz 和 son

然后利用这两个进行背包

必须先用siz背包,得到的基础上用son背包。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if(c=getchar(),c==EOF) return 0;
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	sgn=(c=='-')?-1:1;
	ret=(c=='-')?0:(c-'0');
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
	ret*=sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}

const int N = 1005;
//N为最大点数
const int M = 3000;
//M为最大边数
int n, m;//n m 为点数和边数

struct Edge{
    int from, to, nex;
    bool sign;//是否为桥
}edge[M<<1];
int head[N], edgenum;
void add(int u, int v){//边的起点和终点
    Edge E={u, v, head[u], false};
    edge[edgenum] = E;
    head[u] = edgenum++;
}

int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
int taj;//连通分支标号,从1开始
int Belong[N];//Belong[i] 表示i点属于的连通分支
bool Instack[N];
vector<int> bcc[N]; //标号从1开始

void tarjan(int u ,int fa){
    DFN[u] = Low[u] = ++ Time ;
    Stack[top ++ ] = u ;
    Instack[u] = 1 ;

    for (int i = head[u] ; ~i ; i = edge[i].nex ){
        int v = edge[i].to ;
        if(DFN[v] == -1)
        {
            tarjan(v , u) ;
            Low[u] = min(Low[u] ,Low[v]) ;
            if(DFN[u] < Low[v])
            {
                edge[i].sign = 1;//为割桥
            }
        }
        else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;
    }
    if(Low[u] == DFN[u]){
        int now;
        taj ++ ; bcc[taj].clear();
        do{
            now = Stack[-- top] ;
            Instack[now] = 0 ;
            Belong [now] = taj ;
            bcc[taj].push_back(now);
        }while(now != u) ;
    }
}

void tarjan_init(int all){
    memset(DFN, -1, sizeof(DFN));
    memset(Instack, 0, sizeof(Instack));
    top = Time = taj = 0;
    for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意开始点标!!!
}
vector<int>G[N];
int du[N], siz[N], son[N];
void suodian(){
    memset(du, 0, sizeof(du));
    for(int i = 1; i <= taj; i++)G[i].clear();
    for(int i = 0; i < edgenum; i++){
        int u = Belong[edge[i].from], v = Belong[edge[i].to];
        if(u!=v)G[u].push_back(v), du[v]++;
    }
    memset(siz, 0, sizeof siz);
    for(int i = 1; i <= n; i++)siz[Belong[i]]++;//统计新图中每个点实际上包括了旧图中多少个点
}
void init(){memset(head, -1, sizeof(head)); edgenum=0;}
void dfs(int u){
	son[u] = 0;
	for(int i = 0; i < G[u].size(); i++){
		int v = G[u][i];
		dfs(v);
		son[u] += son[v] + siz[v];
	}
}
int dp[N], tmp[N];
void bei(int x){
	for(int j = 0; j + x <= m; j++)
		dp[j] = max(dp[j], dp[j+x]+x);
}
void work(int x){
	dfs(x);
//	printf("(%d,%d)\n", siz[x], son[x]);
	memset(tmp, -1, sizeof tmp);
	for(int j = 0; j + siz[x] <= m; j++)
	tmp[j] = max(tmp[j], dp[j+siz[x]]+siz[x]);
//	for(int i = 0; i <= m; i++)cout<<tmp[i]<<" ";puts("");
	for(int i = 1; i <= son[x]; i++)
	{
		for(int j = 0; j+1<=m; j++)
		if(tmp[j+1]!=-1)
		tmp[j] = max(tmp[j], tmp[j+1]+1);
	}
	for(int i = 0; i <= m; i++)dp[i] = max(dp[i], tmp[i]);
}
int main(){
	while(~scanf("%d %d", &n, &m)){
		init();
		for(int i = 1, x; i <= n; i++){
			rd(x);
			add(x, i);
		}
		tarjan_init(n);
		suodian();
//		for(int i = 1; i <= taj; i++)printf("%d ", siz[i]);puts(" --siz");

		memset(dp, 0, sizeof dp);
		for(int i = 1; i <= taj ; i++)if(du[i] == 0)work(i);

		cout<<dp[0]<<endl;
	}
	return 0;
}
/*
4 4
2 3 3 4

ans:4

4 2
2 3 4 1

ans:0

6 4
2 3 4 5 6 3

ans:2

12 3
2 3 4 5 6 7 3 9 10 11 12 8
12 11
2 3 4 5 6 7 3 9 10 11 12 8

*/
时间: 2024-07-31 11:49:10

CSU 1580 Outing 强连通+背包的相关文章

CSU1580: Outing(强连通+拓扑排序+dp)

Description Input Output Sample Input 4 4 1 2 3 4 Sample Output 4 HINT Source NCPC 2014 #include<stdio.h> #include<vector> #include<string.h> using namespace std; const int N = 1005; int dfn[N],low[N],Stack[N],flag[N],vist[N],num[N],top,

csu 1547: Rectangle (01背包)

1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MB Submit: 716  Solved: 197 [Submit][Status][Web Board] Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 *

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th

CSU 1612: Destroy Tunnels 强连通分量 Kosaraju算法

链接 :  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1612 题意: 给一个矩阵A 大小N*N, B = A^1 + A^2 + A^3 + .... A^n , B中是否存在非0项. 题目可以转化为 N个点 编号为1-n, 对于任意点v,经过一些任意的步长到达u (u为所有点集的任意一个).离散数学里有图的矩阵相关知识 A^k代表了矩阵中从i到j的步长为k的方法数. 题目就是求整个图是否强连通. 学习了 Kosaraju算法 可以轻

csu 1547(01背包)

1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 996  Solved: 277[Submit][Status][Web Board] Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m)

CSU 1617: Itself is Itself(强连通缩点)思想转换到图论

1617: Itself is Itself Time Limit: 6 Sec  Memory Limit: 128 MB Submit: 21  Solved: 4 [Submit][Status][Web Board] Description Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels ext

BZOJ 2427 软件安装(强连通分量+树形背包)

题意:现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现在有个问题:软件之间存在依赖关系,即软件i只有在安装了软件j(包括软件j的直接或间接依赖)的情况下才能正确工作(软件i依赖软件j).幸运的是,一个软件最多依赖另外一个软件.如果一个软件不能正常工作,那么它能够发挥的作用为0. # include <cstdio> # include <cstri

csu 1640 机智的刷题方式(完全背包)

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <s

CSU 1526 Beam me out! 强连通

题目链接:点击打开链接 题意: 给定n个点的有向图(1为起点,n为终点) 下面每两行给出一个点的出度和所连接的下一个点. 第n个点是没有出度的 图是这样的: 1->2, 1->3, 2->3 第一问: 若存在一种方案使得这个人进入一个点后再也不能到达终点则输出 PRISON , 否则输出 PARDON 第二问: 若这个人可以在图里走无穷步则输出UNLIMITED, 否则输出LIMITED #include <iostream> #include <cstdio>