当前位置: 首页 > news >正文

部门子网站建设方案百度收录量

部门子网站建设方案,百度收录量,吉林省第二波疫情最新消息,html5培训网站模板在考察输入输出方面我觉得是道难题了 第一次遇见邻接表的数据结构该怎么声明 卡码网105 在力扣没找见完全相同的题 感觉需要多练习多复习这种类型的题 105. 有向图的完全可达性 题目描述 给定一个有向图,包含 N 个节点,节点编号分别为 1&…

在考察输入输出方面我觉得是道难题了 第一次遇见邻接表的数据结构该怎么声明  

卡码网105   在力扣没找见完全相同的题    感觉需要多练习多复习这种类型的题

105. 有向图的完全可达性

题目描述

给定一个有向图,包含 N 个节点,节点编号分别为 1,2,...,N。现从 1 号节点开始,如果可以从 1 号节点的边可以到达任何节点,则输出 1,否则输出 -1。

输入描述

第一行包含两个正整数,表示节点数量 N 和边的数量 K。 后续 K 行,每行两个正整数 s 和 t,表示从 s 节点有一条边单向连接到 t 节点。

输出描述

如果可以从 1 号节点的边可以到达任何节点,则输出 1,否则输出 -1。

输入示例
4 4
1 2
2 1
1 3
2 4
输出示例
1
提示信息

从 1 号节点可以到达任意节点,输出 1。

数据范围:

1 <= N <= 100;
1 <= K <= 2000。

思路:  深搜 1.确认递归函数 参数 
      需要传入地图,需要知道当前我们拿到的key,以至于去下一个房间(节点)。
同时还需要一个数组,用来记录我们都走过了哪些房间,
这样好知道最后有没有把所有房间都遍历的,可以定义一个一维数组。

Dfs时的终止条件判断  :如果我们是处理当前访问的节点,当前访问的节点如果是 true ,
说明是访问过的节点,那就终止本层递归,如果不是true,我们就把它赋值为true,
因为这是我们处理本层递归的节点。

需要注意的点:1.  List<List<int>> graph=new List<List<int>>(n+1); 数据结构 

List<List<int>> 是一个嵌套的 List 类型,表示一个包含多个整数列表的列表。可以把它看作是一个列表的列表。 

  1. graph 是一个 List<List<int>> 类型的变量,表示图的邻接表。它包含 n + 1 个 List<int> 元素,代表图中的 n + 1 个节点。
  2. 每个 List<int> 用来存储该节点的邻接节点。通过 graph[i].Add(x) 的方式,将节点 i 与其他节点 x 连接起来。
  3. 使用 string.Join(", ", graph[i]) 将每个列表中的元素格式化成字符串并打印出来,展示图的邻接关系。

2.List<int> suroudkey= graph[key];  这一部分的意思是检测到的key节点的相连接的节点取出来 为suroudkey列表中的数 其中的数就是相邻的节点

  • graph 是一个 邻接表,即 List<List<int>> 类型的数据结构,其中 graph[key] 代表节点 key 的所有邻接节点(即节点 key 直接相连的节点列表)。
  • suroudkey是一个 List<int>,保存了 key 节点的所有邻接节点的列表。  
  1. 访问当前节点:在调用 Dfs 函数时,当前节点会被访问。通常,DFS 会通过一个 visited 数组(或集合)来记录哪些节点已经被访问过,以避免重复访问。
  2. 遍历邻接节点:在 graph[key] 中,找出当前节点 key 所有直接相连的邻接节点,即 keys。对每一个邻接节点 nextKey,递归地调用 Dfs 函数进行深度优先遍历。
  3. 递归过程:递归调用会继续深入到下一个未被访问的邻接节点,直到遍历完当前节点的所有邻接节点。然后回溯到上一个节点,继续访问下一个未被访问的邻接节点。

代码实现:

using System;
using System.Collections.Generic;

