Thursday, April 28, 2016

1294 - Positive Negative Sign

Problem Linkhttp://www.lightoj.com/volume_showproblem.php?problem=1294

Solve :

#include<bits/stdc++.h>

using namespace std;

int main()
{

    long long int n,m,j=1,t;
    scanf("%lld",&t);

    while(t--)
    {
        scanf("%lld%lld",&n,&m);

        n=n/2;
        n=n*m;
        printf("Case %lld: %lld\n",j,n);
        j++;

    }

}

Tuesday, April 26, 2016

Topological Sort Source Code ( 10305 - Ordering Tasks )

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1246

Solve :

#include<bits/stdc++.h>

using namespace std;

vector <int> v[103],top;

int indgre[103];

int main()
{

    int n,e,i,a,b,j,k,l,sp;

    while(cin>>n>>e)
    {
        sp=0;

        if(n==0&&e==0) break;
        memset(indgre,0,sizeof(indgre));
        for(i=1; i<=e; i++)
        {
            cin>>a>>b;
            v[a].push_back(b);
            indgre[b]++;
        }

        for(i=1; i<=n; i++)
        {
            if(indgre[i]==0)
            {
                top.push_back(i);
            }
        }

        for(i=0; i<top.size(); i++)
        {
            k=top[i];

            for(j=0; j<v[k].size(); j++)
            {
                l=v[k][j];
                indgre[l]--;

                if(indgre[l]==0) top.push_back(l);
            }
        }
        for(i=0; i<top.size(); i++)
        {
            if(sp==0) cout<<top[i];
            else cout<<" "<<top[i];
            sp++;
        }
        cout<<endl;
        top.clear();
        for(i=0; i<103; i++)
        {
            v[i].clear();
        }
    }

}



Saturday, April 23, 2016

1387 - Setu

Problem Linkhttp://www.lightoj.com/volume_showproblem.php?problem=1387

Solve :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long int t,n,cnt,i,j=0,k=1,l;
    char c1,c2;

    scanf("%lld",&t);

    while(t--)
    {
        scanf("%lld",&n);
        getchar();
        printf("Case %lld:\n",k);
        k++;
        cnt=0;
        while(n--)
        {

            scanf("%c",&c1);
            if(c1=='d')
            {
                scanf("%c%c%c%c%c%c%d",&c2,&c2,&c2,&c2,&c2,&c2,&j);
                getchar();
                cnt=cnt+j;
            }
            else
            {
                scanf("%c%c%c%c%c",&c2,&c2,&c2,&c2,&c2);
                getchar();
                printf("%lld\n",cnt);
            }
        }
    }

}

1116 - Ekka Dokka

Problem link : http://www.lightoj.com/volume_showproblem.php?problem=1116

Solve :
#include<bits/stdc++.h>

using namespace std;

int main()
{

    long long int t,n,i,j,k=1,w;

    scanf("%lld",&t);

    while(t--)
    {
        scanf("%lld",&w);

        if(w%2!=0) printf("Case %lld: Impossible\n",k);

        else
        {

            for(i=2; i<=w; i=i+2)
            {
                j=w/i;
                if(w%i==0&&j%2!=0)
                {
                    printf("Case %lld: %lld %lld\n",k,j,i);
                    break;
                }
            }
            if(i>w) printf("Case %lld: Impossible\n",k);
        }
        k++;
    }


}

1107 - How Cow

Problem linkhttp://www.lightoj.com/volume_showproblem.php?problem=1107

Solve :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t,x1,y1,x2,y2,x,y,n,i=1;
    scanf("%d",&t);

    while(t--)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        scanf("%d",&n);
        printf("Case %d:\n",i);
        i++;
        while(n--)
        {
            scanf("%d%d",&x,&y);


            if(x>x1 and x<x2 and y>y1 and y<y2)
            {
                printf("Yes\n");
            }
            else printf("No\n");
        }

    }

}

1182 - Parity

Problem linkhttp://www.lightoj.com/volume_showproblem.php?problem=1182

Solve :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long int t,n,j,k=1,cnt;

    scanf("%lld",&t);

    while(t--)
    {
        scanf("%lld",&n);
        cnt=0;
        while(n)
        {
            j=n%2;
            if(j==1) cnt++;
            n=n/2;
        }
        if(cnt%2==0) printf("Case %lld: even\n",k);
        else printf("Case %lld: odd\n",k);
        k++;
    }

}

Sunday, April 17, 2016

UVA-459 - Graph Connectivity- ( Category : DFS )

Problem link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=400

Solution :

#include<bits/stdc++.h>

using namespace std;

map<char,int>mp;
bool visited[30];
vector<int>v[100];

void DFS(int n)
{
    visited[n]=true;
    int u;

    for(int i=0; i<v[n].size(); i++)
    {
        u=v[n][i];
        if(!visited[u])
        {
            DFS(u);
        }
    }
}
int main()
{

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

    int t;
    int ok=0;
    scanf("%d",&t);
    getchar();


    while(t--)
    {

        if (ok>0) cout<<endl;
        ok++;
        //getchar();

        int cnt=0,i,j,k,l=1,node;

        char w,x,y;
        string s;
        cin>>w;
        getchar();
        node=w-64;

        while(getline(cin,s))
        {

            x=s[0];
            if (x==NULL) break;
            y=s[1];
            //getchar();
            if(mp.find(x)==mp.end())
            {
                mp[x]=l;
                l++;
            }
            if(mp.find(y)==mp.end())
            {
                mp[y]=l;
                l++;
            }
            v[mp[x]].push_back(mp[y]);
            v[mp[y]].push_back(mp[x]);


        }
        memset(visited,0,sizeof(visited));
        for(i=1; i<=node; i++)
        {


            if(!visited[i])
            {
                DFS(i);
                cnt++;
            }
        }

        cout<<cnt<<endl;
        mp.clear();

        for(i=0; i<100; i++) v[i].clear();
    }


}

DFS Source Code

#include <bits/stdc++.h>

using namespace std;

vector<int>v[100];
bool visited[100];

int node,edge;

void DFS(int n)
{
    visited[n]=true;
    cout<<n<<" ";
    int temp;
    for(int i=0; i<v[n].size(); i++)
    {
        temp=v[n][i];
        if(!visited[temp])
            DFS(temp);
    }
}


int main()
{
    cin>>node>>edge;
    for(int i=0; i<edge; i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    int src,dst;
    cin>>src>>dst;
    memset(visited,0,sizeof visited);
    cout<<"DFS traverse order is = ";
    DFS(src);
    cout<<endl;
    if(visited[dst])
        cout<<dst<<" is reachable from "<<src<<endl;
    else
        cout<<dst<<" is not reachable from "<<src<<endl;
    return 0;
}

Tuesday, April 12, 2016

280 - Vertex-( UVA ) ( Category : BFS )

Problem link :https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=216&mosmsg=Submission+received+with+ID+17192900

Solution :
#include<bits/stdc++.h>

using namespace std;

int visited[102];
vector<int>v[102];

void bfs(int s,int n)
{
    int cnt=0,u,p,ans;
    memset(visited,-1,sizeof(visited));

    queue<int>q;

    q.push(s);

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(int i=0; i<v[u].size(); i++)
        {
            p=v[u][i];

            if(visited[p]==-1)
            {
                visited[p]=0;
                q.push(p);
                cnt++;
            }
        }
    }
    ans=n-cnt;
    cout<<ans;
    for(int i=1; i<=n; i++)
    {
        if(visited[i]==-1) cout<<" "<<i;
    }
    cout<<endl;
}

int main()
{
    int t,a,b,r=0,t1;


    while(   (cin>>t) and (t!=0))
    {
        while(1)
        {

            if(r==0)
            {
                cin>>a;
                if(a==0) break;
                r++;
            }
            cin>>b;
            if(b==0) r=0;

            else
            {
                v[a].push_back(b);
            }

        }
        cin>>t1;

        for(int i=0; i<t1; i++)
        {
            cin>>b;
            bfs(b,t);
        }

        for(int i=0; i<102; i++) v[i].clear();

    }

}

Monday, April 11, 2016

UVA-439 - ( Knight Moves ) ( Category : BFS )

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=380

Solution :

#include<bits/stdc++.h>
#define pii pair<int,int>

using namespace std;

int visited[9][9];
int fx[]= {-2,-2,-1,1,2,-1,1,2};
int fy[]= {1,-1,2,2,1,-2,-2,-1};

