string moreClasses(string a, vector <string> b) { int i, j, k, x, y, z, n; string ret = ""; VVI m; for( i = 0; i < b.size(); i++ ) { n = atoi(b[i].c_str()); for( j = 0; j < n; j++ ) { m.push_back(VI()); for( k = 0; k < b[i].size(); k++ ) if( !isdigit(b[i][k]) ) m.back().push_back(b[i][k]); } } if( m.size() > 128 ) return "0"; for( i = 0; i < a.size(); i++ ) taken[a[i]] = 1; n = m.size()-bipartitematch(m); x = 33; while( n ) { if( x >= 128 ) return "0"; if( taken[x] ) {x++; continue;} taken[x] = 1; if (x >= 67) cout<<x; z = m.size()-bipartitematch(m); if( z == n ) taken[x] = 0; else ret += x; n = z; x++; } return ret; }
void initDfs(int u,int level = 0) { LVL[u] = level; SZ[u] = 1; for(int i = 0; i < adj[u].size(); ++i) { int v = adj[u][i]; if(SZ[v])continue; P[v] = u; initDfs(v,level + 1); SZ[u] += SZ[v]; } for(int i = 0; i < adj[u].size(); ++i) { int v = adj[u][i]; if(v == P[u])continue; if(2*SZ[v] > SZ[u]-1){ PC[u] = v; } else { groups.push_back(VI()); for(int j = v; j != -1; j = PC[j]) groups.back().push_back(j) , groupId[j] = groups.size() - 1 , groupPos[j] = groups.back().size()-1; } } }
void DFS (int v) { idx[v] = index; low[v] = index; index += 1; st.push_back(v); inStack[v] = true; for (auto w : adj[v]) { if (idx[w] == -1) { DFS(w); low[v] = min(low[v], low[w]); } else if (inStack[w]) { low[v] = min(low[v], low[w]); } } if (low[v] == idx[v]) { int w; components.push_back(VI()); do { w = st.back(); st.pop_back(); inStack[w] = false; componentOf[w] = totalComponents; components[totalComponents].push_back(w); } while (w != v); totalComponents++; } }
bool multi(VVI & x, VVI & y, VVI & z) { int mx = x.size(); int my = y.size(); if (mx == 0 || my == 0) return false; int nx = x[0].size(); int ny = y[0].size(); if (nx == 0 || ny == 0) return false; //cout << "mx: " << mx << endl; //cout << "my: " << my << endl; //cout << "nx: " << nx << endl; //cout << "ny: " << ny << endl; if (nx != my) return false; z.clear(); for (int i = 0; i < mx; i++) { z.push_back(VI(ny, 0)); for (int j = 0; j < ny; j++) { double sum = 0; for (int k = 0; k < my; k++) { sum += (double)x[i][k] * (double)y[k][j]; } if (sum > INT_MAX || sum < INT_MIN) return false; z[i][j] = (int)sum; } } return true; }
VVI parse(VS M) { VVI m; for (int i = 0; i < (int)M.size(); i++) { m.push_back(VI()); istringstream is(M[i]); int n; while (is >> n) { m[i].push_back(n); } } return m; }
VVI exactify(const VVI& S, const int N) { VVI vec; for(int r = 0; r < N; ++r) { for(int c = 0; c < N; ++c) { for(int cand = 0; cand < N; ++cand) { if(S[r][c] != 0 && S[r][c] != cand + 1) { continue; } row_cand[vec.size()] = r*N*N + c*N + cand; vector<int> L(4*N*N); vec.push_back(L); int reg = region(r,c,N); vec.back()[r*N + c] = 1; vec.back()[N*N + r*N + cand] = 1; vec.back()[2*N*N + N*c + cand] = 1; vec.back()[3*N*N + N*reg + cand] = 1; } } } if(!given_solution.empty()) { map<int,int> rev; map<int,int>::iterator it; for(it = row_cand.begin(); it != row_cand.end(); ++it) { rev[it->second] = it->first; } vector<int> check(vec[0].size()); for(int x = 0; x < given_solution.size(); ++x) { for(int y = 0; y < given_solution[x].size(); ++y) { int val = x * N * N + y * N + given_solution[x][y] - 1; for(int i = 0; i < vec[rev[val]].size(); ++i) { check[i] += vec[rev[val]][i]; } } } for(int i = 0; i < check.size(); ++i) { assert(check[i] == 1); } } return vec; }
bool can(int prefLength,VI& result,bool return_result = false){ result.clear(); vector<Hash> hashes; int n = 1; for(int sn = 0; sn<n;++sn){ HashComputer<mod1> curHash1; HashComputer<mod2> curHash2; for(int i = 0; i < prefLength; ++i){ curHash1.add(a[sn][i]); curHash2.add(a[sn][i]); } hashes.push_back(Hash(make_pair(curHash1.value,curHash2.value),sn,0)); for(int i = prefLength; i < a[sn].size(); ++i){ curHash1.shift(a[sn][i-prefLength],a[sn][i],prefLength); curHash2.shift(a[sn][i-prefLength],a[sn][i],prefLength); hashes.push_back(Hash(make_pair(curHash1.value,curHash2.value),sn,i-prefLength+1)); } } sort(hashes.begin(),hashes.end()); //hashes.resize(unique(hashes.begin(),hashes.end(),EqualString())-hashes.begin()); VVI results; for(int i = k-1; i < hashes.size();++i) if(hashes[i].equals(hashes[i-k+1])){ if(return_result == true){ results.push_back(VI()); int k = results.size()-1; for(int j = i-n+1; j <=i;++j) results[k].push_back(hashes[j].pos); } else return true; } if(return_result == true){ result = *min_element(results.begin(),results.end()); return true; } return false; }
void dfs(int depth, VI& nums, VI& cur, USET_VI& uset, VVI& result) { if (depth == nums.size()) { if (check(cur)) { string s = toString(cur); if (uset.find(s) == uset.end()) { uset.insert(s); result.push_back(cur); } } } else { dfs(depth + 1, nums, cur, uset, result); cur.push_back(nums[depth]); dfs(depth + 1, nums, cur, uset, result); cur.pop_back(); } }
HLD(VVI adj) { N = adj.size(); P = PC = VI(N , -1); LVL = SZ = groupId = groupPos = VI(N , 0); this->adj = adj; for(int i = 0; i < N; ++i) { if(SZ[i] == 0) { initDfs(i); groups.push_back(VI()); for(int j = i; j != -1; j = PC[j]) groups.back().push_back(j) , groupId[j] = groups.size() - 1 , groupPos[j] = groups.back().size()-1; } } }
int DFS (PI v, int index) { idx[v.x] = index; low[v.x] = index; index += 1; int children = 0; bool ap = false; for (auto w : adj[v.x]) if (w.y != v.y) { if (idx[w.x] == -1) { st.push_back(w.y); index = DFS(w, index); low[v.x] = min(low[v.x], low[w.x]); if (low[w.x] > idx[v.x]) { bridges.push_back(w.y); } children++; if (low[w.x] >= idx[v.x]) { if (v.y != -1 || children >= 2) { ap = true; } components.push_back(VI()); totalComponents++; int u; do { u = st.back(); st.pop_back(); components.back().push_back(u); } while (u != w.y); } } else if (idx[w.x] < idx[v.x]) { st.push_back(w.y); low[v.x] = min(low[v.x], idx[w.x]); } } if (ap) { cutVertices.push_back(v.x); } return index; }
bool add(VVI & x, VVI & y, VVI & z) { int mx = x.size(); int my = y.size(); if (mx == 0 || my == 0) return false; int nx = x[0].size(); int ny = y[0].size(); if (nx == 0 || ny == 0) return false; //cout << "mx: " << mx << endl; //cout << "my: " << my << endl; //cout << "nx: " << nx << endl; //cout << "ny: " << ny << endl; if (mx != my || nx != ny) return false; z.clear(); for (int i = 0; i < mx; i++) { z.push_back(VI(nx, 0)); for (int j = 0; j < nx; j++) { double tmp = (double)x[i][j] + (double)y[i][j]; if (tmp > INT_MAX || tmp < INT_MIN) return false; z[i][j] = (int)tmp; } } return true; }