|
1 | 1 | class Solution { |
2 | 2 | int flag = 1; |
3 | | - unordered_map<int, vector<int>>children; |
4 | | - unordered_map<int, int>father; |
5 | | - unordered_map<int, int>depth; |
6 | | - unordered_map<int, int>childrenCount; |
| 3 | + unordered_set<int>nodeSet; |
| 4 | + vector<int> relative[501]; |
| 5 | + int isRelative[501][501]; |
| 6 | + vector<int> children[501]; |
| 7 | + unordered_set<int>visited; |
7 | 8 |
|
8 | 9 | public: |
9 | 10 | int checkWays(vector<vector<int>>& pairs) |
10 | 11 | { |
11 | | - unordered_map<int,int>order; |
12 | | - unordered_set<int>nodeSet; |
13 | 12 | for (auto pair: pairs) |
14 | 13 | { |
15 | 14 | nodeSet.insert(pair[0]); |
16 | | - nodeSet.insert(pair[1]); |
17 | | - order[pair[0]]++; |
18 | | - order[pair[1]]++; |
19 | | - } |
20 | | - unordered_map<int,vector<pair<int,int>>>relative; |
21 | | - for (auto pair: pairs) |
22 | | - { |
23 | | - relative[pair[0]].push_back({order[pair[1]], pair[1]}); |
24 | | - relative[pair[1]].push_back({order[pair[0]], pair[0]}); |
25 | | - } |
26 | | - |
27 | | - for (auto node: nodeSet) |
28 | | - { |
29 | | - sort(relative[node].begin(), relative[node].end()); |
30 | | - int i = relative[node].size()-1; |
31 | | - while (i>=0 && (relative[node][i].first > order[node] || relative[node][i].first == order[node] && relative[node][i].second > node)) |
32 | | - { |
33 | | - if (relative[node][i].first == order[node] && flag == 1) |
34 | | - flag = 2; |
35 | | - i--; |
36 | | - } |
37 | | - if (i+1>=0 && i+1 < relative[node].size()) |
38 | | - { |
39 | | - int parent = relative[node][i+1].second; |
40 | | - children[parent].push_back(node); |
41 | | - father[node] = parent; |
42 | | - } |
| 15 | + nodeSet.insert(pair[1]); |
| 16 | + relative[pair[0]].push_back(pair[1]); |
| 17 | + relative[pair[1]].push_back(pair[0]); |
| 18 | + isRelative[pair[0]][pair[1]] = 1; |
| 19 | + isRelative[pair[1]][pair[0]] = 1; |
43 | 20 | } |
44 | 21 |
|
| 22 | + vector<int>nodes(nodeSet.begin(), nodeSet.end()); |
| 23 | + sort(nodes.begin(),nodes.end(),[&](int x,int y)->bool{return relative[x].size()<relative[y].size();}); |
| 24 | + |
45 | 25 | int root = -1; |
46 | | - for (auto node: nodeSet) |
| 26 | + |
| 27 | + for (int i=0; i<nodes.size(); i++) |
47 | 28 | { |
48 | | - if (father.find(node)==father.end()) |
49 | | - { |
50 | | - if (root!=-1) return 0; |
51 | | - root = node; |
| 29 | + int j = i+1; |
| 30 | + while (j<nodes.size() && !isRelative[nodes[i]][nodes[j]]) |
| 31 | + j++; |
| 32 | + if (j<nodes.size()) |
| 33 | + { |
| 34 | + children[nodes[j]].push_back(nodes[i]); |
| 35 | + if (relative[nodes[j]].size() == relative[nodes[i]].size()) |
| 36 | + { |
| 37 | + flag = 2; |
| 38 | + } |
52 | 39 | } |
| 40 | + else |
| 41 | + { |
| 42 | + if (root==-1) |
| 43 | + root = nodes[i]; |
| 44 | + else |
| 45 | + return 0; |
| 46 | + } |
53 | 47 | } |
54 | 48 |
|
55 | | - unordered_set<int>visited; |
56 | | - if (dfs(root, visited, 0)==-1) |
57 | | - return 0; |
58 | | - |
59 | | - for (auto node: nodeSet) |
60 | | - { |
61 | | - if (order[node]!=depth[node]+childrenCount[node]) |
62 | | - return 0; |
63 | | - } |
| 49 | + dfs(root,0); |
64 | 50 |
|
65 | 51 | return flag; |
66 | 52 | } |
67 | 53 |
|
68 | | - int dfs(int cur, unordered_set<int>&visited, int d) |
| 54 | + int dfs(int cur, int depth) |
69 | 55 | { |
| 56 | + if (flag==0) return -1; |
70 | 57 | if (visited.find(cur)!=visited.end()) |
71 | | - return -1; |
| 58 | + { |
| 59 | + flag = 0; |
| 60 | + return -1; |
| 61 | + } |
72 | 62 | visited.insert(cur); |
73 | | - depth[cur] = d; |
74 | | - |
75 | | - int count = 0; |
76 | | - for (auto next: children[cur]) |
| 63 | + int sum = 0; |
| 64 | + for (int child: children[cur]) |
77 | 65 | { |
78 | | - int temp = dfs(next, visited, d+1); |
79 | | - if (temp==-1) |
80 | | - return -1; |
81 | | - else |
82 | | - count+=temp; |
| 66 | + sum += dfs(child, depth+1); |
| 67 | + } |
| 68 | + if (sum+depth!=relative[cur].size()) |
| 69 | + { |
| 70 | + flag = 0; |
| 71 | + return -1; |
83 | 72 | } |
84 | | - childrenCount[cur] = count; |
85 | | - return count+1; |
| 73 | + return sum+1; |
86 | 74 | } |
87 | 75 | }; |
0 commit comments