zoukankan      html  css  js  c++  java
  • Outing

    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+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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]]=1;
            }
        }
    }
    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=0;
        memset(sccno,0,sizeof(sccno));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
            if(!pre[i])dfs(i);
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)
        {
            int a;
            scanf("%d",&a);
            e[i].pb(a),to[a].pb(i);
        }
        find_scc(n);
        rep(i,1,scc_cnt){pk[i]=bel1[i];if(bel1[i]!=1)bfs(i);}
        dp[0]=1;
        rep(i,1,scc_cnt)
        {
            if(vis[i])continue;
            int x=bel1[i],y=pk[i];
            rep(j,1,m)sum[j]=sum[j-1]+dp[j];
            for(j=m;j>=1;j--)
            {
                if(j<x)break;
                else if(j>=x&&j<=y)dp[j]=1;
                else if(sum[j-x]-sum[j-y-1])dp[j]=1;
            }
        }
        for(i=m;dp[i]==0;i--);
        printf("%d
    ",i);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Oracle 在64位机器上使用plSQL连接Oracle的问题(SQL*Net not properly installed)
    Bytes to be written to the stream exceed the Content-Length bytes size specified 解决方法
    Eclipse下建立geoserver源码工程
    (转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
    为nginx创建windows服务自启动
    (转)Nginx反向代理设置 从80端口转向其他端口
    从SNE到t-SNE再到LargeVis
    K NEAREST NEIGHBOR 算法(knn)
    从0开始用python实现神经网络 IMPLEMENTING A NEURAL NETWORK FROM SCRATCH IN PYTHON – AN INTRODUCTION
    Python和数据科学的起步指南
  • 原文地址:https://www.cnblogs.com/dyzll/p/6031972.html
Copyright © 2011-2022 走看看