Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

323. Number of Connected Components in an Undirected Graph #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

とても良いと思います。

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.

2 participants