void init(int r, int c){ memo.clear(); map.clear(); memo = vvi(r, vi(c, -1)); map = vvi(r, vi(c)); }
int main(){ ios::sync_with_stdio(0); int t; cin >> t; for (int i = 0; i < t; ++i) { cin >> n; vi N(n); for (int k = 0; k < n; ++k) { cin >> N[k]; } cin >> m; vi M(m); for (int j = 0; j < m; ++j) { cin >> M[j]; } adj.clear(); adj.resize(n + m + 2); for (int j = 0; j < m; ++j) { for (int k = 0; k < n; ++k) { if(( N[k] && M[j] % N[k] == 0) || (M[j] ==0)) { adj[j].push_back(k + m); adj[k + m].push_back(j); } } } int MCBM = 0; match.assign(n + m + 2, -1); for (int l = 0; l < m; ++l) { vis.assign(m, 0); MCBM += aug(l); } cout << "Case " << i + 1 << ": " << MCBM << endl; } return 0; }
int main() { ios_base::sync_with_stdio(false); cin.tie(0); while(cin >> n >> m) { ans.clear(); g.clear(); g = vvi(n, vi()); memset(vis, false, sizeof(bool)*n); for(int i=0; i<m; ++i) { int u,v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for(int i=0; i<n; ++i) { if(not vis[i]) { root = i; dfs(i); } } cout << (int)ans.size() << endl; bool first = true; for(ii edg : ans) { if(not first) cout << " "; first = false; cout << edg.first << " " << edg.second; } cout << endl << string(10, '-') << endl; } }
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; }
int ColorfulChocolates::maximumSpread (string chocolates, int _maxSwaps) { maxSwaps = _maxSwaps; colors.clear(); colors.resize(26); forn(i, chocolates.size()) colors[chocolates[i] - 'A'].pb(i);; int ret = 0; forn(i, 26) ret = max(ret, spread(i)); return ret; }
int main(int argc, char **argv){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; string s; int n; bool first=true; while(t--){ if(!first) cout<<'\n'; else first=false; cin>>s; n=s.size(); v.clear(); sum.clear(); v.resize(n); sum.resize(n,vi(n,0)); v[0]=s; for(int i=1;i<n;++i)cin>>v[i]; for(int i=0;i<n;++i) for(int j=0;j<n;++j){ sum[i][j]=(v[i][j]-'0'); if(i>0) sum[i][j]+=sum[i-1][j]; if(j>0) sum[i][j]+=sum[i][j-1]; if(i>0 && j>0) sum[i][j]-=sum[i-1][j-1]; } int largest=0,sumhere; /*for(int i=0;i<n;++i) for(int j=0;j<n;++j)cout<<sum[i][j]<<(j==n-1?'\n':' ');*/ for(int i=0;i<n;++i)for(int j=0;j<n;++j) for(int p=i;p<n;++p)for(int q=j;q<n;++q){ //cout<<"("<<i<<","<<j<<") . "<<"("<<p<<","<<q<<")"<<endl; sumhere=sum[p][q]; if(i>0) sumhere-=sum[i-1][q]; if(j>0) sumhere-=sum[p][j-1]; if(i>0 && j>0) sumhere+=sum[i-1][j-1]; if(v[i][j]=='1' && sumhere==(p-i+1)*(q-j+1)) largest=max(largest,sumhere); } cout<<largest<<'\n'; } return 0; }
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 optimal() { paths.clear(); visited.clear(); paths.insert(paths.begin(),h,vi(w,0)); visited.insert(visited.begin(),h,vb(w,false)); for(int i=0;i<w;++i) populate(0,i); int m=paths[0][0]; for(int i=1;i<w;++i) if(paths[0][i]>m) m=paths[0][i]; return m; }
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; }
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; }
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; }
int main(){ ios::sync_with_stdio(0); int rel,network = 0; string a,b; while(cin >> V >> rel && (V||rel)){ int cont = -1; map<string,int> nums; adjmat.clear(); adjmat.resize(V+5,vi(V+5,inf)); for (int k = 0; k < V; ++k) { adjmat[k][k] = 0; } for (int i = 0; i < rel; ++i) { cin >> a >> b; if(nums.find(a) == nums.end()) nums[a] = ++cont; if(nums.find(b) == nums.end()) nums[b] = ++cont; adjmat[nums[a]][nums[b]] = 1; adjmat[nums[b]][nums[a]] = 1; } warshall(); int ans = -1; for (int i = 0; i < V; ++i) { for (int j = 0; j < V; ++j) { //cout << adjmat[i][j]<<" "; ans = max(ans,adjmat[i][j]); }//cout<< endl; } cout << "Network "<< ++network << ": "; if(ans == inf) cout << "DISCONNECTED\n\n"; else cout << ans << endl<<endl; } return 0; }
int main() { ios_base :: sync_with_stdio(0); cin.tie(0); dist.reserve(505); while(scanf("%d %d %d", &n, &m, &q) != EOF) { for (int i = 0 ; i < n ;i++) { scanf("%d", &idade[i]); } v.assign(n, vi()); for (int i = 0 ; i < m; i++) { scanf("%d %d", &s, &d); --s; --d; v[d].push_back(s); } for (int i = 0 ; i < q; i++) { getchar(); scanf("%c %d", &c, &s); --s; if (c == 'P') { queue<int> q; dist.clear(); for (int i = 0 ; i < n ; i++) dist[i] = INF; dist[s] = 0; q.push(s); int mn = INF; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0 ; i < v[u].size(); i++) { int aux = v[u][i]; if (dist[aux] > dist[u] + 1) { dist[aux] = dist[u] + 1; mn = min(mn, idade[aux]); q.push(aux); } } } if (mn == INF) printf("*\n"); else printf("%d\n", mn); } else { scanf("%d", &d); --d; for (int i = 0 ; i < n; i++) { for (int j = 0; j < v[i].size(); j++) { if (v[i][j] == s) v[i][j] = d; else if (v[i][j] == d) v[i][j] = s; } } vi aux = v[s]; v[s] = v[d]; v[d] = aux; } } v.clear(); } }