Posts

Showing posts from January, 2015

STL's EASY WAY TO FIND maximum & minimum IN AN ARRAY

* max_element ( a , a + n )-* min_element ( a , a + n )
ONE_ZERO SPOJ  http://www.spoj.com/problems/ONEZERO/ It's very important to make a visited array [1...n], so that the number which is visited once will not be visited again, if we would not make this visited it will definitely lead to TLE :P (idea credit kishlay ) solution #include<bits/stdc++.h> using namespace std; string bfs(int n) {  pair<int,string> temp;  queue<pair<int,string> > st;  st.push(make_pair(1,"1"));  int visited[n];  for(int i=0;i<n;i++) visited[i]=0; visited[1]=1;  while(!st.empty())  {   string tt1,tt2;   temp=st.front();   st.pop();   if(temp.first%n!=0)   {     tt1=temp.second+"0";     //cout<<tt<<" ";     if(visited[(temp.first*10)%n]==0){     st.push(make_pair((temp.first*10)%n,tt1));     visited[(temp.first*10)%n]=1;}     ...

UNION FIND FOR CODECHEF QUESTION http://www.codechef.com/ALMA2015/problems/ALMA03/

chota bheem and gcd ( https://www.codechef.com/problems/ALMA03  ) #include <bits/stdc++.h> using namespace std; #define MAXN 205 #define MOD 1000000007 int parent [ MAXN ] ,A [ MAXN ] ; void init ( ) { for ( int i= 0 ;i<MAXN;++i ) parent [ i ] = - 1 ; } int find ( int i ) { if ( parent [ i ] ==- 1 ) return i; else return parent [ i ] = find ( parent [ i ] ) ; } void merge ( int x, int y ) { x = find ( x ) ; y = find ( y ) ; if ( x!=y ) { if ( A [ x ] >A [ y ] ) parent [ y ] = x; /// whose rank is greater will become parent (here Rank work is done by Cost itself) else parent [ x ] = y; } } int main ( ) { int t,n; scanf ( "%d" ,&t ) ; while ( t-- ) { init ( ) ; scanf ( "%d" ,&n ) ; for ( int i= 0 ;i<n;++i )  { scanf ( "%d" ,A+i ) ; } for ( int i= 0 ;i<n;++i ) for ( in...
nCr  find  karne  ka  best  example. #include<bits/stdc++.h> using namespace std; int fast_pow(long long base, long long n,long long M) {     if(n==0)        return 1;     if(n==1)     return base;     long long halfn=fast_pow(base,n/2,M);     if(n%2==0)         return ( halfn * halfn ) % M;     else         return ( ( ( halfn * halfn ) % M ) * base ) % M; } int findMMI_fermat(int n,int M) {     return fast_pow(n,M-2,M); } int main() {     long long fact[100001];     fact[0]=1;     int i=1;     int MOD=1000000007;     while(i<=100000)     {         fact[i]=(fact[i-1]*i)%MOD;         i++;     }     int t;     cin>>t;     while(t--)   ...
( BFS  APP. 3rd)  COUNT  NUMBER  OF  CONNECTED  COMPONENTS .  SPOJ (PRAYATNA  PR)    USING  ADJ.  LIST #include<bits/stdc++.h> using namespace std; vector<int> vv[100000]; void bfs(int src,int v,int visited[],int lebel[]) {  int i,rem;  lebel[src]=0;  visited[src]=1;  queue<int> q;  q.push(src);  while(!q.empty())  {   rem=q.front();   q.pop();   for(i=0;i<vv[rem].size();i++)   {    if(visited[ vv[rem][i] ]!=1)    {      lebel[vv[rem][i]]=lebel[rem]+1;      q.push(vv[rem][i]);      visited[vv[rem][i]]=1;    }   }  } } int main() { int t,hh; cin>>t; for(hh=1;hh<=t;hh++) {  int i,j,p,q,rem,v,e,flag=0,gco=1;  cin>>v>>e;   int lebel[v];   int visited[v];  for(i=0;i<v;i++) ...
DIJKSTRA  IN 2 D  MATRIX  USING  PRIORITY QUEUE :) SPOJ SHOPPING #include<bits/stdc++.h> #define f first #define s second using namespace std; int indx[]={-1,0,0,+1}; int indy[]={0,+1,-1,0}; int w,h; string a[1000]; #define pp pair<int,pair<int,int> > class Prioritize { public:     int operator() ( const pair<int,pair<int,int> >& p1, const pair<int,pair<int,int> >& p2 )     {         return p1.first > p2.first;     } }; void dij(int x,int y,int x1,int y1) {   //cout<<"gaya"<<endl;  int visited[h][w];  int i,j,dt1,dt2,dt3;  for(i=0;i<h;i++)    for(j=0;j<w;j++)      visited[i][j]=0;  int dist[h][w];  for(i=0;i<h;i++)    for(j=0;j<w;j++)      dist[i][j]=INT_MAX;   visited[x][y]=1;   dist[x][y]=0;   priority_q...
STL  SET  QUESTION   SPOJ   FRIENDS OF  FRIEND #include<bits/stdc++.h> using namespace std; int main() { int n,i,k,m,j; cin>>n; set<int> s; for(i=0;i<n;i++) { cin>>k; s.insert(k);  cin>>m;  for(j=0;j<m;j++)  {   cin>>k;   s.insert(k);  } }  //for (std::set<int>::iterator it=s.begin(); it!=s.end(); ++it)    // std::cout << ' ' << *it;       cout<<s.size()-n<<endl; return 0; }
DIJKSTRA  USING  SET  (ADJ. LIST )\\  spoj ques (The shortest path) Here set  is doing the same job as  of minimum priority queue.. Set also arrange the in ascending order. #include<bits/stdc++.h> #define pp pair<int,int> using namespace std; void dijk(int src,int des,int nc,vector<pair<int,int> > v[]) {  int i,j,u;  set<pair<int,int> > Q;  set<pair<int,int> > :: iterator itx;    int dist[nc]; for(i=0;i<nc;i++) dist[i]=INT_MAX;  dist[src]=0;  Q.insert(pp(dist[src],src));     while(!Q.empty())     {         itx=Q.begin();         u = itx->second;         Q.erase(itx);                    ///jo visited ho gaya wo poped        if(itx->first<= dist[u])         { ...
( BFS APP. 1) CHECKING OF  BIPARTITE  GRAPH  USING  ADJACENTARY  LIST #include<bits/stdc++.h> using namespace std; vector<int> vv[20001]; //vector<int>  vec[10000]; void bfs(int src,int v,int visited[],int lebel[]) {  int i,rem;  lebel[src]=0;  visited[src]=1;  queue<int> q;  q.push(src);  while(!q.empty())  {   rem=q.front();   q.pop();   for(i=0;i<vv[rem].size();i++)   {    if(visited[ vv[rem][i] ]!=1)    {      lebel[vv[rem][i]]=lebel[rem]+1;      q.push(vv[rem][i]);      visited[vv[rem][i]]=1;    }   }  } for(i=1;i<=v;i++) if(!visited[i]) bfs(i,v,visited,lebel); } int main() { int t,hh; cin>>t; for(hh=1;hh<=t;hh++) {  int i,j,p,q,rem,v,e,flag=0;  cin>>v>>e;   int lebel[v+1];    int visited...
(BFS APP 2) LONGEST   PATH  IN   A  TREE   BY  THE   USE    OF    ADJACENATARY     LIST. #include<bits/stdc++.h> using namespace std; vector<int> vv[10001]; int lebel[10001]; int visited[10001]; void bfs(int src,int v) {     //cout<<v<<endl; // cout<<"hi"<<endl;  int i,rem;  for(i=1;i<=v;i++) lebel[i]=-1;  lebel[src]=0;  for(i=1;i<=v;i++) visited[i]=0;  ///no vertex is visited yet  visited[src]=1;  queue<int> q;  q.push(src);  while(!q.empty())  {   rem=q.front();   //cout<<"nikala"<<rem<<" ";   q.pop();   //cout<<vv[rem].size()<<" size ";   for(i=0;i<vv[rem].size();i++)   {    if(visited[ vv[rem][i] ]!=1)    {      lebel[vv[rem][i]]=lebel[rem]+1;     //     ...
BFS     WITH    LEBEL #include<bits/stdc++.h> using namespace std; int graph[100][100],v; void bfs(int src) {  int lebel[v+1],i,rem;  for(i=1;i<=v;i++) lebel[i]=-1;  lebel[src]=0;  int visited[v+1];  for(i=1;i<=v;i++) visited[i]=0;  ///no vertex is visited yet  visited[src]=1;  queue<int> q;  q.push(src);  while(!q.empty())  {   rem=q.front();   cout<<rem<<" ";   q.pop();   for(i=1;i<=v;i++)   {    if(graph[rem][i]==1 && visited[i]==0)    {     lebel[i]=lebel[rem]+1;     cout<<i<<" ";     q.push(i);     visited[i]=1;    }   }  } cout<<endl; for(i=1;i<=v;i++) cout<<lebel[i]<<" "; } int main() { int i,j; cin>>v; for(i=1;i<=v;i++)  for(j=1;j<=v;j++)  cin>>grap...
//fast input/ out long long int read_int(){ char r; bool start=false,neg=false; long long int ret=0; while(true){ r=getchar(); if((r-'0'<0 || r-'0'>9) && r!='-' && !start){ continue; } if((r-'0'<0 || r-'0'>9) && r!='-' && start){ break; } if(start)ret*=10; start=true; if(r=='-')neg=true; else ret+=r-'0'; } if(!neg) return ret; else return -ret; //LL int scan_lld()    {int ip=getchar_unlocked(),flag=1;lld ret=0;for(;ip<'0'||ip>'9';ip=getchar_unlocked())if(ip=='-'){flag=-1;ip=getchar_unlocked();break;}for(;ip>='0'&&ip<='9';ip=getchar_unlocked())ret=ret*10+ip-'0';return flag*ret;} int scan_d ( ) { int ip=getchar_unlocked ( ) ,ret= 0 ,flag= 1 ;for ( ;ip< '0' ||ip> '9' ;ip=getchar_unlocked ( ) ) if ( ip== ...
power function ll power ( ll a,ll b,ll mod ) { ll ans= 1 ; while ( b ) { if ( b& 1 ) ans= ( ans*a ) %mod; a= ( a*a ) %mod; b/= 2 ; } return ans; }