int main() { int t,k; scanf("%d",&t); for(k=1; k<=t; k++) { int visit[2002]= {0},n,m,i,j,o,p2; scanf("%d %d",&n,&m); p=0; for(i=0; i<=n; i++) for(j=0; j<=n; j++)ma[i][j]=0; while(m--) { scanf("%d %d",&o,&p2); ma[o][p2]=1; } for(i=1; i<=n; i++) { if(!visit[i]) { bfs(visit,n,i,0); } } printf("Scenario #%d:\n",k); if(p)printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); } }
int maxFlow() { int flow = 0; while(bfs()) { for(auto x : G[DEST]) { if(Parent[x] == 0) continue; int M = C[x][DEST] - F[x][DEST]; for(int node = x; node != SRC; node = Parent[node]) { M = min(M, C[Parent[node]][node] - F[Parent[node]][node]); } F[x][DEST] += M; F[DEST][x] -= M; for(int node = x; node != SRC; node = Parent[node]) { F[Parent[node]][node] += M; F[node][Parent[node]] -= M; } flow += M; } } return flow; }
void Graph<T>::find_next_node_bfs(const T node, const T value) { auto node_it = graph[node].begin(), node_it_end = graph[node].end(); for (;node_it != node_it_end; node_it++) if (!discovered[(*node_it)-1]) { dfs_order.push(*node_it); discovered[(*node_it)-1] = true; if (*node_it == value) { std::cout<<"Found node"<<std::endl; while (!dfs_order.empty()) dfs_order.pop(); discovered.clear(); return; } } T tmp = dfs_order.front(); dfs_order.pop(); if (dfs_order.empty()){std::cout<<"Node Not Found (BFS)"<<std::endl; return;} bfs(tmp, value); }
int countComponents(int n, vector<pair<int, int>>& edges) { unordered_map<int, vector<int>>graph; unordered_set<int>visited; int count = 0; for (auto i = 0; i < edges.size(); ++i) { graph[edges[i].first].push_back(edges[i].second); graph[edges[i].second].push_back(edges[i].first); } for (auto i = 0; i < n; ++i) { if (visited.count(i) == 0) { count++; bfs(graph, i, visited); } } return count; }
int main() { int i, j; scanf("%d %d", &N, &M); for (i = 0; i < N; i++) { scanf("%s", map[i]); } for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { if (map[i][j] == 'L') { bfs(i, j); } } } printf("%d\n", max); return 0; }
int main() { /* * Do a breadth-first search in this graph: * * 1--2--4--7 | / \ | |/ \ | | 3 5--6 | | 8--9 */ std::map<int, std::set<int>> adjList; std::map<int, bool> visited; std::map<int, int> dist; addEdge(1, 2, adjList); addEdge(1, 3, adjList); addEdge(2, 3, adjList); addEdge(2, 4, adjList); addEdge(2, 5, adjList); addEdge(4, 7, adjList); addEdge(5, 6, adjList); addEdge(6, 7, adjList); addEdge(8, 9, adjList); // Set all nodes as unvisited at first for (int i = 1; i <= 8; i++) visited[i] = false; // Perform a bfs from the node `1` bfs(1, adjList, visited, dist); // Print the distance to all visited nodes for (auto entry : dist) std::cout << "Distance from 1 to " << entry.first << " is " << entry.second << std::endl; return 0; }
int main() { int num,i,f,g,res; scanf("%d",&num); for(i=0;i<num;i++) { scanf("%d%d",&m,&n); getchar(); for(f=0;f<=m+1;f++) { for(g=0;g<=n+1;g++) { hash[f][g]=1; } } for(f=1;f<=m;f++) { for(g=1;g<=n;g++) { scanf("%c",&map[f][g]); hash[f][g]=0; } getchar(); } scanf("%d%d%d%d%d",&k,&sf,&si,&ef,&ei); res=bfs(); if(1==res) { printf("yes\n"); } else { printf("no\n"); } } return 0; }
int main(){ // int adj_mat[NUM_NODE][NUM_NODE]; int i,j; for(i=1; i<=NUM_NODE; i++){ graph[i] = create_node(i); } add_edge_1(1,2); add_edge_1(1,5); add_edge_1(2,3); add_edge_1(2,4); add_edge_1(3,4); add_edge_1(4,6); add_edge_1(5,4); add_edge_1(5,6); for(i=1; i<=NUM_NODE; i++){ visited[i] = false; } // Adjancancy matrix based representation for(i=1; i<=NUM_NODE; i++){ printf("Nodes adjacent to %d : ", i); Node * current = graph[i]; while(current){ printf("%d ", current->value); current = current->next; } printf("\n"); } bfs(graph[1]); return 0; }
int main(int argc, char *argv[]) { setIO("QTREE7"); scanf("%d",&n); t[1].init(n);t[0].init(n); for(int i = 1,x,y;i<=n-1;++i) { scanf("%d%d",&x,&y); ae(x,y);ae(y,x); } for(int i = 1;i<=n;++i) scanf("%d",&col[i]); for(int i = 1;i<=n;++i) scanf("%d",&we[i]); bfs(1); scanf("%d",&m); for(int i = 1,x,y,z;i<=m;i++) { scanf("%d%d",&x,&y); if(x == 0) printf("%d\n",t[col[y]].query(y)); else if(x == 1) { t[col[y]].cut(y); col[y]^=1; t[col[y]].link(y); } else if(x == 2) { scanf("%d",&z); t[0].modify(y,z); t[1].modify(y,z); } } closeIO(); return EXIT_SUCCESS; }
int main() { int i, j; int a, b; int row, col; int anw ; scanf("%d", &T); for (i = 0; i < T; i++) { anw = 0; memset(map, 0, sizeof(map)); scanf("%d %d %d", &M, &N, &K); for (j = 0; j < K; j++) { scanf("%d %d", &col, &row); map[row][col] = 1; } for (a = 0; a < N; a++) { for (b = 0; b < M; b++) { if(map[a][b] == 1) { bfs(a, b); anw++; } } } printf("%d\n", anw); } }
int main() { freopen("making_maze.txt", "r", stdin); setbuf(stdout, NULL); int T; scanf("%d", &T); while (T--) { scanf("%d%d", &H, &W); int i, j; for (i = 0; i < H; i++) scanf("%s", MAP[i]); // start point scanf("%d%d", &st.u, &st.v); // move count scanf("%d", &M); // moving info for (i = 0; i < M; i++) scanf("%d%d", &mrow[i], &mcol[i]); // initialize board with -1 for (i = 0; i < H; i++) for (j = 0; j < W; j++) board[i][j] = -1; board[st.u][st.v] = 0; // solve int max_mv = 0; max_mv = bfs(); // output printf("%d\n", max_mv); // clear buffer clear_buf(); } return 0; }
int main(){ int u, v, w, q, d, i; for(u=0;u<MAXN;u++){ for(v=0;v<MAXN;v++){ adjMatrix[u][v] = INF; } } scanf("%d %d", &n, &m); for(i=0;i<m;i++){ scanf("%d %d %d", &u, &v, &w); adjMatrix[u][v] = w; } scanf("%d %d", &d, &q); for(i=0;i<q;i++){ scanf("%d", &qq[i]); } for(i=0;i<q;i++){ bfs(qq[i]); res[i] = dist[d]; } for(i=0;i<q;i++){ if(res[i] == INF){ printf("Impossible\n"); }else{ printf("%d\n",res[i]); } } return 0; }
void solve() { int i,f; id=0; for(i=1;i<=n;i++) { for(f=1;f<=m;f++) { if(0 == map[i][f] && '1' == ch[i][f]) { ++id; bfs(id,i,f); } } } for(i=1;i<=n;i++) { for(f=1;f<=m;f++) { if(0 == map[i][f] && '0' == ch[i][f]) { now=-1; flag=0; bfs2(i,f); if(2 == flag) { continue; } idc[now]++; } } } for(i=1;i<=id;i++) { idc[i]=zimu[idc[i]]; } }
int main() { scanf("%d%d", &row, &column); for (int i = 0; i < row; i++) { scanf("%s", maps[i]); for (int j = 0; j < column; j++) { if (maps[i][j] == 'E') { end_x = i; end_y = j; } else if (maps[i][j] == 'S') { start_x = i; start_y = j; } } } for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { dist[i][j] = INF; } } bfs(); ans = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if (maps[i][j] >= '1' && maps[i][j] <= '9') { if (dist[i][j] <= min_step) { // printf("%d\n", dist[i][j]); ans += maps[i][j] - '0'; } } } } printf("%d\n", ans); return 0; }
void run_bfs(Graph graph, int size){ cout << "Running bfs..." << endl; graph_data bfs_data = bfs(graph,size,FIRST_VERTEX); cout << "Colors: "; for (int i = 0; i < size; ++i) { cout << bfs_data.colors[i] << " "; } cout << endl; cout << "Parents:\n"; for (int i = 0; i < size; ++i) { cout << "parent[" << i << "]: " << bfs_data.parents[i] << endl; } cout << "Distances:\n"; for (int i = 0; i < size; ++i) { cout << "distances[" << i << "," << " 0]: " << bfs_data.distances[i] << endl; } }
int main(){ insert_node(1); insert_node(2); insert_node(3); insert_node(4); insert_node(5); insert_node(6); insert_node(7); insert_node(8); insert_node(9); insert_edge(1,2,1); insert_edge(1,4,1); insert_edge(1,5,1); insert_edge(2,3,1); insert_edge(2,5,1); insert_edge(3,6,1); insert_edge(4,7,1); insert_edge(5,6,1); insert_edge(7,5,1); insert_edge(7,8,1); insert_edge(8,9,1); init_visited(); display(); printf("\n\n\n"); bfs(start); init_visited(); printf("\n\n\n"); dfs(start); return 0; }
void dfs() { int curx; int cury; int i; if(sucess == 1) return; if(check() == 0) return; if(tot == 0) { sucess = 1; return; } for(i = 1;i <= tmp; i++) { curx = pos[0][i]; cury = pos[1][i]; if(board[curx][cury] != '*') { bfs(curx, cury); } } }
main(){ int p,n,i,l,j,q,w,co=0; nodetype *edges,*e; edges=NULL; printf("No of vertex to be inserted:\n"); scanf("%d",&n); p=n; printf("Enter the vertex:\n"); while(n--){ scanf("%d",&i); edges=insert_edge(edges,i); } e=edges; printf("No of edges u want to input:\n"); scanf("%d",&l); for(j=0;j<l;j++){ scanf("%d %d",&q,&w); while(e!=NULL){ if(e->edge==q || e->edge==w) co++; e=e->next; } //printf("%d\n",co); if(co==2) edges=insert_adj_edge(edges,q,w); else{ printf("Not a valid edge enter another\n"); j=j-1; } e=edges; co=0; //display(edges); } bfs(edges,p); //display(edges); }
int min_cut(int source, int target) { int max_flow, v; edge_t *redge; if (source == target) return 0; max_flow = 0; while (bfs(source, target)) { v = target; while (v != source) { /* for? */ redge = Vertices[v].parent; redge->redge->flow += 1; redge->flow -= 1; v = redge->v; } max_flow++; } return max_flow; }
int maxflow() { int flow = 0,i; while(bfs()){ int dx = 0x7FFFFFFF; int temp; for(i = t; i != s; i = abs(pre[i])){ if(pre[i] >= 0) temp = net[pre[i]][i].c-net[pre[i]][i].f; else temp = net[i][-pre[i]].f; if(dx > temp) dx = temp; //寻找最小增量 } for(i = t; i != s; i= abs(pre[i])){ if(pre[i] >= 0) net[pre[i]][i].f += dx; //前向弧 else net[i][-pre[i]].f -= dx; //前向弧 } flow += dx; } return flow; }
int fordFulkerson(Graph graph, int source, int drain) { int u, v; int parent[vertexCount]; int max_flow = 0; int residualGraph[vertexCount*vertexCount]; for (u = 0; u < vertexCount; u++) for (v = 0; v < vertexCount; v++) residualGraph[u + vertexCount*v] = graph.getCost(u,v); while (bfs(residualGraph, source, drain, parent)) { int path_flow = INT_MAX; for (v=drain; v!=source; v=parent[v]) { u = parent[v]; path_flow = std::min(path_flow, residualGraph[u + vertexCount*v]); } for (v=drain; v != source; v=parent[v]) { u = parent[v]; residualGraph[u + vertexCount*v] -= path_flow; residualGraph[v + vertexCount*u] += path_flow; } max_flow += path_flow; } return max_flow; }
GPH_ERR_E matrix_main (void) { void* vtx; //char* saveptr = NULL; GRAPH_T* g; EDGE_T* e; unsigned long no; char* ctx = NULL; g = matrix_create (10, 10); //graph_edge_print (g); //graph_vertex_print (g); vtx = graph_vertex_find_i (g, 1); if (NULL == vtx) { fprintf (stderr, "vertext not found\n"); return GPH_ERR_ERR; } no = 0; while (no < ((VTX_UD_T*)vtx)->no) { e = graph_vertex_next_edge_get (g, vtx, &ctx); DEBUG_EDGE_PRINT_I(g,e); no++; } fprintf (stderr, "Starting BFS\n"); bfs (g, 100, bfs_vertex_func); tc_graph_edge_print (g); graphviz_description (g, "graph.lst"); //fprintf (stderr, "Starting DFS\n"); //dfs (g, 2, bfs_vertex_func); graph_delete (g); return GPH_ERR_OK; }
void bfs(int v,int f) { int u,i; node *tmp; u=v; visited[u]++; discovery[u]= ++count; back[u]= count; tmp=ptr[u]; while(tmp) { if(visited[tmp->val]==0) { //tmp->parent = u; parent[tmp->val] = u; //printf("%d",tmp->val); bfs(tmp->val,f); if(back[tmp->val] < discovery[u]) { back[u] = min(back[u], back[tmp->val]); } else { if (art[u]==0) { art[u]++; arti++;} } } else { if (parent[u] != tmp->val){ back[u] = min(discovery[tmp->val], back[u]);} } tmp=tmp->next; } }
int main() { int itr; int nCount; /* 문제의 테스트 케이스 */ int i, j; int x, y; front = rear = cnt = 0; scanf("%d", &n); scanf("%d", &pairCnt); for (i = 1; i <= n; i++){ for (j = 0; j < n; j++){ arr[i][j] = 0; } } // 초기화 for (i = 1; i <= n; i++){ visited[i] = 0; } // 초기화 for (i = 1; i <= pairCnt; i++) { scanf("%d %d", &x, &y); arr[x][y] = arr[y][x] = 1; } q[rear++] = 1; visited[1] = 1; bfs(); printf("%d", cnt-1); printf("\n"); return 0; /* 반드시 return 0으로 해주셔야합니다. */ }
int main() { node _1 { 1, nullptr, nullptr }; node _3 { 3, nullptr, nullptr }; node _2 { 2, &_1, &_3 }; node _5 { 5, nullptr, nullptr }; node _7 { 7, nullptr, nullptr }; node _6 { 6, &_5, &_7 }; node _4 { 4, &_2, &_6 }; node _9 { 9, nullptr, nullptr }; node _11 { 11, nullptr, nullptr }; node _10 { 10, &_9, &_11 }; node _13 { 13, nullptr, nullptr }; node _15 { 15, nullptr, nullptr }; node _14 { 14, &_13, &_15 }; node _12 { 12, &_10, &_14 }; node _8 { 8, &_4, &_12 }; bfs(&_8); return 0; }
int main() { int i, j; char tmp[10000]; scanf("%d %d", &N, &M); for (i = 0; i < N; i++) { scanf("%s", tmp); for (j = 0; j < M; j++) { if (tmp[j] == '1') { map[i][j] = -1; } else { map[i][j] = -2; } } } memcpy(check, map, sizeof(map)); memset(dis, 0, rear); rear = 0; //check[0][0] = 1; insert(0, 0, 1); bfs(); //printf("%d", dis[rear - 1]); return 0; }
TEST_F(DynSSSPGTest, testDynamicBFSGeneratedGraph) { METISGraphReader reader; DorogovtsevMendesGenerator generator(1000); Graph G = generator.generate(); DEBUG("Generated graph of dimension ", G.upperNodeIdBound()); DynBFS dyn_bfs(G, 0); BFS bfs(G, 0); dyn_bfs.run(); bfs.run(); DEBUG("Before the edge insertion: "); GraphEvent ev; count nInsertions = 1000, i = 0; while (i < nInsertions) { DEBUG("Sampling a new edge"); node v1 = Sampling::randomNode(G); node v2 = Sampling::randomNode(G); if (v1 != v2 && !G.hasEdge(v1, v2)) { i++; DEBUG("Adding edge number ", i); G.addEdge(v1, v2); std::vector<GraphEvent> batch; batch.push_back(GraphEvent(GraphEvent::EDGE_ADDITION, v1, v2, 1.0)); DEBUG("Running update with dynamic bfs"); dyn_bfs.update(batch); DEBUG("Running from scratch with bfs"); bfs.run(); G.forNodes([&] (node i) { // std::cout<<"Node "<<i<<":"<<std::endl; // std::cout<<"Actual distance: "<<dij.distance(i)<<", computed distance: "<<ddij.distance(i)<<std::endl; // std::cout<<"Actual number of paths: "<<dij.numberOfPaths(i)<<", computed one: "<<ddij.numberOfPaths(i)<<std::endl; EXPECT_EQ(dyn_bfs.distance(i), bfs.distance(i)); EXPECT_EQ(dyn_bfs.numberOfPaths(i), bfs.numberOfPaths(i)); }); } } }
int main(void){ int u, v, i, j, L[MAX], tL, topo; scanf("%d %d", &N, &K); memset(dG, 0, sizeof(dG)); memset(AG, 0, sizeof(AG)); while(K--){ scanf("%d", &tL); for(i = 0; i < tL; i++) scanf("%d", &L[i]); for(i = 0; i < tL; i++) for(j = i + 1; j < tL; j++) { u = L[i] - 1; v = L[j] - 1; if(!AG[u][v]) { LG[u][dG[u]++] = v; LG[v][dG[v]++] = u; AG[u][v] = 1; AG[v][u] = 1; }}} printf("%d\n", bfs(0, N - 1)); // for(i = 1; i <= N; i++){ // printf("%d: ", i); // for(j = 0; j < dG[i]; j++) // printf("[%d]", LG[i][j]); // printf("\n"); // } return 0; }
bool bfs(unordered_map<int,vector<int>>& preq,vector<bool>& checked,int checkcnt,int depth, int bound,vector<int>& rec) { if(checked[checkcnt]) return true; if(preq.find(checkcnt)==preq.end()) { checked[checkcnt]=true; rec.push_back(checkcnt); return true; } if(depth > bound) return false; for(int i = 0; i < preq[checkcnt].size(); ++i) { if(!checked[preq[checkcnt][i]]) { if(!bfs(preq,checked,preq[checkcnt][i],depth+1,bound,rec)) return false; } if(i==preq[checkcnt].size()-1) { rec.push_back(checkcnt); checked[checkcnt]=true; } } return true; }
int main() { char ss[300]; int i,j; int st,en,sum=0; while(scanf("%s",ss)) { if(strlen(ss)==3&&ss[0]=='0'&&ss[2]=='0') break; else { printf("%d. ",t); for(i=0;ss[i]!='=';i++) s[i]=ss[i]; len=i; for(i++,n=0;ss[i];i++) n=n*10+ss[i]-'0'; if(n>6000000) { printf("IMPOSSIBLE\n"); } else bfs(); } } return 0; }