#include #include #include #include using namespace std; /* * Author * Robin Lee (hex539) * * Algorithm * Dynamic programming - dp[n][m] means we can make 'm' participants * using the first 'n' clusters. * * Complexity * O(N^2) */ int16_t dp[1024][2048]; struct participant {int16_t x,p,r;} v[1024]; int findroot(int a){ return v[a].p==a? a: v[a].p=findroot(v[a].p); } void join(int a,int b){ if ((a=findroot(a))==(b=findroot(b))) return; if (v[a].r>n>>m; for (int i=0; i>v[i].x, --v[i].x, v[i].p=i; for (int i=n; i--;) join(i,v[i].x); // count largest sizes for each cluster and smallest sizes via tortoise/hare algorithm vector> size(n); for (int i=n; i--;) ++size[findroot(i)].second; for (int i=n; i--;) if (i==findroot(i)) size[i].first=get_core_size(i); // dynamic programming to find legal subset sizes dp[0][0]=1; for (int i=0; i