当前位置:网站首页>【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;
}
}
边栏推荐
- Install MySQL on Windows platform (with Navicat premium 12 "using" tutorial)
- 12. Détection d'objets Mask rcnn
- Technology sharing | software development process that you must understand if you want to get started with testing
- FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
- 手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
- Click hijack: X-FRAME-OPTIONS is not configured
- ES6:let、const、箭头函数
- Leetcode 178 Score ranking (June 27, 2022)
- [image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code
- TypeScript --第三节:接口
猜你喜欢

Xiaobai's e-commerce business is very important to choose the right mall system!

Software testing tools: complete and precise

LinkedIn datahub - experience sharing

Go1.18 new feature: discard strings Title Method, a new pit!

10、YOLO系列

12. Détection d'objets Mask rcnn
![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)
[buuctf.reverse] 131-135

Program environment and pretreatment

Structure of the actual combat battalion | module 5

Leetcode daily question: implementing strstr()
随机推荐
MNIST handwritten numeral recognition demo based on pytorch framework
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
var、let、const 三者的区别
TypeScript--第四节:函数
SCP copy folder
Is pension insurance a financial product? Where is the expected return?
6.28 learning content
oracle 去掉html标签
[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code
HandlerThread使用及原理
Easy to use free ppt template
TypeScript--第五节:类
TypeScript -- 第二节:变量声明
MySQL 8.0 above reporting 2058 solution
Résumé de Manon, 25 ans, diplômé en trois ans
这玩意叫跳表?
每日一题:数组中数字出现的次数2
手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
滑环的基本结构及工作原理分析
架构实战营|模块5