【
In this problem, the tree is a communication means and acyclic undirected graph.
Enter a map that consists of a with N nodes (the node value is not repeated 1, 2, ..., N) and one additional tree edge configuration. Two additional edges contained in the intermediate vertices 1 to N, this additional edges are not already present in the tree edge.
FIG result is a two-dimensional array in a side thereof. Each side element is a pair of [u, v], satisfying u <v, u and v denotes a vertex connected to the free edge of the FIG.
A return side can be omitted, so that the result is a Fig tree with N nodes. If there are multiple answers, while the last occurrence of two-dimensional array is returned. Answer edge [u, v] should satisfy the same format u <v.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: undirected graph given:
1
/ \
2--3
Example 2:
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: given undirected graph:
5 - 1 - 2
| |
4 - 3
Note:
The size of the input two-dimensional array of 3-1000.
Two-dimensional array of integers between 1 and N, where N is the size of the input array.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/redundant-connection
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
】
It is about a topic map.
In this title, they also learned new skills, disjoint-set, key in this one, while i = root [i] (root [i] = -1!);
?
int find(int *root, int i) {
while(root[i] != -1) {
i = root[i];
}
return i;
}
int* findRedundantConnection(int** edges, int edgesSize, int* edgesColSize, int* returnSize){
int root[8096];
int i;
int one;
int two;
int *ret;
ret = (int *)malloc(sizeof(int) * 2);
for(i = 0; i< 1000; i++) {
root[i] = -1;
}
for(i = 0; i < edgesSize; i++) {
one = find(root, edges[i][0]);
two = find(root, edges[i][1]);
if(one == two) {
ret[0] = edges[i][0];
ret[1] = edges[i][1];
break;
}
root[one] = two;
}
*returnSize = 2;
return ret;
}