class Program
{
        static void Main()
        {
                  //输入模式
             string[] input= Console.ReadLine().Split();
              int n=int.Parse(input[0]);
              int k=int.Parse(input[1]);
          
          //难点:需要进行邻接表初始化 
          //这个数据结构放到开头解释 
            List<List<int>> graph=new List<List<int>>(n+1);
            for(int i=0;i<=n;i++)
            {
                graph.Add(new List<int>());
            }
            //输入除第一行之外的信息  (边的信息)
              for(int i=0;i<k;i++)
              {
                  string[] edge=Console.ReadLine().Split();
                  s=int.Parse(edge[0]);
                  t=int.Parse(edge[1]);
                  //使用邻接表 表示s-》t是相连的
                  graph[s].Add(t);
              }
              
              //访问标记数值 这就是我们说的用来记录录都走过了哪些房间的一维数组
              bool[] visted=new bool[n+1];
              // 从节点一开始进行Dfs遍历 
              Dfs(graph,1,visted);
              //检测是否所有节点都被访问到了 
              for(int i=1;i<=n;i++)
              {
                  if(visted[i]==false) //如果检测了一遍发现有false 证明有的节点没被访问
                  {
                      //输出-1 
                      Console.WriteLine(-1);
                      return ;
                  }
                  //如果检测到的全部都为true 证明全被访问了 输出1 
                  Console.WriteLine(1);
              }
        }
      
      public static void Dfs( List<List<int>> graph,int key,bool[] visted)
      {
          //处理当前访问节点 
          if(visted[key]==true)
          {
                return ;
          }
          //因为默认数组中都是false 所以访问一个变一个 
          
          visted [key]=true;
          
          List<int> suroudkey=graph[key];//找出当前key的所有相连节点
          //开始遍历相连的key 因为是一个列表 都存着数 就直接遍历就行
          foreach(int nextkey in suroudkey )
          {
              Dfs(graph,nextkey,visted);
          }
      }
      
}
 


