void articulation(string u) { dfs_low[u] = dfs_num[u] = dfsNumberCounter++; for(msi::iterator it = adjList.lower_bound(u); it != adjList.upper_bound(u); it++) { si pair_s = it->second; if (dfs_num[pair_s.first] == WHITE) { dfs_parent[pair_s.first] = u; if (u == dfsRoot) rootChildren++; articulation(pair_s.first); if (dfs_low[pair_s.first] >= dfs_num[u]) articulation_vertex[u] = true; if (dfs_low[pair_s.first] > dfs_num[u]) cout << "Bridge: (" << u << "," << pair_s.first << ") " << endl; dfs_low[u] = min(dfs_low[u], dfs_low[pair_s.first]); } else if (pair_s.first != dfs_parent[u]) { dfs_low[u] = min(dfs_low[u], dfs_num[pair_s.first]); } } }
int main () { int V,E; string X,Y; string root; cin >> V >> E; for(int i=0; i<E; i++) { cin >> X >> Y; if(find(v_names.begin(), v_names.end(), X) == v_names.end()) v_names.push_back(X); if(find(v_names.begin(), v_names.end(), Y) == v_names.end()) v_names.push_back(Y); adjList.insert(make_pair(X,make_pair(Y,0))); } cin >> root; bfs(root); print(V, root); cin >> X >> Y; printPath(X,Y); return 0; }
int main() { int V,E; string X,Y; cin >> V >> E; for (int i=0; i<E; i++) { cin >> X >> Y; if(find(v_names.begin(), v_names.end(), X) == v_names.end()) v_names.push_back(X); if(find(v_names.begin(), v_names.end(), Y) == v_names.end()) v_names.push_back(Y); adjList.insert(make_pair(X,make_pair(Y,0))); adjList.insert(make_pair(Y,make_pair(X,0))); } for (vector<string>::iterator it = v_names.begin(); it != v_names.end(); it++) { if (dfs_num[*it] == WHITE) { dfsRoot = *it; rootChildren = 0; articulation(*it); articulation_vertex[dfsRoot] = (rootChildren > 1); } } cout << "Articulation Points: " << endl; for (vector<string>::iterator it = v_names.begin(); it != v_names.end(); it++) { if (articulation_vertex[*it]) cout << "Vertex: " << *it << endl; } return 0; }
int main() { int t,r,p,i,j,k,m,nr,cost; scanf("%d",&t); while(t--){ scanf("%d",&n); names.clear(); for(i=1;i<=n;i++){ scanf("%s",input); // cities[i]=string(input); names.insert(make_pair(string(input),i)); scanf("%d",&p); adjcnt[i]=p; for(j=0;j<p;j++){ scanf("%d %d",&nr,&cost); AL[i].push_back(make_pair(nr,cost)); // add a neigh and cost } } scanf("%d",&r); for(j=0;j<r;j++){ scanf("%s %s",src,dst); query[j]=make_pair(string(src),string(dst)); } // solve string s1,s2; int srci,dsti; for(j=0;j<r;j++){ s1=query[j].first; s2=query[j].second; srci=names[s1];dsti=names[s2]; printf("%d\n",dijkstra(srci,dsti)); } for(j=1;j<=n;j++)AL[j].clear(); } return 0; }
void bfs(string s) { queue<string> q; q.push(s); bfs_dist[s] = 0; while(!q.empty()) { string u = q.front(); q.pop(); for(msi::iterator it = adjList.lower_bound(u); it != adjList.upper_bound(u); it++) { si pair_s = it->second; if(!bfs_dist.count(pair_s.first)) { bfs_dist[pair_s.first] = bfs_dist[u] + 1; bfs_parent[pair_s.first] = u; q.push(pair_s.first); } } } }
int main() { //READ("input.txt"); //WRITE("output.txt"); int test, cs=0, st, en, index, edges, sum, takenEdges, i, w, stp, enp; string sst, sen; scanf("%d", &test); while(test--) { scanf("%d", &edges); hash.clear(); index = 0; for(i=0; i<edges; i++) { cin >> sst >> sen >> w; if(!hash[sst]) { hash[sst] = ++index; } if(!hash[sen]) { hash[sen] = ++index; } st = hash[sst]; en = hash[sen]; g[i].st = st; g[i].en = en; g[i].w = w; } sort(g,g+edges, comp); sum = takenEdges=0; for(i=1; i<=index; i++)pre[i]=i; for(i=0; i<edges; i++) { stp = findPre(g[i].st); enp = findPre(g[i].en); if(stp!=enp) { sum += g[i].w; pre[stp] = enp; takenEdges++; } } printf("Case %d: ", ++cs); if(takenEdges==index-1) printf("%d\n", sum); else printf("Impossible\n"); } return 0; }
int main(){ int nTest; cin >> nTest; while( nTest-- ){ int f; string f1, f2; cin >> f; parent.clear(); rank.clear(); for( int i = 0; i < f; ++i ){ cin >> f1 >> f2; if( parent[f1] == "" ) parent[f1] = f1; if( parent[f2] == "" ) parent[f2] = f2; merge( f1, f2 ); cout << rank[find(f1)] + 1 << "\n"; } } }