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 ?xed number of places.
Task

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

The ?rst 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\n",ret);
100     }
101     return 0;
102 }

时间: 2024-10-20 03:58:21

CodeForcesGym 100502G Outing的相关文章

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 fro

CodeForcesGym 100212E Long Dominoes

Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. Original ID: 100212E64-bit integer IO format: %I64d      Java class name: (Any) Find the number of ways to tile an m*n rectangle with long dominoes -

CodeForcesGym 100753E Change of Scenery

Change of Scenery Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753E64-bit integer IO format: %I64d      Java class name: (Any) 解题:最短路 1 #include <bits/stdc++.h> 2 using namespace std; 3 typ

CodeForcesGym 100753F Divisions

Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753F64-bit integer IO format: %I64d      Java class name: (Any) 解题:大数质因子分解 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef

CodeForcesGym 100753B Bounty Hunter II

Bounty Hunter II Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753B64-bit integer IO format: %I64d      Java class name: (Any) 解题:最小路径覆盖 1 #include <bits/stdc++.h> 2 using namespace std; 3 co

hihocoder 1154 Spring Outing

传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring outing. N people are voting for a destination out of K candidate places. The voting progress is below: First the class vote for the first candidate p

CodeForcesGym 100502E Opening Ceremony

Opening Ceremony Time Limit: 5000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502E64-bit integer IO format: %I64d      Java class name: (Any) For the grand opening of the algorithmic games in NlogNsglow, a r

CodeForcesGym 100517B Bubble Sort

Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100517B64-bit integer IO format: %I64d      Java class name: (Any) 解题:我们发现假设当前位选择1,那么发现比1大的并且没有使用的有b个,那么当前为选1,后面就还有$2^{b-1}$种方式,所以贪心的选,只要

CodeForcesGym 100753A A Journey to Greece

A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100753A64-bit integer IO format: %I64d      Java class name: (Any) 解题:状压dp 1 #include <bits/stdc++.h> 2 using namespace std; 3 t