当前位置:网站首页>【leetcode】1719. Number of schemes for reconstructing a tree
【leetcode】1719. Number of schemes for reconstructing a tree
2022-06-29 00:26:00 【Chinese fir sauce_】
subject :
1719. The number of schemes to reconstruct a tree
Give you an array pairs , among pairs[i] = [xi, yi] , And satisfy :
pairs There is no repeating element
xi < yi
Make ways The number of schemes with roots satisfying the following conditions :
All node values contained in the tree are in pairs in .
A couple [xi, yi] Appear in the pairs in If and only if xi yes yi Ancestors or yi yes xi The ancestors of the .
Be careful : The constructed tree is not necessarily a binary tree .
Two trees are treated as different schemes when there is at least one node and there are different parent nodes in the two trees .
Please return :
If ways == 0 , return 0 .
If ways == 1 , return 1 .
If ways > 1 , return 2 .
A tree Rooting tree It refers to a tree with only one root node , All edges are from the root to the outside .
We call any node on the path from the root to a node ( Remove the node itself ) Are all of this node The ancestors . The root node has no ancestors .
Example 1:
Input :pairs = [[1,2],[2,3]]
Output :1
explain : As shown in the figure above , There is and only one qualified rooted tree .
Example 2:
Input :pairs = [[1,2],[2,3],[1,3]]
Output :2
explain : There are multiple qualified rooted trees , Three of them are shown in the figure above .
Example 3:
Input :pairs = [[1,2],[2,3],[2,4],[1,5]]
Output :0
explain : There is no root tree that meets the regulations .
Tips :
1 <= pairs.length <= 105
1 <= xi < yi <= 500
pairs The elements in are different from each other .
class Solution {
public int checkWays(int[][] pairs) {
Set<Integer>[] path=new Set[505];// Record which other points each point has a grandson relationship with
for(int i=0;i<path.length;i++){
path[i]=new HashSet<>();}
int numOfNodes=0;
int mostRelations=0;
for(int i=0;i<pairs.length;i++){
if(path[pairs[i][0]].size()==0){
numOfNodes++;}
if(path[pairs[i][1]].size()==0){
numOfNodes++;}
path[pairs[i][0]].add(pairs[i][1]);
path[pairs[i][1]].add(pairs[i][0]);
mostRelations=Math.max(mostRelations,Math.max(path[pairs[i][0]].size(),path[pairs[i][1]].size()));
}
if(numOfNodes!=mostRelations+1){
return 0;}
int rootVal=-1;// Total root nodes
int ans=1;
for(int i=1;;i++){
if(path[i].size()==mostRelations){
rootVal=i;
break;
}
}
for(int p:path[rootVal]){
//p yes root A descendant of , Let's start looking for p Parent node
int dad=-1,partnerNum=505;
for(int partner:path[p]){
if(rootVal==p){
continue;}
if(partnerNum>path[partner].size()&&path[partner].size()>=path[p].size()){
dad=partner;
partnerNum=path[partner].size();
}
}
if(partnerNum==path[p].size()){
ans=2;}
for(int a:path[p]){
if(a==dad){
continue;}
if(!path[dad].contains(a)){
return 0;}
}
}
return ans;
}
}
边栏推荐
- 随笔记:插入排序 --from wcc
- Typescript -- Section 6 generic
- Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
- Daily question 1: remove elements
- oracle 去掉html标签
- Remove HTML tags from Oracle
- Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
- How to carry token authentication in websocket JS connection
- MapReduce case
- 手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
猜你喜欢

12.物体检测Mask-Rcnn

Pinhole camera with added lens

Realization of beauty system with MATLAB

Leetcode daily question: implementing strstr()

旋轉接頭安裝使用注意事項
![[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter](/img/7b/f9cea5dfe6831f5f226b907e2095eb.jpg)
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
![[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)](/img/5d/bee9a6e50384a7e05e9a71e9fbed9a.jpg)
[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)

运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统

随笔记:插入排序 --from wcc

光纤滑环价格过高的原因
随机推荐
这玩意叫跳表?
利用verilogA模块采样
TypeScript --第三节:接口
[gym 102423]-elven efficiency | thinking
[buuctf.reverse] 131-135
With notes: insert sort --from WCC
WPF implementation calls local camera~
每日一题:数组中数字出现的次数2
[leetcode] 522. 最长特殊序列 II 暴力 + 双指针
Give you a project, how will you carry out performance testing (I)
Daily question 1: missing numbers
Notes: three ways to define setters and Getters
12.物体检测Mask-Rcnn
Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
Precautions for installation and use of rotary joint
11.目标分割
Single machine multi instance MySQL master-slave replication
EditText监听焦点
Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
Technology sharing | software development process that you must understand if you want to get started with testing