zoukankan      html  css  js  c++  java
  • CodeForcesGym 100502G Outing

    Outing

    Time Limit: 1000ms
    Memory Limit: 524288KB
    This problem will be judged on CodeForcesGym. Original ID: 100502G
    64-bit integer IO format: %I64d      Java class name: (Any)

      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.
    Task

    Given the preferences of all participants, find the maximum number of participants that can join.
    Input

    The first line of input contains two integers n and k (1 ≤ k ≤ n ≤ 1000), where n denotes the total number of participants and k denotes the number of places on the bus. The second line contains n integers xi for i = 1,2,...,n, where 1 ≤ xi ≤ n. The meaningof xi is that the i-th participant will refuse to join the excursion unless the xi-th participant also joins.
    Output

    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.

    Sample Input 1

    4 4

    1 2 3 4

    Sample Output 1

    4
    Sample Input 2

    12 3

    2 3 4 5 6 7 4 7 8 8 12 12

    Sample Output 2

    2

    Sample Input 3 

    5 4

    2 3 1 5 4

    Sample Output 3

    3

    解题:强连通缩点+虚拟根节点+树形dp

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int maxn = 1010;
      4 struct arc {
      5     int to,next;
      6     arc(int x = 0,int y = -1) {
      7         to = x;
      8         next = y;
      9     }
     10 } e[maxn<<1];
     11 int head[maxn],low[maxn],dfn[maxn],belong[maxn],rs[maxn],tot,idx,scc;
     12 bool instack[maxn],connect[maxn][maxn],dp[maxn][maxn];
     13 int root,n,m,ind[maxn];
     14 vector<int>g[maxn];
     15 stack<int>stk;
     16 void tarjan(int u) {
     17     dfn[u] = low[u] = ++idx;
     18     instack[u] = true;
     19     stk.push(u);
     20     for(int i = head[u]; ~i; i = e[i].next) {
     21         if(!dfn[e[i].to]) {
     22             tarjan(e[i].to);
     23             low[u] = min(low[u],low[e[i].to]);
     24         } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
     25     }
     26     if(low[u] == dfn[u]) {
     27         int v;
     28         scc++;
     29         do {
     30             instack[v = stk.top()] = false;
     31             belong[v] = scc;
     32             rs[scc]++;
     33             stk.pop();
     34         } while(v != u);
     35     }
     36 }
     37 void add(int u,int v) {
     38     e[tot] = arc(v,head[u]);
     39     head[u] = tot++;
     40 }
     41 void init() {
     42     while(!stk.empty()) stk.pop();
     43     for(int i = 0; i < maxn; ++i) {
     44         dfn[i] = low[i] = 0;
     45         rs[i] = belong[i] = 0;
     46         instack[i] = false;
     47         head[i] = -1;
     48         memset(dp[i],false,sizeof dp[i]);
     49         memset(connect[i],false,sizeof connect[i]);
     50         g[i].clear();
     51         ind[i] = 0;
     52     }
     53     tot = idx = scc = 0;
     54     rs[root = 0] = 0;
     55 }
     56 void dfs(int u) {
     57     dp[u][0] = dp[u][rs[u]] = true;
     58     for(int i = g[u].size()-1; i >= 0; --i) {
     59         int v = g[u][i];
     60         dfs(v);
     61         for(int j = m; j >= rs[u]; --j) {
     62             if(dp[u][j]) continue;
     63             for(int k = 0; k <= j - rs[u]; ++k)
     64             if(dp[v][k] && dp[u][j - k]) {
     65                 dp[u][j] = true;
     66                 break;
     67             }
     68         }
     69     }
     70 }
     71 int main() {
     72     int u;
     73     while(~scanf("%d %d",&n,&m)) {
     74         init();
     75         for(int i = 1; i <= n; ++i) {
     76             scanf("%d",&u);
     77             add(u,i);
     78         }
     79         for(int i = 1; i <= n; ++i)
     80             if(!dfn[i]) tarjan(i);
     81         for(int i = 1; i <= n; ++i) {
     82             for(int j = head[i]; ~j; j = e[j].next) {
     83                 if(belong[i] == belong[e[j].to]) continue;
     84                 if(connect[belong[i]][belong[e[j].to]]) continue;
     85                 g[belong[i]].push_back(belong[e[j].to]);
     86                 ind[belong[e[j].to]]++;
     87                 connect[belong[i]][belong[e[j].to]] = true;
     88             }
     89         }
     90         for(int i = 1; i <= scc; ++i)
     91             if(!ind[i]) g[root].push_back(i);
     92         dfs(root);
     93         int ret = 0;
     94         for(int i = m; i >= 0; --i)
     95             if(dp[root][i]) {
     96                 ret = i;
     97                 break;
     98             }
     99         printf("%d
    ",ret);
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    python学习-装饰器
    python-内置函数
    HA高可用解决方案-RHCS部署
    mac安装nose,command not found:nosetests
    iPhone的home键进果汁了,按起来粘粘的感觉
    Reportng 的测试报告在 Jenkins 中显示不全
    atom markdown报错:AssertionError: html-pdf: Failed to load PhantomJS module.
    markdown的图片外链
    reportng定制修改
    运行maven build报错No goals have been specified for this build.
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4461729.html
Copyright © 2011-2022 走看看