当前位置:网站首页>[leetcode daily question] number of schemes to reconstruct a tree
[leetcode daily question] number of schemes to reconstruct a tree
2022-06-29 03:03:00 【Oysters bring the sea to Chicago】
The number of schemes to reconstruct a tree
difficulty : difficult 


The most difficult daily question to date , I can't understand the solution ,cv Go to sleep ~
The code is as follows :
int N = 510;
int[] cnts = new int[N], fa = new int[N];
boolean[][] g = new boolean[N][N];
public int checkWays(int[][] pairs) {
int m = pairs.length;
Set<Integer> set = new HashSet<>();
for (int[] p : pairs) {
int a = p[0], b = p[1];
g[a][b] = g[b][a] = true;
cnts[a]++; cnts[b]++;
set.add(a); set.add(b);
}
List<Integer> list = new ArrayList<>(set);
Collections.sort(list, (a,b)->cnts[b]-cnts[a]);
int n = list.size(), root = list.get(0);
if (m < n - 1) return 0; // The forest
fa[root] = -1;
for (int i = 1; i < n; i++) {
int a = list.get(i);
boolean ok = false;
for (int j = i - 1; j >= 0 && !ok; j--) {
int b = list.get(j);
if (g[a][b]) {
fa[a] = b;
ok = true;
}
}
if (!ok) return 0;
}
int c = 0, ans = 1;
for (int i : set) {
int j = i;
while (fa[j] != -1) {
if (!g[i][fa[j]]) return 0;
if (cnts[i] == cnts[fa[j]]) ans = 2;
c++;
j = fa[j];
}
}
return c < m ? 0 : ans;
Execution results : success 
边栏推荐
- PHP XML expat parser
- Map and set use pari as the key value. How to define
- 測試入門——集成測試
- Three methods of time series prediction: statistical model, machine learning and recurrent neural network
- Redis master-slave replication
- Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
- Démarrer le test - test d'intégration
- 99 multiplication table
- 逆序对对数计算,顺序对对数计算——归并排序
- set time format
猜你喜欢
![[Shangshui Shuo series] the simplest subtitle configuration](/img/22/7e0bcb489d0f2d35c7fe3248ad3419.png)
[Shangshui Shuo series] the simplest subtitle configuration

allegro 设计中显示网络飞线或关闭网络飞线的方法

Basic MySQL database operations

Nvisual helps integrators transform

How does sound amplify weak sounds

They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?

深入解析 Apache BookKeeper 系列:第三篇——读取原理

解决allegro中测量距离时,点击一个点后光标闪烁的问题

Wechat applet custom component

2022-2028 global MWIR camera industry research and trend analysis report
随机推荐
sql连续登录问题
Oracle Recovery Tools实战批量坏块修复
Is it safe for qiniu school to open an account in 2022?
After today, I look forward to the new year's eve of the year of the rabbit
2022-2028 global CAE engineering service industry research and trend analysis report
Data discretization
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
LinkedList学习
层次分析法(AHP)
Centos7 installation php7.2
2022-2028 global pneumatic test probe industry survey and trend analysis report
Equal wealth
信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
Informatics Olympiad 1361: Produce
PAT甲级 A1057 Stack
How to skip time when closing a socket connection_ Wait status of wait
Relationship between EMC, EMI and EMS
今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭
In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day