int bfs(pii s,pii d)
{
    memset(visited,-1,sizeof(visited));

    int i,j,k,x,y;

    queue<pii>q;
    pii u,a;

    visited[s.first][s.second]=0;
    q.push(s);

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(i=0; i<8; i++)
        {
            x=u.first+fx[i];
            y=u.second+fy[i];

            if(x>=1 and x<=8 and y>=1 and y<=8 and visited[x][y]==-1)
            {
                visited[x][y]=visited[u.first][u.second]+1;
                a.first=x;
                a.second=y;
                q.push(a);
            }
            if(a==d) break;
        }

    }

    return visited[d.first][d.second]-visited[s.first][s.second];
}

int main()
{

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

    char a,b,dummy;
    int c ,d,a1,b1;
    int v;

    while(scanf("%c%d%c%c%d",&a,&c,&dummy,&b,&d)==5)
    {
        getchar();

        a1=a-'a'+1;
        b1=b-'a'+1;

        pii s,ds;

        s.first=c;
        s.second=a1;
        ds.first=d;
        ds.second=b1;

        v= bfs(s,ds);

        printf("To get from %c%d to %c%d takes %d knight moves.\n",a,c,b,d,v);
    }



}

10959 - UVA-( The Party, Part I ) (Category : BFS )

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1900

Solution :

#include<bits/stdc++.h>

using namespace std;

vector<long long int>v[1005];
long long int visited[1005];

void bfs(long long int k)
{
    memset(visited,-1,sizeof(visited));

    queue<long long int>q;

    visited[0]=0;
    q.push(0);

    long long int u,p,i;

    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(i=0;i<v[u].size();i++)
        {
            p=v[u][i];

            if(visited[p]==-1)
            {
                visited[p]=visited[u]+1;
                q.push(p);
            }
        }
    }
  for(i=1;i<k;i++)
        {
            cout<<visited[i]<<endl;
        }
}


int main()

{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long int t,i,j,k,l,a,b;

    scanf("%lld",&t);

    j=0;

    while(t--)
    {
        if(j>0) cout<<endl;

        j++;

        cin>>k>>l;

        for(i=0;i<l;i++)
        {
            cin>>a>>b;

            v[a].push_back(b);
            v[b].push_back(a);

        }

        bfs(k);
        for(i=0;i<k;i++) v[i].clear();
    }

}


Sunday, April 10, 2016

UVA-762 ( WE SHIP CHEAP ) (Category : BFS)

Problem link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=703

Solution :

#include<bits/stdc++.h>

using namespace std;

map<string,long long int>mp;
vector<long long int>v[1000];

long long int visited[1000];
long long int parent[1000];

vector<string>w;
vector<long long int>c;

void bfs(long long int s,long long int d)
{
    if(s==-30||d==-30)
    {
        cout<<"No route"<<endl;
        return;
    }
    memset(visited,-1,sizeof(visited));
    memset(parent,-1,sizeof(parent));

    long long  int u,p;

    queue<long long int>q;

    q.push(s);

    visited[s]=0;

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(long long int i=0; i<v[u].size(); i++)
        {
            p=v[u][i];

            if(visited[p]==-1)
            {
                visited[p]=visited[u]+1;
                parent[p]=u;
                q.push(p);
            }
            if(u==d) break;
        }
    }
    if(visited[d]==-1) cout<<"No route"<<endl;
    else
    {


        long long int r=d;
        c.push_back(r);
        r=parent[r];
        while(r!=s)
        {
            c.push_back(r);
            r=parent[r];
        }
        c.push_back(s);

        reverse(c.begin(),c.end());

        for(long long int i=0; i<(c.size()-1); i++)
        {
            cout<<w[c[i]]<<" "<<w[c[i+1]]<<endl;
        }

    }

}

int main()
{
//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long int t,i,j,k,l=0;
    string a,b;

    while(scanf("%lld",&t)==1)
    {
        if(l>0) cout<<endl;
        l++;
        j=0;

        for(i=0; i<t; i++)
        {
            cin>>a>>b;
            getchar();

            if(mp.find(a)==mp.end())
            {
                mp[a]=j;
                w.push_back(a);
                j++;
            }
            if(mp.find(b)==mp.end())
            {
                mp[b]=j;
                w.push_back(b);
                j++;
            }
            v[mp[a]].push_back(mp[b]);
            v[mp[b]].push_back(mp[a]);
        }
        cin>>a>>b;

        if(mp.find(a)==mp.end()) mp[a]=-30;
        if(mp.find(b)==mp.end()) mp[b]=-30;


        bfs(mp[a],mp[b]);

        mp.clear();
        c.clear();
        w.clear();
        for( i=0; i<1000; i++) v[i].clear();
    }


}

