Skip to content

Conversation

if (left > right) {
swap(left, right);
}
nodes[right] = left;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほとんど Union Find ですが、ここのところで、nodes のグラフの単純化が行われていないので、結果的に計算時間はかかるかもしれませんね。ここで、元の edge.front(), edge.back() から上流の node に対して、nodes[node] = left にすれば、Union Find を展開したものになっていると思います。

Space : O(n)

step1に対してセルフレビューで修正を加えた
- 各ノードを参照する事に階層を引き上げる処理を加えた。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://discord.com/channels/1084280443945353267/1183683738635346001/1197738650998415500
高速化の手法はいくつかあるみたいですが、これでも十分ですね。

public:
int countComponents(int n, vector<vector<int>>& edges) {
vector<vector<int>> adjacency_nodes = vector<vector<int>>(n, vector<int>());
vector<char> visited = vector<char>(n, '0');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には、0, 1 を使いたいです。'1' ではなくて。

@@ -0,0 +1,39 @@
class Solution {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とても良いと思います。

@colorbox colorbox merged commit 7172def into main Mar 9, 2025
@colorbox colorbox deleted the 323 branch March 9, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants