襄樊网站开发,用wordpress 安装自己喜欢的主题 主题图片显示不对,黄山购物网站建设,wordpress 商业2024.2.29 题目来源我的题解方法一 深度搜索#xff08;暴力#xff09; 超时方法二 树形动态规划 题目来源
力扣每日一题#xff1b;题序#xff1a;2581
我的题解
方法一 深度搜索#xff08;暴力#xff09; 超时 以每个节点node为跟进行深度搜索#xff0c;并在搜… 2024.2.29 题目来源我的题解方法一 深度搜索暴力 超时方法二 树形动态规划 题目来源
力扣每日一题题序2581
我的题解
方法一 深度搜索暴力 超时 以每个节点node为跟进行深度搜索并在搜索过程中记录前驱节点然后判断[前驱节点当前节点]是否在guesses中出现。若出现则表示Bob猜测对一次并记录在count数组中。最后遍历count数组看有多少满足count[i]k。该值就是可能成为树根的 节点数目 时间复杂度O( n ( n e ) n(ne) n(ne))。n表示树的节点数e表示树的边数 空间复杂度O(n) class Solution {//为了快速判断[u,v]对是否存在连接成字符串ListListString guessnew ArrayList();//记录以节点i为根用户猜对的次数当然由于在过程中进行了截断所以最大值为kint[] count;public int rootCount(int[][] edges, int[][] guesses, int k) {int nedges.length1;countnew int[n];ListInteger[] gcreateGraph(n,edges);for(int[] t:guesses){int u t[0];int v t[1];guess.add(u-v);}for(int i0;in;i){dfs(i,i,g,-1,k);}int res0;for(int i0;in;i){if(count[i]k)res;}return res;}//深度优先搜索public void dfs(int root,int cur,ListInteger[] g,int pre,int k){//根节点没有父节点if(pre!-1){String tpre-cur;//Bob猜测正确if(guess.contains(t))count[root];//截断当已经正确的次数达到k表明以root为根一定满足if(count[root]k)return;}for(int next:g[cur]){//防止循环遍历if(next!pre){dfs(root,next,g,cur,k);}}}//构建树public ListInteger[] createGraph(int n,int[][] edges){ListInteger[] gnew ArrayList[n];for(int i0;in;i){g[i]new ArrayList();}for(int[] t:edges){int fromt[0];int to t[1];g[from].add(to);g[to].add(from);}return g;}
}//优化版本但是还是超时
// 利用了如下的结论进行优化。
//基于已经计算出以 x 为树根时猜对的次数很容易就可以计算出以 y 为树根时猜对的次数
//如果 (x,y) 存在于 guesses猜对次数减一
//如果 (y,x) 存在于 guesses猜对次数加一。class Solution {ListString guessnew ArrayList();int cnt0;int res0;public int rootCount(int[][] edges, int[][] guesses, int k) {int nedges.length1;// countnew int[n];ListInteger[] gcreateGraph(n,edges);for(int[] t:guesses){int u t[0];int v t[1];guess.add(u-v);}dfs(0,0,g,-1,k);rdfs(g,0,-1,k,cnt);return res;}public void rdfs(ListInteger[] g,int x,int t,int k,int cnt){if(cntk){res;}for(int y:g[x]){if(ty)continue;rdfs(g,y,x,k,cnt-(guess.contains(x-y)?1:0)(guess.contains(y-x)?1:0));}}public void dfs(int root,int cur,ListInteger[] g,int pre,int k){if(pre!-1){String tpre-cur;if(guess.contains(t))cnt;}for(int next:g[cur]){if(next!pre){dfs(root,next,g,cur,k);}}}public ListInteger[] createGraph(int n,int[][] edges){ListInteger[] gnew ArrayList[n];for(int i0;in;i){g[i]new ArrayList();}for(int[] t:edges){int fromt[0];int to t[1];g[from].add(to);g[to].add(from);}return g;}
}
方法二 树形动态规划 看官方题解吧弄不明白 终于补完2月的每日一题了。
有任何问题欢迎评论区交流欢迎评论区提供其它解题思路代码也可以点个赞支持一下作者哈~ 文章转载自: http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.thjqk.cn.gov.cn.thjqk.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.ltpmy.cn.gov.cn.ltpmy.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn