-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
if (left > right) { | ||
swap(left, right); | ||
} | ||
nodes[right] = left; |
There was a problem hiding this comment.
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に対してセルフレビューで修正を加えた | ||
- 各ノードを参照する事に階層を引き上げる処理を加えた。 |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
とても良いと思います。
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/
https://www.lintcode.com/problem/3651/
https://discord.com/channels/1084280443945353267/1183683738635346001/1197738650998415500
sakupan102/arai60-practice#22
shining-ai/leetcode#19
hayashi-ay/leetcode#37
sakupan102/arai60-practice#22
kazukiii/leetcode#20
Yoshiki-Iwasa/Arai60#21
goto-untrapped/Arai60#35
Ryotaro25/leetcode_first60#20
tarinaihitori/leetcode#19