BNUOJ 48270 Outing(强联通+缩点+01背包)
/***
题目:G. Outing
题意:给出n个人坐公交,这些人有一定的依赖关系,依赖关系由数组a确定,
假如a[2]=3的话,代表只有第三个人上车了第二个人才有上车的可能,求出最大的可能上车的人数
思路:先强连通缩点,最后一个一些强连通分量,然后对这些联通分量做背包,注意物体的重量是可变的
***/
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<iostream>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1010;//点数
const int MAXM = 100100;//边数
int a[MAXN],f[MAXN],zhi[MAXN],num_max[MAXN],dp[MAXN];
struct Edge
{
int to,next;
} edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强连通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
//num数组不一定需要,结合实际情况
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if( !DFN[v] )
{
Tarjan(v);
if( Low[u] > Low[v] )Low[u] = Low[v];
}
else if(Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}
while( v != u);
}
}
void solve(int N)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
memset(num,0,sizeof(num));
Index = scc = top = 0;
for(int i = 1; i <= N; i++)
if(!DFN[i])
Tarjan(i);
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int get(int x)
{
if(x==f[x])return x;
return get(f[x]);
}
int vis[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
int n,k,t;
while(~scanf("%d%d",&n,&k))
{
init();
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
addedge(i,a[i]);
}
solve(n);
for(int i=1; i<=scc; i++)
f[i] = i;
for(int i=1; i<=n; i++)
{
if(Belong[i] == Belong[a[i]])continue;
f[Belong[i]] = Belong[a[i]];
}
memset(vis,0,sizeof(vis));
memset(num_max,0,sizeof(num_max));
for(int i=1; i<=n; i++)
{
int t = get(f[Belong[i]]);
num_max[t]++;
// vis[t]=1;///判断这个强连通分量是不是依赖其他强连通分量
}
memset(dp,0,sizeof(dp));
for(int i=1; i<=scc; i++)
{
//printf("num[%d]=%d , num_max[%d]=%d\n",i,num[i],i,num_max[i]);
// if(!vis[i])continue;
for(int j=k; j>=0; j--)
for(int s=num[i]; s<=num_max[i]; s++)
if(j >= s)
dp[j]=max(dp[j],dp[j-s]+s);
}
printf("%d\n",dp[k]);
}
return 0;
}
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<iostream>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1010;//点数
const int MAXM = 100100;//边数
int a[MAXN],f[MAXN],zhi[MAXN],num_max[MAXN],dp[MAXN];
struct Edge
{
int to,next;
} edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强连通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
//num数组不一定需要,结合实际情况
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if( !DFN[v] )
{
Tarjan(v);
if( Low[u] > Low[v] )Low[u] = Low[v];
}
else if(Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}
while( v != u);
}
}
void solve(int N)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
memset(num,0,sizeof(num));
Index = scc = top = 0;
for(int i = 1; i <= N; i++)
if(!DFN[i])
Tarjan(i);
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int get(int x)
{
if(x==f[x])return x;
return get(f[x]);
}
int vis[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
int n,k,t;
while(~scanf("%d%d",&n,&k))
{
init();
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
addedge(i,a[i]);
}
solve(n);
for(int i=1; i<=scc; i++)
f[i] = i;
for(int i=1; i<=n; i++)
{
if(Belong[i] == Belong[a[i]])continue;
f[Belong[i]] = Belong[a[i]];
}
memset(vis,0,sizeof(vis));
memset(num_max,0,sizeof(num_max));
for(int i=1; i<=n; i++)
{
int t = get(f[Belong[i]]);
num_max[t]++;
// vis[t]=1;///判断这个强连通分量是不是依赖其他强连通分量
}
memset(dp,0,sizeof(dp));
for(int i=1; i<=scc; i++)
{
//printf("num[%d]=%d , num_max[%d]=%d\n",i,num[i],i,num_max[i]);
// if(!vis[i])continue;
for(int j=k; j>=0; j--)
for(int s=num[i]; s<=num_max[i]; s++)
if(j >= s)
dp[j]=max(dp[j],dp[j-s]+s);
}
printf("%d\n",dp[k]);
}
return 0;
}