Skip to content

Commit a24af3b

Browse files
committed
feat: add solutions to lc problem: No.2508
No.2508.Add Edges to Make Degrees of All Nodes Even
1 parent 512ddf7 commit a24af3b

File tree

7 files changed

+483
-9
lines changed

7 files changed

+483
-9
lines changed

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
**方法一:二分查找**
7272

73-
题目可以转换为:对某个开销值,看它能不能在 maxOperations 次操作内得到。二分枚举开销值,找到最小的开销值即可
73+
我们可以将题目可以转换为:对某个开销值,看它能不能在 maxOperations 次操作内得到。因此,二分枚举开销值,找到最小的且满足条件的开销值即可
7474

7575
时间复杂度 $O(n \times \log M)$。其中 $n$ 和 $M$ 分别为数组 `nums` 的长度和最大值。
7676

solution/2500-2599/2508.Add Edges to Make Degrees of All Nodes Even/README.md

Lines changed: 170 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,200 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:分类讨论**
66+
67+
我们先通过 `edges` 构建图 $g$,然后找出所有度数为奇数的点,记为 $vs$。
68+
69+
如果 $vs$ 的长度为 $0$,说明图 $g$ 中所有点的度数都是偶数,直接返回 `true` 即可。
70+
71+
如果 $vs$ 的长度为 $2$,说明图 $g$ 中有两个点的度数是奇数。如果我们可以直接用一条边连接这两个点,使得图 $g$ 中所有点的度数都是偶数,返回 `true`;否则,如果我们能找到第三个点 $c$,使得我们能够连接 $a$ 和 $c$,以及连接 $b$ 和 $c$,使得图 $g$ 中所有点的度数都是偶数,返回 `true`;否则,返回 `false`
72+
73+
如果 $vs$ 的长度为 $4$,我们枚举两两组合的所有情况,判断是否存在满足条件的情况,是则返回 `true`;否则,返回 `false`
74+
75+
其它情况,返回 `false`
76+
77+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别为节点的数量和边的数量。
78+
6579
<!-- tabs:start -->
6680

6781
### **Python3**
6882

6983
<!-- 这里可写当前语言的特殊实现逻辑 -->
7084

7185
```python
72-
86+
class Solution:
87+
def isPossible(self, n: int, edges: List[List[int]]) -> bool:
88+
g = defaultdict(set)
89+
for a, b in edges:
90+
g[a].add(b)
91+
g[b].add(a)
92+
vs = [i for i, v in g.items() if len(v) & 1]
93+
if len(vs) == 0:
94+
return True
95+
if len(vs) == 2:
96+
a, b = vs
97+
if a not in g[b]:
98+
return True
99+
return any(a not in g[c] and c not in g[b] for c in range(1, n + 1))
100+
if len(vs) == 4:
101+
a, b, c, d = vs
102+
if a not in g[b] and c not in g[d]:
103+
return True
104+
if a not in g[c] and b not in g[d]:
105+
return True
106+
if a not in g[d] and b not in g[c]:
107+
return True
108+
return False
109+
return False
73110
```
74111

75112
### **Java**
76113

77114
<!-- 这里可写当前语言的特殊实现逻辑 -->
78115

79116
```java
80-
117+
class Solution {
118+
public boolean isPossible(int n, List<List<Integer>> edges) {
119+
Set<Integer>[] g = new Set[n + 1];
120+
Arrays.setAll(g, k -> new HashSet<>());
121+
for (var e : edges) {
122+
int a = e.get(0), b = e.get(1);
123+
g[a].add(b);
124+
g[b].add(a);
125+
}
126+
List<Integer> vs = new ArrayList<>();
127+
for (int i = 1; i <= n; ++i) {
128+
if (g[i].size() % 2 == 1) {
129+
vs.add(i);
130+
}
131+
}
132+
if (vs.size() == 0) {
133+
return true;
134+
}
135+
if (vs.size() == 2) {
136+
int a = vs.get(0), b = vs.get(1);
137+
if (!g[a].contains(b)) {
138+
return true;
139+
}
140+
for (int c = 1; c <= n; ++c) {
141+
if (a != c && b != c && !g[a].contains(c) && !g[c].contains(b)) {
142+
return true;
143+
}
144+
}
145+
return false;
146+
}
147+
if (vs.size() == 4) {
148+
int a = vs.get(0), b = vs.get(1), c = vs.get(2), d = vs.get(3);
149+
if (!g[a].contains(b) && !g[c].contains(d)) {
150+
return true;
151+
}
152+
if (!g[a].contains(c) && !g[b].contains(d)) {
153+
return true;
154+
}
155+
if (!g[a].contains(d) && !g[b].contains(c)) {
156+
return true;
157+
}
158+
return false;
159+
}
160+
return false;
161+
}
162+
}
81163
```
82164

