深度优先搜索 dfs Matt's Graph Book

来源:互联网 发布:沙文主义 知乎 编辑:程序博客网 时间:2024/05/17 08:18
中文题意:就是一个图,去掉一条边后问你连不连通
Matt's Graph Book
Attempted by: 1659
/
Accuracy: 52%
/
Maximum Score: 20
/
 
4 Votes
Tag(s):
 

Algorithms, Easy

PROBLEM
EDITORIAL
MY SUBMISSIONS
ANALYTICS

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

SAMPLE INPUT
 
433 00 11 22
SAMPLE OUTPUT
 
Connected
Explanation

Number of vertices: 4 
Number of edges: 3

enter image description here

It is still Connected.

Time Limit:2.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:1024 KB
#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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电弧光打了眼睛怎么办 被电焊光伤了眼怎么办 被弧光闪了眼睛怎么办 乌龟下面壳烂了怎么办 小孩卵圆孔未闭合该怎么办 刚辞职又后悔了怎么办 隼羽头饰卖了怎么办 u盘变成只读了怎么办 小孩被蟑螂咬了怎么办 被蟑螂咬出血了怎么办 多肉植物张长了怎么办 蟹爪莲叶子蔫了怎么办 混沌与秩序2延迟怎么办 混沌与秩序2脸书怎么办 ios炉石传说卡门怎么办 狗狗对主人低吼怎么办 吹雪之松锦徒长怎么办 武装突袭被禁了怎么办 眼球小血管破了怎么办 眼睛白眼球破了怎么办 打拳击手腕伤了怎么办 上眼皮进东西了怎么办 眼睛进了异物怎么办妙招 眼睛毛血管破了怎么办 怪物猎人x钱不够怎么办 小米2开不了机怎么办 3ds更新系统不动怎么办 u盘中病毒了怎么办 aj5白银前面皱了怎么办 狗吃了巧克力该怎么办 孩子零食吃多了怎么办 上学时月经侧漏怎么办 漏电被电的脚肿怎么办 走路有尿溢出来怎么办 篮球气嘴漏气了怎么办 暗线插座盒坏了怎么办 焊过电焊眼睛疼怎么办 烧电焊后眼睛痛怎么办? 焊完电焊眼睛疼怎么办 烧电焊后眼睛痛怎么办 用电焊后眼睛痛怎么办