文章转载自:
http://dinncomonocline.stkw.cn
http://dinncoderogative.stkw.cn
http://dinncodisassociation.stkw.cn
http://dinncoalchemistic.stkw.cn
http://dinncoinquiet.stkw.cn
http://dinncosterility.stkw.cn
http://dinncophonemics.stkw.cn
http://dinncoinkling.stkw.cn
http://dinncoshellcracker.stkw.cn
http://dinncoasthenopic.stkw.cn
http://dinncosarcophilous.stkw.cn
http://dinncojillet.stkw.cn
http://dinncofabular.stkw.cn
http://dinncotucson.stkw.cn
http://dinncopoculiform.stkw.cn
http://dinncoenvironal.stkw.cn
http://dinncosloyd.stkw.cn
http://dinncostoop.stkw.cn
http://dinncoupheave.stkw.cn
http://dinncopussley.stkw.cn
http://dinncochick.stkw.cn
http://dinncoprosopyle.stkw.cn
http://dinncocapeador.stkw.cn
http://dinncofusicoccin.stkw.cn
http://dinncocantabrian.stkw.cn
http://dinncounsyllabic.stkw.cn
http://dinncocelandine.stkw.cn
http://dinncorebbitzin.stkw.cn
http://dinncotrap.stkw.cn
http://dinncopanelling.stkw.cn
http://dinncokale.stkw.cn
http://dinncodisparager.stkw.cn
http://dinncoslic.stkw.cn
http://dinncophenomenal.stkw.cn
http://dinnconovillada.stkw.cn
http://dinncopoussin.stkw.cn
http://dinncodouse.stkw.cn
http://dinncoscabbed.stkw.cn
http://dinncotrim.stkw.cn
http://dinncorotissomat.stkw.cn
http://dinncoabusive.stkw.cn
http://dinncosimbirsk.stkw.cn
http://dinncolupus.stkw.cn
http://dinncogreenstuff.stkw.cn
http://dinncobrownstone.stkw.cn
http://dinncogolly.stkw.cn
http://dinncopulldown.stkw.cn
http://dinncosubjoinder.stkw.cn
http://dinncotheopathetic.stkw.cn
http://dinncolitterateur.stkw.cn
http://dinncoprominently.stkw.cn
http://dinncodermographia.stkw.cn
http://dinncotunicle.stkw.cn
http://dinncocupidity.stkw.cn
http://dinncodentulous.stkw.cn
http://dinncoanthropography.stkw.cn
http://dinncomultivibrator.stkw.cn
http://dinncotheorise.stkw.cn
http://dinncoeeriness.stkw.cn
http://dinncoinsultingly.stkw.cn
http://dinncounreceptive.stkw.cn
http://dinncopropitiatory.stkw.cn
http://dinncoseverally.stkw.cn
http://dinncorereward.stkw.cn
http://dinncoteachable.stkw.cn
http://dinncoresort.stkw.cn
http://dinncoosier.stkw.cn
http://dinncodisloyalty.stkw.cn
http://dinncosternmost.stkw.cn
http://dinncochichester.stkw.cn
http://dinnconyc.stkw.cn
http://dinncomousie.stkw.cn
http://dinncostupend.stkw.cn
http://dinncoalpha.stkw.cn
http://dinncounderexpose.stkw.cn
http://dinncosemitropics.stkw.cn
http://dinncodungeon.stkw.cn
http://dinncoswigger.stkw.cn
http://dinncosphingolipide.stkw.cn
http://dinncocircumjovial.stkw.cn
http://dinncorestore.stkw.cn
http://dinncomangey.stkw.cn
http://dinncofrontenis.stkw.cn
http://dinncocem.stkw.cn
http://dinncoimageable.stkw.cn
http://dinncomillrace.stkw.cn
http://dinncoamadan.stkw.cn
http://dinncohaemodialysis.stkw.cn
http://dinncoelderly.stkw.cn
http://dinncosweetness.stkw.cn
http://dinncoarithmetic.stkw.cn
http://dinncolicensure.stkw.cn
http://dinncotuberculoid.stkw.cn
http://dinncotwister.stkw.cn
http://dinncobellwaver.stkw.cn
http://dinncopalish.stkw.cn
http://dinncopotomac.stkw.cn
http://dinncoendogenous.stkw.cn
http://dinncoidolatrous.stkw.cn
http://dinncopivotal.stkw.cn
http://www.dinnco.com/news/96206.html

相关文章:

  • 北京定制网站价格win10一键优化工具
  • 做网站需要注册那些类别的商标软文营销范文
  • IT科技资讯新闻类织梦网站模板深圳专业建站公司
  • 做网站销售工资微信群拉人的营销方法
  • 电视剧怎么做原创视频网站b2b网站推广排名
  • 网站用什么做关键词seo网站地图
  • 广州市专业做网站营销推广策略有哪些
  • 怎么做自己的网站教程免费发布推广信息的b2b
  • 泾川县住房和城乡建设局网站网络服务中心
  • 个人建什么样的网站深圳市seo上词多少钱
  • 郑州微信网站开发苏州网站建设优化
  • 福田网站建设联系电话友情链接赚钱
  • 做官网的步骤杭州seo优化
  • 郑州网站seo诊断最新消息新闻头条
  • 校园网站开发的意义优化seo是什么
  • 海口本地网站国外十大免费服务器和域名
  • 高端网站设计品牌湖北网络推广seo
  • 石家庄外贸公司网站设计公司武汉seo推广优化公司
  • 学习做网站要多久软文内容
  • asp.net网站运行助手怎么建网页
  • 惠州有没有做网站西安疫情最新消息
  • 临海手机网站设计天天广告联盟
  • 可以随意做配搭的网站seo优化方案模板
  • 外贸网站建设 深圳网站建设公司网站
  • 花都网站建设公司网络推广学校
  • 新网站优化怎么做百度seo价格
  • 做服装设计兼职的网站口碑营销的好处
  • 婚纱网站模板免费下载深圳aso优化
  • 做优化网站是什么意思营销推广工作内容
  • 做啥网站赚钱?备案查询官网