int main(){ while(scanf("%d",&N) == 1 && N){ int sum = 0,ans = INT_MAX,cmax = 0; L.resize(N); taken.resize(N); loop(i,N) { scanf("%d",&L[i]); sum += L[i]; cmax = max(cmax,L[i]); taken[i] = 0; } sort(all(L)); for(int i = sum;i>=1;i--){ if(sum%i == 0){ int l = sum/i,n = i; if(l >= cmax){ fill(all(taken),0); bool c = possibe(0,0,0,l,n); if(c){ ans = l; break; } } } } printf("%d\n",ans); }
// Constructs the lookup table in O(V log V) time and memory lowest_common_ancestor(const vvi& tree = {}, int root = 0) { depth.resize(tree.size()); pos.resize(tree.size()); vector<visit> tour; if (tree.size()) dfs(tree, tour, root, root); euler = sparse_table<visit>(tour); }
int main(){ freopen("1199.txt", "r", stdin); //setbuf(stdout, NULL); scanf("%d", &N); adj = vvi(N, vi(N)); int total_edge = 0; in_degree.resize(N); out_degree.resize(N); for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ scanf("%d", &adj[i][j]); out_degree[i] += adj[i][j]; in_degree[j] += adj[i][j]; } } bool flag = false; for(int i = 0; i < N; i++){ if( (out_degree[i] & 1) || (in_degree[i] & 1)){ flag = true; break; } } if(flag){ printf("-1\n"); } else{ vi trace; solve(0, trace); for(int i = 0; i < trace.size(); i++){ printf("%d ", trace[i] + 1); } printf("\n"); } }
heavy_light(const vvi &graph, int root) { links = lca(graph, 0); st = seg_tree<T>(graph.size()); index.resize(graph.size()), jump.resize(graph.size()); dfs(root, root, root, graph); }
int main(){ int N, M, a, b, c; while(cin >> N >> M && N != 0 && M != 0){ AdjList.resize(N); dfs_num.resize(N); visited.resize(N); dfs_low.resize(N); numSCC = dfsNumberCounter = 0; for(int i = 0; i < M; i++){ cin >> a >> b >> c; AdjList[a-1].push_back(b-1); if(c == 2) AdjList[b-1].push_back(a-1); } for(int i = 0; i < N; i++) if(dfs_num[i] == 0) tarjan(i); if(numSCC == 1) cout << 1 << endl; else cout << 0 << endl; visited.clear(); AdjList.clear(); dfs_low.clear(); dfs_num.clear(); S.clear(); } }
int main() { int tc; cin >> tc; while (tc--) { int n, m; cin >> n >> m; AdjList.clear(); AdjList.resize(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; AdjList[a].push_back(ii(b, 0)); } bool cac = true; int numScc = 0; dfs_num.clear(); dfs_num.resize(n, UNVISITED); dfs_parent.clear(); dfs_parent.resize(n, 0); for(int i = 0; i < n; i++) { if(dfs_num[i] == UNVISITED) { numScc++; if(graphCheck(i)) { cac = false; break; } } } if(cac && numScc == 1) cout << "YES\n"; else cout << "NO\n"; } return 0; }
void init() { pertenece.resize(n); rank.resize(n, 0); size.resize(n, 1); for(int i = 0; i < n; i++) { pertenece[i] = i; } }
Task(int _tl, int _testsCnt) { tl = _tl; testsCnt = _testsCnt; averages.resize(testsCnt + 1, 0); counters.resize(testsCnt + 1, 0); sums.resize(testsCnt + 1, 0); averages[testsCnt] = (tl / AVG_CONST) * testsCnt; }
void scc () { nr.resize (gr.size (), -1); low.resize (gr.size ()); inst.resize (gr.size (), false); for (int i = 0; i < gr.size (); ++i) { if (nr[i] < 0) { visit (i); } } }
void init(int n) { h.resize(n, -1); par.resize(n, -1); //add 1 vi v1; v1.pb(1); Node z1(1, 1, 0, v1); G1.pb(z1); h[1] = 1; h[0] = 0; }
int main () { #ifndef ONLINE_JUDGE freopen(INP, "r", stdin); freopen(OUT, "w", stdout); #endif scanf("%d", &n); order.clear(); arr1.assign(n+1,0); a1.clear(); FOR(i,1,n) scanf("%d", &a), a1.pb(a); REP(i,n) arr1[a1[i]-1] = i+1; REP(i,n) order[arr1[i]] = i; while(scanf("%d", &a) != EOF){ arr.assign(n+1,0); a2.clear(); a2.pb(a); ans.resize(0); len = 0; FOR(i,1,n-1) scanf("%d", &a), a2.pb(a); REP(i,n) arr[a2[i]-1] = i+1; REP(i,n) updateLis(arr[i]); cout << len << endl; } return 0; }
void clear() { dfs_num.clear(); dfs_num.resize(v+1); connectedComp.clear(); cnt = 0; }
// calculates the "male optimal" stable marriage (i.e. there // is no stable marriage where any man prefers his match // over the one given here) a female optimal matching can be // found by swapping men and women // mpref: vector i is the list of i's preferred partners, in // *decreasing* order of preference (0 indexed) // fpref: as above, but for women // match: a vector (passed in with any size) filled so that // position i gives the man matched with woman i void stable_marriage(const vvi& mpref, const vvi& fpref, vi& match) { // initially no one matched match.resize(mpref.size(), -1); // get a map from (w, m) to w's "rank" for m vvi fmap(match.size(), vi(match.size())); for(int i = 0; i < match.size(); ++i) for(int j = 0; j < match.size(); ++j) fmap[i][fpref[i][j]] = j; vi next_prop(match.size(), 0); queue<int> mfree; for(int i = 0; i < match.size(); ++i) mfree.push(i); while(!mfree.empty()) { const int m = mfree.front(); const int w = mpref[m][next_prop[m]]; mfree.pop(); if(match[w] == -1) { match[w] = m; } else if(fmap[w][match[w]] > fmap[w][m]) { mfree.push(match[w]); match[w] = m; } else if(++next_prop[m] < match.size()) { mfree.push(m); } } }
int main() { ifstream fin("marathon.in"); ofstream fout("marathon.out"); int N; fin >> N; points.resize(N); for (int i = 0; i < N; ++i) { fin >> points[i].first >> points[i].second; } T.resize(N - 2); res.resize(N - 2); T[0].first = 0; T[0].second = dist_cont(2, N-1); res[0] = T[0].first + T[0].second + dist_two(0, 2); int result = res[0]; for (int k = 1; k <= N - 3; ++k) { T[k].first = T[k - 1].first + dist_two(k - 1, k); T[k].second = T[k - 1].second - dist_two(k + 1, k+2); res[k] = T[k].first + T[k].second + dist_two(k, k + 2); result = min(result, res[k]); } fout << result << '\n'; return 0; }
int main() { ios::sync_with_stdio(0); double d; int m, tc = 1; while( cin >> d >> m) { cout << "Case " << tc++ << ":" << endl; memo.clear(); memo.resize(105, -1); prices.clear(); prices.resize(m + 1); prices[0].first = 1; prices[0].second = (d + eps) * 100; for(int i = 1; i <= m; i++) { int tmp; cin >> tmp >> d; prices[i].first = tmp; prices[i].second = (d + eps) * 100; } string str; cin.get(); getline(cin, str); stringstream ss(str); int tmp; while(ss >> tmp) { cout << "Buy " << tmp << " for $" << fixed << setprecision(2) << (double)solve(tmp) / 100 << endl; } } return 0; }
int main(void) { std::ios_base::sync_with_stdio (false); size_t n; cin >> n >> ws; vvs graph(n, vs()); dfs_num.resize(n, 0); parent.resize(n, -1); finished = false; a = b = c = -1; string line; for (size_t line_cnt = 0; line_cnt < n; line_cnt++) { getline(cin, line); for (size_t char_cnt = 0; char_cnt < n; char_cnt++) { if (line.at(char_cnt) == '1') graph[line_cnt].push_back(char_cnt); } } for (size_t counter = 0; counter < n; counter++) if (!dfs_num[counter]) dfs(graph, counter); if (a >= 0) cout << a+1 << " " << b+1 << " " << c+1 << endl; else cout << -1 << endl; return 0; }
int maxFlow() { int result = 0; while(dinic_bfs()) { work.clear(); work.resize(n, 0); while(int d = dinic_dfs(s, inf)) result += d; } return result; }
Solution(int _taskNum) { taskNum = _taskNum; curTest = sum = testsComplete = 0; testStart.resize(task[taskNum].testsCnt); average = task[taskNum].averages.back(); if (DEBUG) cerr << "new solution with average = " << average << endl; counted = false; }
void blmnfrd() { dist.clear(); dist.resize(n, -inf); dist[0] = 100; for (int i = 0; i < n - 1; ++i) for (int j = 0; j < n; ++j) for (auto k : vec[j]) if (dist[j] + k.first > 0) dist[k.second] = max(dist[k.second], dist[j] + k.first); }
int main() { int n ; cin >> n; position.resize(n+1) ; edges.resize(n+1) ; int a, b, c ; rep(i,n) { cin >> a >> b >> c; edges[a].left = b , edges[a].right = c ; }
inline void get_visited(vi & visited, vi & states) { states.resize(0); for(int i = 0; i != visited.size(); i++) { if(visited[i]) { states.push_back(i); visited[i] = white; } } }
int main() { s(N) ; s(K) ; pos.resize(N); rep(0,N-1,i) s(pos[i]) ; sort(pos.begin() , pos.end()); p(solve(0,100000000)); return 0; }
int main(){ ios::sync_with_stdio(0); int count=0; while(cin >> n && n){ cout << "**********************************************************\n"; cout << "Network #"<<++count << endl; dist.clear(); dist.resize(n,vd(n)); memset(memo,-1,sizeof memo); X.clear();X.resize(n); Y.clear();Y.resize(n); for (int i = 0; i < n; ++i) { cin >> X[i] >> Y[i]; } for (int j = 0; j < n; ++j) { for (int i = j+1; i < n; ++i) { dist[j][i]= dist[i][j] = dis(j,i); //cerr << "dist " << j << " " << i <<" -> "<< dist[j][i] << endl; } } double ans = inf; int id =-1; for (int k = 0; k < n; ++k) { double val = tsp(k,(1<<k)); //cerr << val << endl; if(val < ans) ans = val,id=k; } //id=0; //cerr << id << endl; int msk=0; while(--n) { msk |= (1 << id); if(dis(id,par[id][msk])!=16) { printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n", X[id], Y[id], X[par[id][msk]], Y[par[id][msk]], dis(id, par[id][msk]) ); id = par[id][msk]; } } printf("Number of feet of cable required is %.2lf.\n",ans); } return 0; }
int solve(){ dp.resize(v.size()); dp[0]=v[0]; int maxi=dp[0]; for(int i=1;i<dp.size();i++){ if(dp[i-1]>0)dp[i]=dp[i-1]+v[i]; else dp[i]=v[i]; maxi=max(maxi,dp[i]); } return maxi; }
void generateMapping(int r1, int r2) { map.resize(n); map[r1] = r2 - n; sort(children[r1].begin(), children[r1].end(), compare); sort(children[r2].begin(), children[r2].end(), compare); for (int i = 0; i < (int) children[r1].size(); i++) { int u = children[r1][i]; int v = children[r2][i]; generateMapping(u, v); } }
int main(){ ios::sync_with_stdio(0); while(cin >> n && (n != -1)) { vec.clear(); vec.resize(n); int enrgy, b, num; for (int i = 0; i < n; ++i) { cin >> enrgy >> num; for (int j = 0; j < num; ++j) { cin >> b; vec[i].push_back(ii(enrgy, b - 1)); } } blmnfrd(); vis.clear(); vis.resize(n , 0); dfs(0); vi vis1 = vis; if(dist[n - 1] > 0) cout << "winnable" << endl; else { bool happen = false; for (int i = 0; i < n && !happen; ++i) for (auto j : vec[i]) if (dist[j.second] < dist[i] + j.first && dist[i] + j.first > 0) { vis.clear(); vis.resize(n,0); dfs(i); if (vis[n - 1] && vis1[i]) { happen = true; break; } } //if(chk()) cout << "winnable" << endl; if (happen) cout << "winnable" << endl; else cout << "hopeless\n"; } } return 0; }
int main(){ ios::sync_with_stdio(0); int door,w,a; while(cin >> n && (n!=-1)){ adjlist.clear(); adjlist.resize(n); vis.clear(); vis.resize(n,0); for (int i = 0; i < n; ++i) { cin >> w >> door; while(door--){ cin >> a; adjlist[i].push_back(ii(a-1,w)); } } bellmanFord(); if(dist[n-1] > 0) cout << "winnable\n"; else{ bool haspos = false; for (int i = 0; i < n && !haspos; ++i) { for(auto & e: adjlist[i]){ if(dist[i] + e.second > 0 && dist[e.first] < dist[i] + e.second){ vis.clear(); vis.resize(n,0); dfs(i); if(vis[n-1] ){ haspos = true; break; } } } } if(haspos) cout << "winnable\n"; else cout << "hopeless\n"; } } return 0; }
void run() { readln(n); a.resize(n, -1); int x; fori(n - 1) readln(x), a[x - 1] = 1; fori(n) if (a[i] == -1){ writeln(i + 1); return;} }
int lis(const vl& in, vi& liss, vi& parents) { // position j gives index of the last element in "best" // known lis of length j (where best means ends in // smallest element) vi length_idx(in.size() + 1, -1); // position i gives the immediate predecessor of element i // in best known lis parents.resize(in.size(), -1); // gives the length of the best known lis ending in pos i liss.resize(in.size(), -1); // best known length int L = 0; for(int i = 0; i < in.size(); ++i) { // binary search for largest pos j <= L s.t. // in[length_idx[j]] < in[i] // i.e. the lis we can append in[i] onto int low = 1, high = L; while(low <= high) { int mid = ceil((low+high)/2); if(in[length_idx[mid]] < in[i]) low = mid+1; else high = mid-1; } // low is length of lis ending in in[i] parents[i] = length_idx[low-1]; length_idx[low] = i; liss[i] = low; // update best known length L = max(L, low); } return L; }
bool dinic_bfs() { dist.clear(); dist.resize(n, -1); dist[s] = 0; queue<int> queue1; queue1.push(s); while(!queue1.empty()) { int u = queue1.front(); queue1.pop(); for(auto &e : graph[u]) { if(dist[e] != -1 || rem[u][e] <= 0) continue; dist[e] = dist[u] + 1; queue1.push(e); } } return (dist[t] != -1); }