Tags » BF's

Light OJ 1257 - Farthest Nodes in a Tree (II)

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1257


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             30001
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d:\n",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

vector<int> graph,cost;

int dis;
int dis1;
int a,b,max_dis,n;

void BFS(int src)
{
    dis=0;
    queue<int>Q;
    Q.push(src);
    while(!Q.empty())
    {
        int u= Q.front();
        Q.pop();
        if(dis[u]>max_dis)
        {
            max_dis=dis[u];
            a=u;
        }
        loop(i,SZ(graph[u]))
        {
            int v=graph[u][i];
            if(dis[v]==-1)
            {
                dis[v]=dis[u]+cost[u][i];
                Q.push(v);
            }
        }
    }
}

void BFS1(int src)
{
    dis1=0;
    queue<int>Q;
    Q.push(src);
    while(!Q.empty())
    {
        int u= Q.front();
        Q.pop();
        loop(i,SZ(graph[u]))
        {
            int v=graph[u][i];
            if(dis1[v]==-1)
            {
                dis1[v]=dis1[u]+cost[u][i];
                Q.push(v);
            }
        }
    }
}

void allclear()
{
    loop(i,n+1)
    {
        graph[i].clear();
        cost[i].clear();
        dis[i]=-1;
        dis1[i]=-1;
    }
    max_dis=-1;
    a=b=-1;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t,x,y,w;
    sf(t);

    TEST_CASE(t)
    {
        sf(n);
        allclear();
        loop(i,n-1)
        {
            sfff(x,y,w);
            graph[x].pb(y);
            graph[y].pb(x);
            cost[x].pb(w);
            cost[y].pb(w);
        }
        BFS(0);
        b=a;
        loop(i,n+1)
            dis[i]=-1;
        max_dis=-1;
        BFS(b);
        b=a;
        BFS1(b);
        PRINT_CASE;
        loop(i,n)
        {
            pf("%d\n",max(dis[i],dis1[i]));
        }
    }
    return 0;
}

… 78 more words
Light OJ

Light OJ 1175 - Jane and the Frost Giants

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1175


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

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

char graph;
int dj;
int df;
bool visit;

int r,c;
vector<pii>fdata;
pii pointj;

bool testf(pii tmp)
{
    if(tmp.ff<0 || tmp.ff>=r || tmp.ss<0 || tmp.ss>=c || visit || graph!='.')
        return 0;
    return 1;
}

bool testj(pii tmp)
{
    if(tmp.ff<0 || tmp.ff>=r || tmp.ss<0 || tmp.ss>=c || visit || graph!='.')
        return 0;
    return 1;
}

void bfsf()
{
    loop(i,r+1) loop(j,c+1)
    {visit[i][j]=0;df[i][j]=inf;}

    queue<pii>Q;
    pii u,v;
    for(int i=0;i<SZ(fdata);i++)
    {
        u=fdata[i];
        df=0;
        Q.push(u);
        visit=1;
    }
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        loop(i,4)
        {
            v.ff=u.ff+fx[i];
            v.ss=u.ss+fy[i];
            if(testf(v))
            {
                visit=1;
                df=df+1;
                Q.push(v);
            }
        }
    }
}

int bfsj(pii src)
{
    loop(i,r+1) loop(j,c+1)
       {
         visit[i][j]=0;
         dj[i][j]=inf;
       }
    visit=1;
    pii u,v;
    dj=0;

    queue<pii>Q;
    Q.push(src);
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        if(u.ff==0 || u.ff==r-1 || u.ss==0 || u.ss==c-1)
            return dj;
        loop(i,4)
        {
            v.ff=u.ff+fx[i];
            v.ss=u.ss+fy[i];
            if(testj(v) && (df > dj+1))
            {
                visit=1;
                dj=dj+1;
                Q.push(v);
            }
        }
    }
    return -1;
}


int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sff(r,c);
        loop(i,r) loop(j,c)
        {
            cin>>graph[i][j];
            if(graph[i][j]=='J')
                pointj.ff=i,pointj.ss=j;
            else if(graph[i][j]=='F')
                fdata.pb(MP(i,j));
        }

        bfsf();
        int ans=bfsj(pointj);
        PRINT_CASE;
        if(ans==-1)
            pf("IMPOSSIBLE\n");
        else
            pf("%d\n",ans+1);
        fdata.clear();
    }
    return 0;
} 68 more words
Light OJ

Light OJ 1141 - Number Transformation

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1141


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

vector<int>v,factor;
bool prime;

void sieve()
{
    prime[0]=1;
    for(int i=2; i<1100;)
    {
        v.pb(i);
        for(int j=i+i; j<1100; j+=i)
            prime[j]=1;
        for(++i;; i++)
            if(!prime[i])
                break;
    }
}

int d;

bool visited;

int bfs(int src, int dst)
{
    d=0;
    queue<int>Q;
    Q.push(src);
    visited=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(u==dst) return d[u];
        int temp=u;
        if(prime[u]==0) continue;
        for(int i=0; temp>1; i++)
        {
            if(temp%v[i]==0)
            {
                factor.pb(v[i]);
                while(temp%v[i]==0)
                    temp/=v[i];
            }
        }

        for(int i=0; i<SZ(factor); i++)
        {
            int v=u+factor[i];
            if(v<=dst && !visited[v])
            {
                visited[v]=1;
                Q.push(v);
                d[v]=d[u]+1;
            }
        }
        factor.clear();
    }
    return -1;
}

void allclear()
{
    ms(visited,0);
    factor.clear();
    ms(d,0);
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    sieve();

    int t;
    sf(t);
    TEST_CASE(t)
    {
        allclear();
        int a,b;
        sff(a,b);
        PRINT_CASE;
            cout<<bfs(a,b)<<endl;
    }
    return 0;
}

Light OJ

Light OJ 1094 - Farthest Nodes in a Tree

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1094


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

vector<int>edge;
vector<int>cost;
int n,point1,point2,ans,v1;
bool visited;
int d;
void bfs(int src)
{
    visited=1;
    d=0;
    queue<int>Q;
    Q.push(src);

    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(d[u]>v1)
        {
            v1=d[u];
            point1=u;
        }

        for(int i=0;i<SZ(edge[u]);i++)
        {
            int v=edge[u][i];
            if(d[v]<d[u]+cost[u][i] && !visited[v])
            {
                d[v]=d[u]+cost[u][i];
                Q.push(v);
                visited[v]=1;
            }
        }
    }
}

void allclear(int n)
{
    loop(i,n+5)
    {
        edge[i].clear();
        cost[i].clear();
        d[i]=0;
        visited[i]=0;
    }
    v1=-1;
}
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sf(n);
        allclear(n);
        loop(i,n-1)
        {
            int u,v,w;
            sfff(u,v,w);
            edge[u].pb(v);
            edge[v].pb(u);
            cost[u].pb(w);
            cost[v].pb(w);
        }
        bfs(0);
        loop(i,n+5)
       {
          d[i]=0;
          visited[i]=0;
       }
        v1=-1;
        bfs(point1);
        PRINT_CASE;
        pf("%d\n",v1);
    }
    return 0;
}

… 46 more words
Light OJ

Light OJ 1066 - Gathering Food

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1066


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

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

char ara,ch;
int cnt,n;
bool visited;
int d;
map<char,pii> mp;

bool test(pii temp)
{
    if(temp.ff<0 || temp.ff>=n || temp.ss<0 || temp.ss>=n || visited|| ara=='#')
        return 0;
    if(ara>='A' && ara<='Z')
    {
        if(ara>ch)
            return 0;
    }
    return 1;
}

int bfs(pii src, pii dst)
{
    pii u=src;
    visited=1;
    d=0;
    queue<pii>Q;
    Q.push(u);
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        if(u==dst) return d;
        pii v;
        for(int i=0; i<4; i++)
        {
            v.ff=u.ff+fx[i];
            v.ss=u.ss+fy[i];
            if(test(v))
            {
                visited=1;
                d=d+1;
                Q.push(v);
            }
        }
    }
    return -1;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sf(n);
        cnt=0;
        loop(i,n)
        loop(j,n)
        {
            cin>>ara[i][j];
            if(ara[i][j]>='A' && ara[i][j]<='Z')
            {
                cnt++;
                mp[j]]=MP(i,j);
            }
        }
        int ans=0;
        bool ispossible=1;
        for(int i=0; i<cnt-1; i++)
        {
            int tmp=0;
            ch='A'+i+1;
            tmp=bfs(mp['A'+i],mp['A'+i+1]);
            ms(d,0);
            ms(visited,0);
            if(tmp==-1)
            {
                ispossible=0;
                break;
            }
            else
                ans+=tmp;

        }
        PRINT_CASE;
        if(ispossible)
            pf("%d\n",ans);
        else
            pf("Impossible\n");
        mp.clear();
    }
    return 0;
} 46 more words
Light OJ

Light OJ 1039 - A Toy Company

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1039


/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long


using namespace std;

map<string,bool> visited,block;

struct data
{
    string str;
    int cnt=0;
    data(string s, int n)
    {
        str=s;
        cnt=n;
    }
};

bool test(string str)
{
    if(visited) return 0;
    return 1;
}

int bfs(string src,string dst)
{
    visited.clear();
    visited=1;
    data u(src,0);
    queue<data>Q;
    Q.push(u);

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

        if(block)
            continue;
        if(u.str==dst) return u.cnt;

        for(int i=0; i<3; i++)
        {
            string temp=u.str;
            temp[i]++;
            if(temp[i]=='z'+1)
                temp[i]='a';
            if(test(temp))
            {
                visited=1;
                data v(temp,u.cnt+1);
                Q.push(v);
            }
            string temp1=u.str;
            temp1[i]-=1;
            if(temp1[i]=='a'-1)
                temp1[i]='z';
            if(test(temp1))
            {
                visited=1;
                data v(temp1,u.cnt+1);
                Q.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        string str1,str2;
        cin>>str1>>str2;
        int n;
        sf(n);
        while(n--)
        {
            string tmp1,tmp2,tmp3;
            cin>>tmp1>>tmp2>>tmp3;
            loop(i,SZ(tmp1))
            {
                string xx;
                xx+=tmp1[i];
                loop(i,SZ(tmp2))
                {
                    xx+=tmp2[i];
                    loop(i,SZ(tmp3))
                    {
                        xx+=tmp3[i];
                        block=1;
                        xx.erase(xx.end()-1,xx.end());
                    }
                    xx.erase(xx.end()-1,xx.end());
                }
                xx.erase(xx.end()-1,xx.end());
            }
        }
        PRINT_CASE;
        pf("%d\n",bfs(str1,str2));
        block.clear();
    }
    return 0;
} 7 more words
Light OJ

Leetcode: Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 89 more words

Leetcode