深度优先搜索 dfs Matt's Graph Book
来源:互联网 发布:沙文主义 知乎 编辑:程序博客网 时间:2024/05/17 08:18
Matt loves Graph Theory. He recently enrolled into the Algorithms course and started liking it too. His teacher understood his love for graphs and decided to give him a problem.
She gives him a Tree and asks him to write a program to find if it's connected or not. She realizes that Matt would do this in no time. So, instead she gives him a Graph and then removes a vertex from it and now asks him to find if the resultant graph is connected or not.
Since he is new to programming and algorithms, you have to help him solve this problem.
Input
First line contains a single integer N denoting the number of vertices in the graph. Second line contains a single integer k denoting the number of edges in the graph. k lines follow each containing two space separated integers a and b denoting the edge between the vertices a and b. Then, the last line contains a single integer x which denotes the vertex that is removed from the graph.
Output
"Connected" (without quotes) if the resultant graph is connected. "Not Connected" (without quotes), otherwise.
Constraints
1 ≤ N ≤ 105
1 ≤ k ≤ 2×105
0 ≤ a,b ≤ 105
0 ≤ x ≤ 105
433 00 11 22
Connected
Number of vertices: 4
Number of edges: 3
It is still Connected.
#include <bits/stdc++.h>#include <vector>#define mod 1000000007using namespace std;const int mn=200005;vector <int>adj[200005];bool visited[200005]={0};void dfs(int s){ visited[s]=1; for(int i=0;i<adj[s].size();++i) { if(!visited[adj[s][i]]) dfs(adj[s][i]); }}int main(){ int nodes,edges; cin>>nodes>>edges; int u,v; for(int i=0;i<edges;++i) { cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } int x; cin>>x; adj[x].clear(); visited[x]=1; if(x!=0)dfs(0); else dfs(1); bool ok=true; for(int i=0;i<nodes;++i) { if(!visited[i]){ ok=false; break; } } if(!ok) cout<<"Not Connected"; else cout<<"Connected"; return 0;}
- 深度优先搜索 dfs Matt's Graph Book
- 深度优先搜索 dfs Monk and Graph Problem
- 深度优先搜索 DFS
- 深度优先搜索 DFS
- DFS 深度优先搜索
- DFS 深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- DFS深度优先搜索
- 深度优先搜索(DFS)
- dfs深度优先搜索
- dfs 深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- DFS深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- 最新7篇数据科学/深度学习/CNN/知识图谱/文本匹配等中英文综述论文推介(附下载)
- linux安装numpy
- 检测Android是否安装了某个程序
- 关于ConstraintLayout的部分属性总结
- 【刷题】Linked List Cycle II
- 深度优先搜索 dfs Matt's Graph Book
- node模块
- 《大象 Thinking in UML》学习笔记(七)——UML核心视图之动态视图:活动图、时序图
- 输入若干个整数输入是-1时,输入结束;后计算所有元素之和及平均值
- Android kotlin fragment中使用布局控件
- Java_HttpClientUtil
- webapp开发参考
- 多线程首次项目应用一些细节
- (六)Lucene Index Toolbox(Luke) 各个版本下载地址