Friday, April 8, 2016

UVA-336 ( A NODE TO FAR ) ( Category : Bfs with Array Compression)

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=272

Solution:

#include<bits/stdc++.h>

using namespace std;

map<long long int,long long int>mp;

long long int visited[32];
vector<long long int>v[32];

long long int bfs(long long int s,long long int t,long long int node)
{
    memset(visited,-1,sizeof(visited));

    queue<long long int>q;

    visited[s]=0;
    q.push(s);

    long long int u,p;

    while(!q.empty())
    {
        u=q.front();
        q.pop();

        for(long long int i=0; i<v[u].size(); i++)
        {
            p=v[u][i];

            if(visited[p]==-1)
            {
                visited[p]=visited[u]+1;
                q.push(p);
            }

        }
    }
    long long int cnt=0;
    for(long long int i=1; i<node; i++)
    {
        if(visited[i]>t||visited[i]==-1) cnt++;
    }
    return cnt;
}

int main()
{

    long long int r,i,a,b,c,d,e,f,p=1;

    while(1)
    {
        cin>>r;
        if(r==0) break;

        c=1;

        for(i=0; i<r; i++)
        {
            cin>>a>>b;

            if(mp.find(a)==mp.end())
            {
                mp[a]=c;
                c++;
            }
            if(mp.find(b)==mp.end())
            {
                mp[b]=c;
                c++;
            }
            v[mp[a]].push_back(mp[b]);
            v[mp[b]].push_back(mp[a]);

        }

        while(1)
        {
            cin>>d>>e;
            if(d==0&&e==0) break;
            f=bfs(mp[d],e,c);
            cout<<"Case "<<p<<": "<<f<<" nodes not reachable from node "<<d<<" with TTL = "<<e<<"."<<endl;
            p++;

        }
        mp.clear();

        for(long long int w=0; w<32; w++) v[w].clear();
    }


}

Tuesday, April 5, 2016

10653 - Bombs! NO they are Mines!! -UVA-BFS

Problem link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1594&mosmsg=Submission+received+with+ID+17149522

Solution :

#include<bits/stdc++.h>
#define pii pair<int,int>

using namespace std;

int cell[1050][1050];
int r,c;
int visited[1050][1050];

int fx[]= {1,-1,0,0};
int fy[]= {0,0,1,-1};

void bfs(pii s,pii d)
{
    int b,i,k,x,y;

    memset(visited,-1,sizeof(visited));

    queue<pii>q;
    pii a,u;

    q.push(s);
    visited[s.first][s.second]=0;

    while(!q.empty())
    {
        a=q.front();
        q.pop();

        for(i=0; i<4; i++)
        {
            x=a.first+fx[i];
            y=a.second+fy[i];

            if(x>=0 and x<r and y>=0 and y<c and cell[x][y]!=-1 and visited[x][y]==-1)
            {
                visited[x][y]=visited[a.first][a.second]+1;
                u=make_pair(x,y);
                q.push(u);
            }
            if(a.first==d.first&&a.second==d.second) break;
        }
    }
    cout<< visited[d.first][d.second]-visited[s.first][s.second]<<endl ;
}

int main()
{
    int a,b,e,f,g,i,j,w;

    while(cin>>r>>c && (r && c))
    {

        memset(cell,0,sizeof(cell));

        scanf("%d",&a);

        for(i=0; i<a; i++)
        {
            scanf("%d%d",&b,&w);
            for(j=0; j<w; j++)
            {
                scanf("%d",&e);
                cell[b][e]=-1;
            }
        }
        pii s,d;

        scanf("%d%d",&s.first,&s.second);

        scanf("%d%d",&d.first,&d.second);

        bfs(s,d);

    }

}

Sunday, April 3, 2016

Risk-567-UVA ( Category : BFS )


Problem
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=508&mosmsg=Submission+received+with+ID+17140993


Solve:

#include<bits/stdc++.h>

using namespace std;
vector<int>v[23];

int bfs(int s,int d)
{
    int visited[25];
    memset(visited,-1,sizeof(visited));
    int w;
    queue<int>q;

    visited[s]=0;

    q.push(s);

    int u,p;

    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0; i<v[u].size(); i++)
        {
            p=v[u][i];
            if(visited[p]==-1)
            {
                visited[p]=visited[u]+1;
                q.push(p);
                if(p==d) break;
            }
        }

    }
    w= visited[d]-visited[s];
    return w;
}

int main()
{

    int a,b,c,d,e,f,g,h=1,i,j;

    while(scanf("%d",&a)==1)
    {
        b=1;

        for(i=1; i<=a; i++)
        {
            scanf("%d",&c);
            v[b].push_back(c);
            v[c].push_back(b);
        }
        b++;

        for(i=1; i<=18; i++)
        {
            scanf("%d",&a);

            for(j=1; j<=a; j++)
            {
                scanf("%d",&c);
                v[b].push_back(c);
                v[c].push_back(b);
            }
            b++;
        }

        scanf("%d",&e);
        printf("Test Set #%d\n",h);
        h++;
        for(i=1; i<=e; i++)
        {
            scanf("%d%d",&f,&g);

            c=bfs(f,g);

            printf("%2d to %2d: %d\n",f,g,c);
        }
        printf("\n");
        for(i=0; i<=21; i++) v[i].clear();
    }
}

Saturday, April 2, 2016

BFS source code #1

Problem : First you have to store a graph in a adjacency list.Then you have to determine the shortest distance between source and destination node.

Problem link and solution idea : http://www.shafaetsplanet.com/planetcoding/?p=604

Source code :

#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<<"Eenter node and edge number : ";
    int node,edge;

    cin>>node>>edge;

    vector<int>v[100];

    int i;
    int a,b;
    cout<<"Enter nodes connecting edges : "<<endl;
    for(i=1; i<=edge; i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
    }
    cout<<endl;
    int source,destination;

    while(1)
    {
        cout<<"Source : ";
        cin>>source;
        cout<<"destination : ";
        cin>>destination;

        int visited[100];

        memset(visited,-1,sizeof(visited));

        queue<int>q;

        q.push(source);

        visited[source]=0;

        int u,p;

        while(!q.empty())
        {
            u=q.front();
            q.pop();
            for(i=0; i<v[u].size(); i++)
            {
                p=v[u][i];

                if(visited[p]==-1)
                {
                    visited[p]=visited[u]+1;
                    q.push(p);
                    if(p==destination) break;
                }

            }
        }
        cout<<"Shortest distance betwwen destination and source : ";
        cout<<visited[destination]-visited[source]<<endl<<endl;

    }
    return 0;
}

Friday, April 1, 2016

Array compression with map

problem link : http://www.shafaetsplanet.com/planetcoding/?p=1388

code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    map<long long int,long long int>mp;

    long long int a[1000];//given array
    long long int b[1000];//compressed/matched array

    long long  int d,c=0,i,j=0;
    long long  int n;
    cin>>n;//array size

    for(i=0; i<n; i++)
    {
        cin>>a[i];//taking array inputs
    }
    cout<<endl;
    for(i=0; i<n; i++)
    {
        d=a[i];
        if(mp.find(d)==mp.end())//checking if map with value d is empty or not
        {
            mp[d]=c;

            cout<<d <<" is matched with "<<c<<endl<<endl;
            c++;
        }
        d=mp[d];
        b[j]=d;
        j++;
    }
    cout<<endl;
    cout<<"compressed/matched array:"<<endl<<endl;
    for(i=0; i<n; i++)
    {
        cout<<b[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Finding the positions of a number in a array using 2d vector

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int a[100];
    int n;//size of array
    cin>>n;
    int i;
    int d;
    vector<int> v[10000];//2d vector
    for(i=0; i<n; i++)
    {
        cin>>d;
        a[i]=d;
        v[d].push_back(i);
    }
    while(1)
    {

        int c;//number to find
        cin>>c;
        if(v[c].size()==0)
        {
            cout<<"khali"<<endl;//no such elements
        }
        for(i=0; i<v[c].size(); i++)
        {

            cout<<v[c][i]<<" ";//printing the positions of that number
        }

        cout<<endl;

    }
}