Skip to content

Commit cb0d674

Browse files
committed
graph problem
1 parent 779aaac commit cb0d674

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: 323. Number of Connected Components.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
3+
void dfs(int key, unordered_map<int, vector<int> > &relation, unordered_set<int> &visited) {
4+
visited.insert(key);
5+
for(auto it = relation[key].begin(); it!= relation[key].end(); it++) {
6+
if(visited.find(*it) == visited.end())
7+
dfs(*it, relation, visited);
8+
}
9+
}
10+
11+
public:
12+
int countComponents(int n, vector<pair<int, int>>& edges) {
13+
14+
unordered_map<int, vector<int> > relation;
15+
unordered_set<int> visited;
16+
17+
for(auto i: edges) {
18+
relation[i.first].push_back(i.second);
19+
relation[i.second].push_back(i.first);
20+
}
21+
22+
int count = 0;
23+
24+
for(int i = 0;i<n;i++) {
25+
if(visited.find(i) == visited.end()) {
26+
count++;
27+
dfs(i, relation, visited);
28+
}
29+
}
30+
31+
return count;
32+
33+
34+
}
35+
};

Diff for: Non-leetcode/Implement your own sizeof.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
2+
int main()
3+
{
4+
double x;
5+
printf("%d", my_sizeof(x));
6+
getchar();
7+
return 0;
8+
}

0 commit comments

Comments
 (0)