当前位置:网站首页>【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;
}
}
边栏推荐
- 小白创业做电商,选对商城系统很重要!
- Encapsulation of JDBC connection and disconnection database
- Give you a project, how will you carry out performance testing (I)
- 11. target segmentation
- 手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
- Common mistakes in software testing
- 剑指 Offer 12. 矩阵中的路径
- Single machine multi instance MySQL master-slave replication
- PHP函数file_get_contents与操作系统的内存映射
- MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
猜你喜欢

手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??

Reprint: VTK notes - clipping and segmentation - irregular closed loop clipping -vtkselectpolydata class (black mountain old demon)

转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)

MySQL 8.0 above reporting 2058 solution

oracle 去掉html标签

Baidu online disk login verification prompt: unable to access this page, or the QR code display fails, the pop-up window shows: unable to access this page, ensure the web address....

12. Détection d'objets Mask rcnn

Sword finger offer 12 Path in matrix

Notes: three ways to define setters and Getters
JVM底层又是如何实现synchronized的
随机推荐
Program environment and pretreatment
How the slip ring motor works
Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
Single machine multi instance MySQL master-slave replication
旋转接头安装使用注意事项
6.28 学习内容
大智慧上开户是安全的吗
LG. Hankson's interesting questions, C language
[image registration] SAR image registration based on particle swarm optimization Improved SIFT with matlab code
单机多实例MYSQL主从复制
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
TypeScript--第五节:类
旋轉接頭安裝使用注意事項
JVM工作原理介绍
Encapsulation of JDBC connection and disconnection database
Daily question 1: the number of numbers in the array 2
手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
JVM底层又是如何实现synchronized的
转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)
每日一题: 数组中数字出现的次数