83165
### **C++**
84166

85167
```cpp
86-
168+
class Solution {
169+
public:
170+
bool isPossible(int n, vector<vector<int>>& edges) {
171+
vector<unordered_set<int>> g(n + 1);
172+
for (auto& e : edges) {
173+
int a = e[0], b = e[1];
174+
g[a].insert(b);
175+
g[b].insert(a);
176+
}
177+
vector<int> vs;
178+
for (int i = 1; i <= n; ++i) {
179+
if (g[i].size() % 2) {
180+
vs.emplace_back(i);
181+
}
182+
}
183+
if (vs.size() == 0) {
184+
return true;
185+
}
186+
if (vs.size() == 2) {
187+
int a = vs[0], b = vs[1];
188+
if (!g[a].count(b)) return true;
189+
for (int c = 1; c <= n; ++c) {
190+
if (a != b && b != c && !g[a].count(c) && !g[c].count(b)) {
191+
return true;
192+
}
193+
}
194+
return false;
195+
}
196+
if (vs.size() == 4) {
197+
int a = vs[0], b = vs[1], c = vs[2], d = vs[3];
198+
if (!g[a].count(b) && !g[c].count(d)) return true;
199+
if (!g[a].count(c) && !g[b].count(d)) return true;
200+
if (!g[a].count(d) && !g[b].count(c)) return true;
201+
return false;
202+
}
203+
return false;
204+
}
205+
};
87206
```
88207
89208
### **Go**
90209
91210
```go
92-
211+
func isPossible(n int, edges [][]int) bool {
212+
g := make([]map[int]bool, n+1)
213+
for _, e := range edges {
214+
a, b := e[0], e[1]
215+
if g[a] == nil {
216+
g[a] = map[int]bool{}
217+
}
218+
if g[b] == nil {
219+
g[b] = map[int]bool{}
220+
}
221+
g[a][b], g[b][a] = true, true
222+
}
223+
vs := []int{}
224+
for i := 1; i <= n; i++ {
225+
if len(g[i])%2 == 1 {
226+
vs = append(vs, i)
227+
}
228+
}
229+
if len(vs) == 0 {
230+
return true
231+
}
232+
if len(vs) == 2 {
233+
a, b := vs[0], vs[1]
234+
if !g[a][b] {
235+
return true
236+
}
237+
for c := 1; c <= n; c++ {
238+
if a != c && b != c && !g[a][c] && !g[c][b] {
239+
return true
240+
}
241+
}
242+
return false
243+
}
244+
if len(vs) == 4 {
245+
a, b, c, d := vs[0], vs[1], vs[2], vs[3]
246+
if !g[a][b] && !g[c][d] {
247+
return true
248+
}
249+
if !g[a][c] && !g[b][d] {
250+
return true
251+
}
252+
if !g[a][d] && !g[b][c] {
253+
return true
254+
}
255+
return false
256+
}
257+
return false
258+
}
93259
```
94260

95261
### **...**

solution/2500-2599/2508.Add Edges to Make Degrees of All Nodes Even/README_EN.md

Lines changed: 156 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,177 @@ Every node in the resulting graph is connected to an even number of edges.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def isPossible(self, n: int, edges: List[List[int]]) -> bool:
60+
g = defaultdict(set)
61+
for a, b in edges:
62+
g[a].add(b)
63+
g[b].add(a)
64+
vs = [i for i, v in g.items() if len(v) & 1]
65+
if len(vs) == 0:
66+
return True
67+
if len(vs) == 2:
68+
a, b = vs
69+
if a not in g[b]:
70+
return True
71+
return any(a not in g[c] and c not in g[b] for c in range(1, n + 1))
72+
if len(vs) == 4:
73+
a, b, c, d = vs
74+
if a not in g[b] and c not in g[d]:
75+
return True
76+
if a not in g[c] and b not in g[d]:
77+
return True
78+
if a not in g[d] and b not in g[c]:
79+
return True
80+
return False
81+
return False
5982
```
6083

6184
### **Java**
6285

6386
```java
64-
87+
class Solution {
88+
public boolean isPossible(int n, List<List<Integer>> edges) {
89+
Set<Integer>[] g = new Set[n + 1];
90+
Arrays.setAll(g, k -> new HashSet<>());
91+
for (var e : edges) {
92+
int a = e.get(0), b = e.get(1);
93+
g[a].add(b);
94+
g[b].add(a);
95+
}
96+
List<Integer> vs = new ArrayList<>();
97+
for (int i = 1; i <= n; ++i) {
98+
if (g[i].size() % 2 == 1) {
99+
vs.add(i);
100+
}
101+
}
102+
if (vs.size() == 0) {
103+
return true;
104+
}
105+
if (vs.size() == 2) {
106+
int a = vs.get(0), b = vs.get(1);
107+
if (!g[a].contains(b)) {
108+
return true;
109+
}
110+
for (int c = 1; c <= n; ++c) {
111+
if (a != c && b != c && !g[a].contains(c) && !g[c].contains(b)) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
if (vs.size() == 4) {
118+
int a = vs.get(0), b = vs.get(1), c = vs.get(2), d = vs.get(3);
119+
if (!g[a].contains(b) && !g[c].contains(d)) {
120+
return true;
121+
}
122+
if (!g[a].contains(c) && !g[b].contains(d)) {
123+
return true;
124+
}
125+
if (!g[a].contains(d) && !g[b].contains(c)) {
126+
return true;
127+
}
128+
return false;
129+
}
130+
return false;
131+
}
132+
}
65133
```
66134

67135
### **C++**
68136

69137
```cpp
70-
138+
class Solution {
139+
public:
140+
bool isPossible(int n, vector<vector<int>>& edges) {
141+
vector<unordered_set<int>> g(n + 1);
142+
for (auto& e : edges) {
143+
int a = e[0], b = e[1];
144+
g[a].insert(b);
145+
g[b].insert(a);
146+
}
147+
vector<int> vs;
148+
for (int i = 1; i <= n; ++i) {
149+
if (g[i].size() % 2) {
150+
vs.emplace_back(i);
151+
}
152+
}
153+
if (vs.size() == 0) {
154+
return true;
155+
}
156+
if (vs.size() == 2) {
157+
int a = vs[0], b = vs[1];
158+
if (!g[a].count(b)) return true;
159+
for (int c = 1; c <= n; ++c) {
160+
if (a != b && b != c && !g[a].count(c) && !g[c].count(b)) {
161+
return true;
162+
}
163+
}
164+
return false;
165+
}
166+
if (vs.size() == 4) {
167+
int a = vs[0], b = vs[1], c = vs[2], d = vs[3];
168+
if (!g[a].count(b) && !g[c].count(d)) return true;
169+
if (!g[a].count(c) && !g[b].count(d)) return true;
170+
if (!g[a].count(d) && !g[b].count(c)) return true;
171+
return false;
172+
}
173+
return false;
174+
}
175+
};
71176
```
72177
73178
### **Go**
74179
75180
```go
76-
181+
func isPossible(n int, edges [][]int) bool {
182+
g := make([]map[int]bool, n+1)
183+
for _, e := range edges {
184+
a, b := e[0], e[1]
185+
if g[a] == nil {
186+
g[a] = map[int]bool{}
187+
}
188+
if g[b] == nil {
189+
g[b] = map[int]bool{}
190+
}
191+
g[a][b], g[b][a] = true, true
192+
}
193+
vs := []int{}
194+
for i := 1; i <= n; i++ {
195+
if len(g[i])%2 == 1 {
196+
vs = append(vs, i)
197+
}
198+
}
199+
if len(vs) == 0 {
200+
return true
201+
}
202+
if len(vs) == 2 {
203+
a, b := vs[0], vs[1]
204+
if !g[a][b] {
205+
return true
206+
}
207+
for c := 1; c <= n; c++ {
208+
if a != c && b != c && !g[a][c] && !g[c][b] {
209+
return true
210+
}
211+
}
212+
return false
213+
}
214+
if len(vs) == 4 {
215+
a, b, c, d := vs[0], vs[1], vs[2], vs[3]
216+
if !g[a][b] && !g[c][d] {
217+
return true
218+
}
219+
if !g[a][c] && !g[b][d] {
220+
return true
221+
}
222+
if !g[a][d] && !g[b][c] {
223+
return true
224+
}
225+
return false
226+
}
227+
return false
228+
}
77229
```
78230

79231
### **...**

0 commit comments

Comments
 (0)