-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from SurbhiSharma15/master
Update README.md
- Loading branch information
Showing
4 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
struct DSU{ | ||
|
||
vector<int> parent ; | ||
vector<int> size ; | ||
int comp; | ||
|
||
DSU(int N){ | ||
parent.resize(N+1,0); | ||
size.resize(N+1,0); | ||
comp = N; | ||
for(int i=1;i<=N;i++){ | ||
make_set(i) ; | ||
} | ||
} | ||
void make_set(int v){ | ||
parent[v] = v ; | ||
size[v] = 1 ; | ||
} | ||
|
||
int find_set(int v){ | ||
if(parent[v] == v){ | ||
return v ; | ||
} | ||
return parent[v] = find_set(parent[v]); | ||
} | ||
void union_set(int a, int b){ | ||
a = find_set(a) ; | ||
b = find_set(b) ; | ||
|
||
if(a!=b){ | ||
if(size[a]> size[b]){ | ||
swap(a, b); | ||
} | ||
parent[a] = parent[b] ; | ||
size[b] += size[a]; | ||
comp--; | ||
} | ||
|
||
} | ||
|
||
int count_comp(){ | ||
return comp; | ||
} | ||
|
||
int size_set(int v){ | ||
return size[find_set(v)] ; | ||
} | ||
}; | ||
|
||
|
||
|
||
struct edge{ | ||
int u , v , w ; | ||
edge(int u , int v , int w ){ | ||
this->u = u ; | ||
this->v = v ; | ||
this->w = w ; | ||
} | ||
}; | ||
|
||
bool comp( edge &a , edge &b ){ | ||
return a.w < b.w ; | ||
} | ||
|
||
void solve(){ | ||
|
||
int n , m ; | ||
cin >> n >> m ; | ||
|
||
vector<edge> edg ; | ||
|
||
while(m--){ | ||
int u , v , w ; | ||
cin >> u >> v >> w ; | ||
edge newedge(u,v,w) ; | ||
edg.pb(newedge) ; | ||
} | ||
|
||
sort(all(edg) , comp ) ; | ||
|
||
int mst_sum = 0 ; | ||
|
||
DSU vec(n) ; | ||
|
||
for(auto x : edg ){ | ||
int u = x.u ; | ||
int v = x.v ; | ||
int w = x.w ; | ||
|
||
u = vec.find_set(u) ; | ||
v = vec.find_set(v) ; | ||
|
||
if(u!=v){ | ||
mst_sum += w ; | ||
vec.union_set(u,v) ; | ||
} | ||
} | ||
|
||
cout << mst_sum << endl ; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Solution { | ||
public: | ||
|
||
bool isleaf(Node *root){ | ||
if(root->left == NULL && root->right==NULL) return true ; | ||
return false ; | ||
} | ||
|
||
void leftboun(Node *root, vector<int> & ans){ | ||
Node * curr= root->left; | ||
while(curr){ | ||
if(!isleaf(curr)) ans.push_back(curr->data) ; | ||
if(curr->left) curr=curr->left ; | ||
else curr=curr->right ; | ||
} | ||
} | ||
void rightboun(Node *root, vector<int> & ans){ | ||
Node * curr= root->right ; | ||
vector<int> temp ; | ||
while(curr){ | ||
if(!isleaf(curr)) temp.push_back(curr->data) ; | ||
if(curr->right) curr=curr->right ; | ||
else curr= curr->left ; | ||
} | ||
for(int i= temp.size()-1;i>=0;i--){ | ||
ans.push_back(temp[i]) ; | ||
} | ||
} | ||
|
||
void leaves(Node *root, vector<int> & ans){ | ||
if(isleaf(root)){ | ||
ans.push_back(root->data) ; | ||
return ; | ||
} | ||
if(root->left) leaves(root->left, ans) ; | ||
if(root->right) leaves(root->right,ans) ; | ||
|
||
} | ||
|
||
vector <int> boundary(Node *root) | ||
{ | ||
//Your code here | ||
vector<int> ans ; | ||
if(!root) return ans ; | ||
|
||
if(!isleaf(root)) ans.push_back(root->data) ; | ||
leftboun(root,ans) ; | ||
leaves(root,ans) ; | ||
rightboun(root,ans) ; | ||
|
||
return ans ; | ||
} | ||
}; |