-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPossible.cpp
More file actions
37 lines (37 loc) · 1.23 KB
/
isPossible.cpp
File metadata and controls
37 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
bool isPossible(int n, vector<vector<int>>& edges) {
vector<unordered_set<int> > graph(n + 1);
for (auto &v: edges) {
graph[v[0]].insert(v[1]);
graph[v[1]].insert(v[0]);
}
vector<int> odds;
for (int i = 1; i <= n; ++i) {
if (graph[i].size() & 1) {
odds.push_back(i);
}
}
if (odds.size() == 0) return true;
if (odds.size() == 2) {
int a = odds[0], b = odds[1];
if (graph[a].find(b) == graph[a].end()) return true;
for (int i = 1; i <= n; ++i) {
if (i != a && i != b && graph[i].find(a) == graph[i].end() && graph[i].find(b) == graph[i].end()) return true;
}
return false;
}
if (odds.size() == 4) {
bool temp[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 4; ++j) {
temp[i][j] = graph[odds[i]].find(odds[j]) == graph[odds[i]].end();
}
}
return (temp[0][1] && temp[2][3]) ||
(temp[0][2] && temp[1][3]) ||
(temp[0][3] && temp[1][2]);
}
return false;
}
};