上海哪个网站能应聘做家教的,根据网站开发app,wordpress虚拟主机加速,百度推广的优势题目描述
现有一棵由 n 个节点组成的无向树#xff0c;节点编号从 0 到 n - 1 #xff0c;共有 n - 1 条边。
给你一个二维整数数组 edges #xff0c;长度为 n - 1 #xff0c;其中 edges[i] [ai, bi] 表示树中节点 ai 和 bi 之间存在一条边。另给你一个整数数组 restr…题目描述
现有一棵由 n 个节点组成的无向树节点编号从 0 到 n - 1 共有 n - 1 条边。
给你一个二维整数数组 edges 长度为 n - 1 其中 edges[i] [ai, bi] 表示树中节点 ai 和 bi 之间存在一条边。另给你一个整数数组 restricted 表示 受限 节点。
在不访问受限节点的前提下返回你可以从节点 0 到达的 最多 节点数目。
注意节点 0 不 会标记为受限节点。
示例 1 输入n 7, edges [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted [4,5]
输出4
解释上图所示正是这棵树。
在不访问受限节点的前提下只有节点 [0,1,2,3] 可以从节点 0 到达。
示例 2 输入n 7, edges [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted [4,2,1]
输出3
解释上图所示正是这棵树。
在不访问受限节点的前提下只有节点 [0,5,6] 可以从节点 0 到达。
解题思路
本题并不难解题主要是抓住题意因为受限节点不可以访问所以我们可以直接将受限节点涉及到的边直接排除在外而后在验证节点是否受限时如果一个个查显然时间复杂度过高这时我们可以使用Set减少查询的时间复杂度。而后进行一次dfs就可以了而后我们还需要知道因为这是一棵树所以节点不会重复访问所以我们直接即可。
代码如下
class Solution {int cnt0;public int reachableNodes(int n, int[][] edges, int[] restricted) {SetInteger setnew HashSetInteger();ListInteger lists[]new ArrayList[n];for(int i:restricted)//存入setset.add(i);for(int i0;in;i)lists[i]new ArrayList();for(int i0;in-1;i){int xedges[i][0];int yedges[i][1];if(set.contains(x)||set.contains(y))//不进行边加入continue;lists[x].add(y);lists[y].add(x);}boolean flag[]new boolean[n];flag[0]true;dfs(0,lists,flag);return cnt;}public void dfs(int p,ListInteger lists[],boolean flag[]){cnt;//不会重复直接ListInteger listlists[p];for(int i0;ilist.size();i){int llist.get(i);if(!flag[l]){flag[l]true;dfs(l,lists,flag);flag[l]false;}}}
}
文章转载自: http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.mkygc.cn.gov.cn.mkygc.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.snmth.cn.gov.cn.snmth.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.cfpq.cn.gov.cn.cfpq.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn