// calculate the factorial numbers // general idea: // https://blog.codechef.com/2009/07/02/tutorial-for-small-factorials/ // optimization: store all calculated numbers, calculate only when needed void fac(vvi &f, size_t n) { for (size_t highest = f.size(); highest < n; ++highest) { vi current; const int prev = highest-1; int carry = 0; for (vi::iterator it = f[prev].begin(); it != f[prev].end(); ++it) { int next = *it * (highest+1) + carry; carry = next / RANGE; current.push_back(next % RANGE); } while (carry) { current.push_back(carry % RANGE); carry = carry / RANGE; } f.push_back(current); } }
int main() { ifstream fin ("graph2.txt"); //graph1.txt for(int i=0;i<MAX;i++){ vi newVecG,newVecR; graphG.push_back(newVecG); graphR.push_back(newVecR); } int node1,node2; while(fin>>node1>>node2){ //cout<<node1<<" "<<node2<<endl; graphG[node1].push_back(node2); graphR[node2].push_back(node1); } for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs(i); } } cout<<"Reversed PostOrder of R"<<endl; int SCCid=0; while(!postOrder.empty()){ int node=postOrder.top(); postOrder.pop(); //cout<<node<<endl; if(visited2[node]==false){ dfs_mark(node,SCCid); SCCid++; } } cout<<"Node\tSCCid"<<endl; for(int i=0;i<MAX;i++){ cout<<i<<"\t"<<SCCids[i]<<endl; } return 0; }
int main() { int t; scanf("%d",&t); for(int i=0;i<t;++i) { scanf("%d %d",&h,&w); stones.clear(); stones.insert(stones.begin(),h,vi(w,0)); for(int i=0;i<h;++i) for(int j=0;j<w;++j) scanf("%d",&stones[i][j]); cout<< optimal() <<endl; } return 0; }
vvi mat_pow(vvi a,int n) { vvi ret; for(int i=0;i<a.size();i++) { vector<int> v(a.size(),0); v[i]=1; ret.push_back(v); } while(n>0) { if(n%2==1) ret=mul(ret,a); a=mul(a,a); n/=2; } return ret; }
void addTree(int x, int y, int z) { vi tree; tree.push_back(x); tree.push_back(y); tree.push_back(z); trees.push_back(tree); }
int main() { // freopen("in.txt", "r", stdin); scanf("%d", &n); adj.resize(n + 5); g.resize(n + 5); for (int i = 1; i <= n; i++) { string l; cin >> l; if (loc.find(l) == loc.end()) loc[l] = top++; location[i] = loc[l]; int m; scanf("%d", &m); for (int j = 0; j < m; j++) { int v; scanf("%d", &v); // adj[i].push_back(v); adj[v].push_back(i); } adj[i].push_back(i); } for (int i = 1; i <= n; i++) { for (int j = 0; j < sz(adj[i]); j++) { for (int k = j + 1; k < sz(adj[i]); k++) { int u = adj[i][j]; int v = adj[i][k]; if (location[u] != location[v]) { g[u].push_back(v); g[v].push_back(u); } } } } clr(vis, -1); for (int i = 1; i <= n; i++) if (vis[i] == -1) bfs(i); if (sz(ans) == 0) ans.push_back(1); printf("%d\n", sz(ans)); for (int i = 0; i < sz(ans); i++) printf("%d ", ans[i]); return 0; }
void dfs(int i, vi path, int left) { if (left == 0) ans.push_back(path); if (i == n) return ; if (left >= candidate[i]) { dfs (i + 1, path, left); path.push_back(candidate[i]); dfs(i, path, left - candidate[i]); } }
vvi mul(vvi a, vvi b) { vvi ret; for(int i=0;i<a.size();i++) { vector<int> v; for(int j=0;j<a[i].size();j++) { lli val=0; for(int k=0;k<b.size();k++) { val+=(lli)a[i][k]*(lli)b[k][j]; val%=mod; } v.push_back((int)val); } ret.push_back(v); } return ret; }
int solve(){ int maxLen = 0; for( int i = 0; i < map.size(); i++ ){ for( int j = 0; j < map[i].size(); j++ ){ //printf("%3d", dp(i, j)); if( maxLen < dp(i, j) ) maxLen = dp(i, j); } } return maxLen + 1; }
centroid_decomposition( const vvi& graph ) { N = sz(graph); centroid_graph.resize(N); parent.assign(N, -1); node_mapping.assign(N, -1); subtree_size.assign(N, 0); decompose(0, -1, graph); reverse_mapping.assign(N, -1); for(int i = 0; i < N; ++i) reverse_mapping[ node_mapping[i] ] = i; }
void initRules() { rulesDirection.resize(4); for(int i=0; i<4; ++i) rulesDirection[i].resize(4); //First index : ball, second : goal rulesDirection[LEFT][LEFT] = LEFT; rulesDirection[LEFT][FRONT] = LEFT; rulesDirection[LEFT][RIGHT] = LEFT; rulesDirection[LEFT][BACK] = LEFT; rulesDirection[FRONT][LEFT] = RIGHT; rulesDirection[FRONT][FRONT] = FRONT; rulesDirection[FRONT][RIGHT] = LEFT; rulesDirection[FRONT][BACK] = LEFT; rulesDirection[RIGHT][LEFT] = RIGHT; rulesDirection[RIGHT][FRONT] = RIGHT; rulesDirection[RIGHT][RIGHT] = RIGHT; rulesDirection[RIGHT][BACK] = RIGHT; rulesDirection[BACK][LEFT] = RIGHT; rulesDirection[BACK][FRONT] = RIGHT; rulesDirection[BACK][RIGHT] = LEFT; rulesDirection[BACK][BACK] = RIGHT; rulesSpeed.resize(3); for(int i=0; i<3; ++i) rulesSpeed[i].resize(3); rulesSpeed[CLOSE][CLOSE] = SLOW; rulesSpeed[CLOSE][AVG_DIST] = SLOW; rulesSpeed[CLOSE][FAR] = SLOW; rulesSpeed[AVG_DIST][CLOSE] = SLOW; rulesSpeed[AVG_DIST][AVG_DIST] = AVG_SPEED; rulesSpeed[AVG_DIST][FAR] = AVG_SPEED; rulesSpeed[FAR][CLOSE] = FAST; rulesSpeed[FAR][AVG_DIST] = AVG_SPEED; rulesSpeed[FAR][FAR] = FAST; }
int main(){ int n; cin>>n; vi t; t.resize(n,-1); dp.resize(n,t); // dp is n X n for(int i=0;i<n;i++){ int r,c; cin>>r>>c; vi temp; temp.pb(r); temp.pb(c); matrices.pb(temp); } cout<<solve(0,n-1)<<endl; int A; cin>>A; return 0; }
void worker(vvi & ret, vi & temp, int level, vi & S) { if(level == S.size()) { ret.push_back(temp); return; } worker(ret, temp, level + 1, S); temp.push_back(S[level]); worker(ret, temp, level + 1, S); temp.pop_back(); }
void showgraph(vvi& g) { for(int i=0;i<g.size();i++) { cout<<(i+1)<<" : "; for(int j=0;j<g[i].size();j++) cout<<g[i][j]+1<<" "; cout<<endl; } }
void solve() { for (ui i = 0; i < trees.size(); i++) { float min = -1; for (ui j = 0; j < trees.size(); j++) { if (i == j) continue; float dist = calcDist(trees[i][0], trees[i][1], trees[i][2], trees[j][0], trees[j][1], trees[j][2]); if (dist < min || min == -1) min = dist; } if (min < 10) answer[(int) min]++; } }
void flip(vvi& g, vvi& revg) { for(int i=0;i<g.size();i++) { for(int j=0;j<g[i].size();j++) { revg[g[i][j]].push_back(i); } } }
int main() { int q, c; s = 0, t = 1; // 2 to c is Cat nodes, c + 1 to q ins question nodes. while(cin >> c >> q && (q || c)) { int tot = 0; n = q + c + 2; graph.clear(); graph.resize(n); rem.clear(); rem.resize(n, vi(n, 0)); for(int i = 2 + c; i < 2 + c + q; i++) { graph[s].push_back(i); graph[i].push_back(s); rem[s][i] = 1; } for(int i = 2; i < 2 + c; i++) { int cc; cin >> cc; tot += cc; graph[i].push_back(t); graph[t].push_back(i); rem[i][t] = cc; } for(int i = 2 + c; i < q + 2 + c; i++) { int qq; cin >> qq; for(int j = 0; j < qq; j++) { int cc; cin >> cc; cc += 2; graph[i].push_back(cc - 1); graph[cc - 1].push_back(i); rem[i][cc - 1] = 1; } } int mx = maxFlow(); if(mx == tot) { cout << 1 << endl; for(int i = 2; i < c + 2; i++) { vi ans; for(int j = c + 2; j < c + 2 + q; j++) if(rem[i][j] > 0) ans.push_back(j - c - 2 + 1); if(ans.size()) cout << ans[0]; for(int i = 1; i < ans.size(); i++) cout << " " << ans[i]; cout << endl; } } else cout << 0 << endl; } return 0; }
int main() { int tc = 0; while(cin >> n && n) { int m; cin >> s >> t >> m; s--; t--; rem.clear(); rem.resize(n, vi(n, 0)); graph.clear(); graph.resize(n); for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; graph[a].push_back(b); graph[b].push_back(a); rem[a][b] += c; rem[b][a] += c; } cout << "Network " << ++tc << endl; cout << "The bandwidth is " << maxFlow() << "." << endl << endl; } return 0; }
void tarjanSCC(int u) { dfs_low[u] = dfs_num[u] = dfsNumberCounter++; // dfs_low[u] <= dfs_num[u] S.push_back(u); // stores u in a vector based on order of visitation visited[u] = 1; for (int j = 0; j < (int)AdjList[u].size(); j++) { ii v = AdjList[u][j]; if (dfs_num[v.first] == 0) tarjanSCC(v.first); if (visited[v.first]) dfs_low[u] = min(dfs_low[u], dfs_low[v.first]); } if (dfs_low[u] == dfs_num[u]) { // if this is a root (start) of an SCC scc.push_back(vi()); while (1) { int v = S.back(); S.pop_back(); visited[v] = 0; scc.back().push_back(v); node_to_scc_num[v] = scc_num; if (u == v) break; } scc_num++; } }
int main() { //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); init(); for (int i = 0; i < k; ++i) for (int j = i + 1; j < k; ++j) { d[i][j] = d[j][i] = get_dist(a[i], a[j]) * w; } l = 0; int cost = n * m; for (int i = 0; i < k; ++i) for (int j = i + 1; j < k; ++j) if (d[i][j] < cost) { r[l++] = piii(d[i][j], pii(i, j)); } for (int i = 0; i < k; ++i) p[i] = i; sort(r, r + l); g.resize(k); int cnt = 0; int res = 0; for (int i = 0; i < l; ++i) { int x = r[i].second.first; int y = r[i].second.second; if (get_parent(x) == get_parent(y)) continue; unite(x, y); g[x].push_back(y); g[y].push_back(x); res += r[i].first; ++cnt; } res += (k - cnt) * cost; printf("%d\n", res); memset(was, 0, sizeof was); for (int i = 0; i < k; ++i) { if (was[i]) continue; dfs(i, i); } return 0; }
int lcp(int x, int y) { int ret = 0; if (x == y) return n - x; for (int k = P.size() - 1; k >= 0 && x < n && y < n; --k) if (P[k][x] == P[k][y]) { x += 1 << k; y += 1 << k; ret += 1 << k; } return ret; }
vvi find_sccs(const vvi& adj_list) { // Reset num_scc = ticks = 0; s = stack<int>(); d = vi(adj_list.size(), UNEXPLORED); low = vi(adj_list.size()); scc = vi(adj_list.size(), NO_SCC); in_stack = vector<bool>(adj_list.size(), false); // DFS all nodes for (int a = 0; a < adj_list.size(); ++a) { if (d[a] == UNEXPLORED) tarjan(adj_list, a); } // Count which scc every node belong to vvi sccs(num_scc); for (int i = 0; i < scc.size(); ++i) { sccs[scc[i]].push_back(i); } return sccs; }
void print (const vvi& s) { int N = s.size(); for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { printf ("%c", s[i][j]); } printf ("\n"); } printf("\n"); }
int main() { ios::sync_with_stdio(0); int tc; cin >> tc; while(tc--) { scc.clear(); int n, m; cin >> n >> m; AdjList.assign(n, vii()); for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; if(a != b) AdjList[a].push_back(ii(b, 0)); } int V = n; dfs_num.assign(V, 0); dfs_low.assign(V, 0); visited.assign(V, 0); node_to_scc_num.assign(V, -1); dfsNumberCounter = numSCC = scc_num = 0; for (int i = 0; i < V; i++) if (dfs_num[i] == 0) tarjanSCC(i); graph.assign(scc_num, vi()); for(int i = 0; i < scc.size(); i++) { for(int j = 0; j < scc[i].size(); j++) for(auto &e: AdjList[scc[i][j]]) if(node_to_scc_num[e.first] != i) graph[i].push_back(node_to_scc_num[e.first]); } memo.assign(scc_num, -1); int ans = 0; for(int i = 0; i < scc_num; i++) { int cur = scc[i].size(); ans = max(ans, solve(i) + cur); } cout << ans << endl; } return 0; }
void aho_corasick(string &sentence, vector<string> &words,vvi &matches){ matches.assign(sentence.length(), vi()); int state = 0, ss = 0; for (int i = 0; i < sentence.length(); ++i, ss = state) { while (a[ss].child[mp(sentence[i])] == -1) ss = a[ss].failure; state = a[state].child[mp(sentence[i])] = a[ss].child[mp(sentence[i])]; for (ss = state; ss != -1; ss = a[ss].match_par) for (int w : a[ss].match) matches[i + 1 - words[w].length()].push_back(w); } }
bool rootedTreeIsomorphism(int r1, int r2) { L.assign(n, vi()); pred.assign(2 * n, -1); children.assign(2 * n, vi()); int h1 = dfs(r1); int h2 = dfs(r2); if (h1 != h2) return false; int h = h1 - 1; vi label(2 * n); subtreeLabels.assign(2 * n, vi()); for (int i = h - 1; i >= 0; i--) { for (int j = 0; j < (int) L[i + 1].size(); j++) { int v = L[i + 1][j]; subtreeLabels[pred[v]].push_back(label[v]); } /*for (int j = 0; j < (int) L[i].size(); j++) { int v = L[i][j]; sort(subtreeLabels[v].begin(), subtreeLabels[v].end()); }*/ sort(L[i].begin(), L[i].end(), compare); for (int j = 0, cnt = 0; j < (int) L[i].size(); j++) { if (j && !equals(L[i][j], L[i][j - 1])) ++cnt; label[L[i][j]] = cnt; } } if (!equals(r1, r2)) return false; //generateMapping(r1, r2); return true; }
void flood_fill(vvi& g) { int ct = 0; memset(&visit,0,sizeof(visit)); for(int i=0;i<g.size();i++) { if(!visit[i]) { dfs(g,i); ct++; } } cout<<ct<<endl; }
int main(){ ios::sync_with_stdio(0); int tc; cin >> tc; while (tc--) { string a; cin >> a; cin >> row >> col; dp.clear(); dp.resize(row , vi (col , -1)); vec.clear(); vec.resize(row , vi (col)); for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) cin >> vec[i][j] , dp[i][j] = -1; int ans = -1; for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) ans = max (ans , dfs (i , j)); cout << a << ": " << ans + 1<< endl; } return 0; }
int main(){ ios::sync_with_stdio(0); int n; while(cin >> n) { int a, num, b; char c; cont = 0; dfs_num.clear(); dfs_num.resize(n, -1); dfs_low.clear(); dfs_low.resize(n); dfs_parent.clear(); dfs_parent.resize(n); vec.clear(); vec.resize(n); for (int i = 0; i < n; ++i) { cin >> a >> c >> num >> c; for (int j = 0; j < num; ++j) { cin >> b; vec[a].push_back(b); } } for (int k = 0; k < n; ++k) if (dfs_num[k] == -1) artiBri(k); sort(bri.begin(), bri.end()); cout << bri.size() << " critical links" << endl; for (int l = 0; l < bri.size(); ++l) cout << bri[l].first << " - " << bri[l].second << endl; } return 0; }
void construct() { P.push_back(vi(n, 0)); compress(); vi occ(n + 1, 0), s1(n, 0), s2(n, 0); for (int k = 1, cnt = 1; cnt / 2 < n; ++k, cnt *= 2) { P.push_back(vi(n, 0)); fill(occ.begin(), occ.end(), 0); for (int i = 0; i < n; ++i) occ[i+cnt<n ? P[k-1][i+cnt]+1 : 0]++; partial_sum(occ.begin(), occ.end(), occ.begin()); for (int i = n - 1; i >= 0; --i) s1[--occ[i+cnt<n ? P[k-1][i+cnt]+1 : 0]] = i; fill(occ.begin(), occ.end(), 0); for (int i = 0; i < n; ++i) occ[P[k-1][s1[i]]]++; partial_sum(occ.begin(), occ.end(), occ.begin()); for (int i = n - 1; i >= 0; --i) s2[--occ[P[k-1][s1[i]]]] = s1[i]; for (int i = 1; i < n; ++i) { P[k][s2[i]] = same(s2[i], s2[i - 1], k, cnt) ? P[k][s2[i - 1]] : i; } } }