template<class V> void print_container(V& container, bitset<MAX_BITS_SIZE>& flag, bool reverse, bool is_servercmp) { if (reverse) { flag.flip(); } typename V::iterator it = container.begin(); if (!is_servercmp) // print different block { int32_t count = static_cast<int32_t> (flag.count()); for (int32_t i = 0, c=0; (it != container.end()) && (count > 0); i++, it++) { if (flag.test(i)) { cout << *it ; if((++c) % 20 == 0) cout<<"\n"; else cout<<" "; count--; } } printf(" [%d]\n", static_cast<int32_t>((flag.count() - count))); } else { for (int32_t i = 0; it != container.end(); i++, it++) { printf("%23s%c", tbsys::CNetUtil::addrToString(*it).c_str(), suffix(flag[i])); } printf(" [%zd]\n", container.size()); } }
int main(){ CPPinput; int n,k; cin>>n>>k; for(int i=1;i<=4;++i)for(int j=1;j<=n;++j)cin>>mp[i][j]; vector<tuple<int,int,int>> ans; while(bk.count()<k){ // for(int i=1;i<=4;++i){ for(int j=1;j<=n;++j)cout<<mp[i][j]<<" "; cout<<endl; } cout<<endl; { for(int i=1;i<=n;++i){ if(mp[2][i]==mp[1][i] && mp[1][i]){ ans.eb(mp[2][i],1,i); bk[mp[2][i]]=1; mp[2][i]=0; } if(mp[3][i]==mp[4][i] && mp[4][i]){ ans.eb(mp[3][i],4,i); bk[mp[3][i]]=1; mp[3][i]=0; } } } { int ex=-1,ey=-1; for(int i=2;i<=3;++i)for(int j=1;j<=n;++j)if(!mp[i][j])ex=i,ey=j; if(ex==-1)J**Z("-1\n"); int nx=ex,ny=ey; vector<pii> cc; cc.eb(nx,ny); if(nx==2){ while(ny<n)cc.eb(nx,++ny); } else{ while(ny>1)cc.eb(nx,--ny); } if(nx==2)cc.eb(++nx,ny); else cc.eb(--nx,ny); if(nx==2){ while(ny<n)cc.eb(nx,++ny); } else{ while(ny>1)cc.eb(nx,--ny); } if(nx==2)cc.eb(++nx,ny); else cc.eb(--nx,ny); if(nx==2){ while(ny!=ey)cc.eb(nx,++ny); } else{ while(ny!=ey)cc.eb(nx,--ny); } cc.pop_back(); for(int i=0;i<cc.size()-1;++i){ if(mp[cc[i+1].first][cc[i+1].second]){ // cout<<"move from "<<cc[i+1]<<" to "<<cc[i]<<endl; ans.eb(mp[cc[i+1].first][cc[i+1].second],cc[i].first,cc[i].second); mp[cc[i].first][cc[i].second]=mp[cc[i+1].first][cc[i+1].second]; mp[cc[i+1].first][cc[i+1].second]=0; } } } } if(bk.count()!=k)J**Z("-1\n"); cout<<ans.size()<<endl; for(auto &t:ans){ int a,b,c; tie(a,b,c)=t; cout<<a<<" "<<b<<" "<<c<<'\n'; } }
void dfs(int nowlen,int laststr){ int rest=f.count(); if(nowlen+rest>=anslen) return; if(!rest){ memcpy(ans,now,sizeof(now)); anslen=nowlen; return; } static const int z=pow(c,l-1); int newstr=(laststr%z)*c; if(nowlen<l-1){ for(int k=0;k<c;++k,++newstr) now[nowlen]=k,dfs(nowlen+1,newstr); return; } for(int k=0;k<c;++k,++newstr) if(f.test(newstr)){ now[nowlen]=k; f.reset(newstr); dfs(nowlen+1,newstr); f.set(newstr); } newstr=(laststr%z)*c; for(int k=0;k<c;++k,++newstr) if(!f.test(newstr)) now[nowlen]=k,dfs(nowlen+1,newstr); }
int main() { end: while (cin >> n >> m) { v.clear(); getline(cin, s); fill(v.begin(), v.end(), vector<int>()); awake.reset(); getline(cin, s); for (char c : s) { awake.set(c - 'A'); } for (int i = 0; i < m; i++) { getline(cin, s); int a = s[0] - 'A', b = s[1] - 'A'; v[a].push_back(b); v[b].push_back(a); } int years = 0; while (awake.count() < n) { vector<int> wokeup; for (int cur = 0; cur < 26; cur++) { if (awake[cur]) { continue; } int awake_neighbors = 0; for (int neighbor : v[cur]) if (awake[neighbor]) { awake_neighbors++; } if (awake_neighbors >= 3) { wokeup.push_back(cur); } } if (wokeup.empty()) { cout << "THIS BRAIN NEVER WAKES UP\n"; goto end; } for (int i : wokeup) { awake.set(i); } years++; } cout << "WAKE UP IN, " << years << ", YEARS\n"; } return 0; }
int main() { ifstream f("plimbare.in"); ofstream g("plimbare.out"); f>>n; m=(n*(n-1))/2; for(int i=0; i<m; ++i) { int a,b; f>>a>>b; ma[a][b]=it[a][b]=1; } for(int k=1; k<=n; ++k) for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) it[i][j]|=(it[i][k]&it[k][j]); for(int i=1; i<=n; ++i) if(!fol[i]) { int dc=1; fol[i]=1; for(int j=i+1; j<=n; ++j) if(it[i][j] && it[j][i]) { ++dc; fol[j]=1; } if(dc>dmax) dmax=dc,fr=i; } fol&=0; dfs(fr,fr); fol&=0; fol[fr]=1; for(int i=nxt[fr];i!=fr; i=nxt[i]) fol[i]=1; for(;fol.count()<dmax;) { for(int i=nxt[fr];i!=fr; i=nxt[i]) s(i); s(fr); } g<<dmax<<'\n'<<fr<<' '; for(int i=nxt[fr]; i!=fr; i=nxt[i]) g<<i<<' '; return 0; }
int main() { freopen("milk3.in","r",stdin); freopen("milk3.out","w",stdout); for (int i = 0; i < 3; ++i) cin>>cap[i]; int init[3]; init[0] = init[1] = 0; init[2] = cap[2]; dfs(init); int space_num = ans.count() - 1; for (int i = 0; i < 21; ++i) { if( ans.test(i) && space_num ) { cout<<i<<" "; space_num--; } else if(ans.test(i)) cout<<i<<endl; } return 0; }
int main() { freopen("holstein.in","r",stdin); freopen("holstein.out","w",stdout); scanf("%d",&v); for (int i=0;i<v;i++){ scanf("%d",&cow[i]); } scanf("%d",&g); for (int i=0;i<g;i++){ for (int j=0;j<v;j++){ scanf("%d",&vi[i][j]); } } //枚举每一种饲料喂养 bool f; for (int i=0;i<(1<<(g));i++){ bitset<16> tmp(i); for (int j=0;j<g;j++){ if (tmp.test(j)==true){ for (int z=0;z<v;z++){ feed[z] += vi[j][z]; } } } f = true; for (int z=0;z<v;z++){ if (feed[z]<cow[z]){ f = false; break; } } memset(feed,0,sizeof(feed)); //在个数相同的情况下,字典序小的优先,所以把bitset还原回数字之后小的优先 if (f&&((miniset.count()==0||tmp.count()<miniset.count())||(tmp.count()==miniset.count()&&tmp.to_ulong()<miniset.to_ulong()))){ miniset = tmp; } } printf("%d",miniset.count()); for (int i=0;i<g;i++){ if (miniset.test(i)) printf(" %d",i+1); } printf("\n"); return 0; }
void dfs(vector<vector<int>>& vv, bitset<maxNUM>& bt, int pos, int n, int k) { if(pos > n || bt.count() > k) return ; if(bt.count() == k && pos == n){ string ans = bt.to_string(); vector<int> vt; vt.clear(); for(int i = 0; i < n; i++) { if(ans[i] == '1') vt.push_back(i + 1); } vv.push_back(vt); return ; } bt.flip(maxNUM - pos - 1); dfs(vv, bt, pos + 1, n, k); bt.flip(maxNUM - pos - 1); dfs(vv, bt, pos + 1, n, k); }
int main() { int T; scanf("%d", &T); for (int i = 0; i < N; ++ i) { blk[i] = i / BLKSZ; } while (T --) { scanf("%d%d", &n, &m); lastans = 0; for (int j = 0; j < 5; ++ j) { for (int i = 0; i <= blk[n]; ++ i) { bt[j][i].reset(); } } for (int i = 0; i < n; ++ i) { for (int j = 0; j < 5; ++ j) { scanf("%d", &p[j][i].d); p[j][i].id = i; } } for (int j = 0; j < 5; ++ j) { sort(p[j], p[j] + n); } for (int j = 0; j < 5; ++ j) { for (int i = 0; i < n; ++ i) { bt[j][blk[i]].set(p[j][i].id); mx[j][blk[i]] = p[j][i].d; } for (int i = 1; i <= blk[n - 1]; ++ i) { bt[j][i] |= bt[j][i - 1]; } } scanf("%d", &m); while (m --) { for (int j = 0; j < 5; ++ j) { scanf("%d", &lim); ans[j].reset(); lim ^= lastans; int l = 0; while (l <= blk[n - 1] && mx[j][l] <= lim) { l ++; } ans[j] = l > 0 ? bt[j][l - 1] : 0; for (int i = l * BLKSZ; i < n; ++ i) { if (p[j][i].d > lim) break; ans[j].set(p[j][i].id); } } ans1.set(); for (int j = 0; j < 5; ++ j) { ans1 &= ans[j]; } lastans = ans1.count(); printf("%d\n", lastans); } } return 0; }
void dfs(int dep, int& ans, bitset<2000> b) { if(dep >= ans || dep > 10) return; if(b.count() == m) {ans = dep; return;} for(int i = 0; i < m; ++i) { if(b[i] == 1) continue; int u = edge[i].u, v = edge[i].v; dfs(dep + 1, ans, b | sta[u]); dfs(dep + 1, ans, b | sta[v]); break; } }
int verif(int k,int d){ bs&=0; for(int i=1;i<=k;++i){ int cc=a[i]; for(int j=1;j<=m[cc][0];++j){ bs[m[cc][j]]=1; } } if(bs.count()==d)return 1; return 0; }
int search(bitset<10> &digits, int n, int depth) { int count = 0; if (digits.count() > n) return 0; count++; for (int d = 0; d <= 9; d++) { // number cannot start with 0 if (depth == 0 && d == 0) continue; if (digits[d] == false) { digits[d] = true; count += search(digits, n, depth+1); digits[d] = false; } } return count; }
i64 ulam(const int v, const i64 target) { //the first fact is that the second even number is 2v+2 ulambit.reset(); //create v+1 bit for 2v+3,..., 4v+3; int n1 = v/2; for(unsigned int i = 0; i<= v;++i){ ulambit.set(i); } i64 period = 0; i64 span = 0; do{ int lastbit = ulambit[0]; int firstbit = ulambit[v]; int result = lastbit^firstbit; ulambit <<= 1; ulambit.set(0, result); ulambit[v+1] = 0; if(result) ++period; span += 2; }while(ulambit.count()<v+1); //printf("%d %lld %lld\n", v, period, span); i64 nres = (target-3)%period; i64 mult = (target-3)/period; ulambit.reset(); ulambit.set(0); i64 span0 = span; span = v; period = 0; do{ if(period == nres) break; int lastbit = ulambit[0]; int firstbit = ulambit[v]; int result = lastbit^firstbit; ulambit <<= 1; ulambit.set(0, result); ulambit[v+1] = 0; if(result) ++period; span += 2; }while(true); return mult*span0+span; }
int solve() { int ans = inf; for (int i = 0; i < (1<<K); i++) { int cnt = 0; s.reset(); for (int j = 0; j < K; j++) { if (i&(1<<j)) { cnt++; s |= p[j]; } } if (s.count() == N * N - K) ans = min(ans, cnt); } return ans == inf ? -1 : ans; }
/* * Backtack through all unused nodes, adding their * neighbors to the used set each call such that * we find the maximal independent set */ void backtrack(int i, bitset<100> used) { // Base Case, we have explored all nodes if(used.count() == n) { if(current.count() >= solution.count()) { // We found a bigger Independent Set. solution = current; } } // Recursive Case, keep exploring for (int j = i; j < n; ++j) { if(!used[j]) { current[j] = 1; backtrack(j+1, used | adjMatrix[j]); current[j] = 0; } } }
int main() { freopen("berarii2.in", "r", stdin); freopen("berarii2.out", "w", stdout); scanf("%d %d %d", &N, &M, &P); for (int m=0; m<M; ++m){ int X, Y; scanf("%d %d", &X, &Y); graph[Y].push_back(X); } int src; vector<int> que; for (int p=0; p<P; ++p){ int src; scanf("%d", &src); vis[src] = 1; que.push_back(src); } for (unsigned i=0; i<que.size(); ++i){ src = que[i]; for (unsigned j=0; j<graph[src].size(); ++j){ if (!vis[graph[src][j]]){ vis[graph[src][j]] = 1; que.push_back(graph[src][j]); } } } printf("%d\n", N-vis.count()); for (int n=1; n<=N; ++n){ if (!vis[n]) printf("%d\n", n); } return 0; }
int main() { ifstream f("berarii2.in"); ofstream g("berarii2.out"); f>>n>>m>>p; for(int i=0; i<m; ++i) { int a,b; f>>a>>b; gt[b].push_back(a); } for(int i=1; i<=n; ++i) viz[i]=1; for(int i=0; i<p; ++i) { int a; f>>a; dfs(a); } g<<viz.count()<<'\n'; for(int i=1; i<=n; ++i) if(viz[i]) g<<i<<'\n'; return 0; }
int meow(vector<int> &ct){ for(int i:ct)nowdeg[i]=G[i].size(),sele[i]=1; int ans=0; while("meow"){ int mndeg=9999,num; for(int i=1;i<=n;++i){ if(sele[i] && nowdeg[i]<mndeg){ mndeg=nowdeg[i]; num=i; } } if(mndeg==9999)break; ans=max(ans,int(mndeg*sele.count())); sele[num]=0; for(int i:G[num]){ if(sele[i])--nowdeg[i]; } } return ans; }
bool majcheck(bitset<N> a){ int count = a.count(); if(2*count >= N) return true; else return false; }
size_t countMarked() { return marked.count(); }