-
Notifications
You must be signed in to change notification settings - Fork 80
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 #56 from sanskar28/master
Added Miscellaneous problems in C++
- Loading branch information
Showing
4 changed files
with
131 additions
and
3 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,33 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
struct Node { | ||
Node* parent = nullptr; | ||
int count = 1; // # decendents + current | ||
}; | ||
Node* getRoot(Node* node) { | ||
while(node->parent) node = node->parent; | ||
return node; | ||
} | ||
int merge(Node* a, Node* b) { | ||
Node* r1 = getRoot(a); | ||
Node* r2 = getRoot(b); | ||
if(r1 == r2) return r1->count; | ||
// Add small tree to root of big tree | ||
Node *big, *small; | ||
if(r1->count > r2->count) { big = r1; small = r2; } else { big = r2; small = r1; } | ||
small->parent = big; | ||
big->count += small->count; | ||
return big->count; | ||
} | ||
int main() { | ||
int n; | ||
scanf("%d", &n); | ||
int largest = 1, a, b; | ||
unordered_map<int, Node> nodes; | ||
for(int i = 0; i < n; ++i) { | ||
scanf("%d %d", &a, &b); | ||
largest = max(largest, merge(&nodes[a], &nodes[b])); | ||
printf("%d\n", largest); | ||
} | ||
return 0; | ||
} |
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,81 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
struct trie{ | ||
trie *child[2]; | ||
bool is_end; | ||
}; | ||
trie *new_node() | ||
{ | ||
trie *temp=new trie; | ||
temp->child[0]=NULL; | ||
temp->child[1]=NULL; | ||
temp->is_end=false; | ||
return temp; | ||
} | ||
void insert(trie *root,int x) | ||
{ | ||
trie *temp=root; | ||
int index; | ||
for(int i=31;i>=0;i--) | ||
{ | ||
if(x&(1<<i)) | ||
index=1; | ||
else | ||
index=0; | ||
if(temp->child[index]==NULL) | ||
temp->child[index]=new_node(); | ||
|
||
temp=temp->child[index]; | ||
} | ||
temp->is_end=true; | ||
} | ||
void search_result(trie *root,int x) | ||
{ int cnt=0,index,index_; | ||
trie *temp=root; | ||
for(int i=31;i>=0;i--) | ||
{ | ||
if(x&(1<<i)) | ||
index=1; | ||
else | ||
index=0; | ||
|
||
index_=(index+1)%2; | ||
|
||
if(temp->child[index_]!=NULL && temp->is_end==false) | ||
{ | ||
cnt=cnt+pow(2,i) ; | ||
temp=temp->child[index_]; | ||
} | ||
else { | ||
temp=temp->child[index]; | ||
} | ||
} | ||
cout<<cnt<<endl; | ||
} | ||
void max_xor(int vec[],int n,int q[],int t) | ||
{ | ||
trie *root=new_node(); | ||
|
||
for(int i=0;i<n;i++){ | ||
//cout<<"insert: "<<q[i]<<endl;; | ||
insert(root,vec[i]);} | ||
|
||
for(int i=0;i<t;i++) | ||
{ //cout<<"q["<<i<<"]: "<<q[i]<<"="; | ||
search_result(root,q[i]); | ||
} | ||
} | ||
int main() | ||
{ | ||
int n,x,t; | ||
cin>>n; | ||
int vec[n]; | ||
for(int i=0;i<n;i++) | ||
cin>>vec[i]; | ||
|
||
cin>>t;int q[t]; | ||
for(int i=0;i<t;i++) | ||
cin>>q[i]; | ||
max_xor(vec,n,q,t); | ||
return 0; | ||
} |
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,14 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--){ | ||
unsigned int n; | ||
cin >> n; | ||
cout << ~n << endl; | ||
} | ||
return 0; | ||
} |
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