Skip to content

Commit 705845c

Browse files
committed
daily
1 parent f23c6cd commit 705845c

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

my-submissions/h2493.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Notes
2+
3+
- Convert edges into a two way mapping via a defaultdict
4+
- Track all visited nodes
5+
- Parse from a random starting node
6+
- Repeat from a random non visited node in case it's a disconnected graph
7+
8+
- Groupings are partially based on cycles that exist
9+
- E.g. in example 1, there's a cycle that's 1-2-6-4-1
10+
- These maximize the grouping spread of those nodes in particular,
11+
limiting it to cycle_size / 2
12+
- If a cycle is odd, it's impossible
13+
14+
Idea:
15+
16+
- Is the answer just the longest independent line ignoring cycles
17+
- Returning -1 no answer if there's an odd cycle
18+
19+
Update: 49/55 test cases
20+
21+
- My worry was true, cycles won't always end up being the longest independent line.
22+
- I need to calculate the lengths of each vistigial branch out from a cycle, and find which of the two ways
23+
around the cycle is smaller since the other branch will just "fold up"
24+
25+
Plan:
26+
27+
- I have a feeling that brute forcing the farthest distance from each individual point is pbly
28+
within AC range for the constraints... As much as I want to deny it and not want to implement it
29+
since I've spent so long working on a more efficient Solution
30+
31+
... I hate that I was right...

my-submissions/h2493.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Solution:
2+
# output: (dist, node_no)
3+
def furthest_vertex(self, curr_node: int, edges: DefaultDict[int, Set[int]], visited: Set[int] = None) -> Tuple[int, int] :
4+
if not visited :
5+
visited = set()
6+
nxt_to_visit = deque([(1, curr_node)])
7+
farthest = (-1, -1)
8+
while nxt_to_visit :
9+
dist, curr = nxt_to_visit.popleft()
10+
11+
if curr in visited :
12+
continue
13+
visited.add(curr)
14+
15+
if dist > farthest[0] :
16+
farthest = (dist, curr)
17+
18+
for nxt in edges[curr] :
19+
if nxt in visited :
20+
continue
21+
nxt_to_visit.append((dist + 1, nxt))
22+
23+
return farthest
24+
25+
def odd_cycle_len(self,
26+
curr_node: int,
27+
traversal_no: int,
28+
edges: DefaultDict[int, Set[int]],
29+
visited: Dict[int, int] = None) -> bool :
30+
if curr_node in visited :
31+
return (traversal_no - visited[curr_node]) % 2 != 0
32+
33+
visited[curr_node] = traversal_no
34+
35+
return any(self.odd_cycle_len(x, traversal_no + 1, edges, visited) for x in edges[curr_node])
36+
37+
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
38+
# Edge list from each vertex
39+
e = defaultdict(set)
40+
for u, v in edges :
41+
e[u].add(v)
42+
e[v].add(u)
43+
44+
groups = 0
45+
46+
to_visit = set(range(1, n + 1))
47+
while to_visit :
48+
curr = to_visit.pop()
49+
50+
visited = {}
51+
if self.odd_cycle_len(curr, 1, e, visited) :
52+
return -1
53+
54+
to_visit -= visited.keys()
55+
groups += max([self.furthest_vertex(x, e) for x in visited.keys()])[0]
56+
57+
return groups

0 commit comments

Comments
 (0)