Light OJ : 1417 – Forwarding Emails

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


/*
         +-+ +-+ +-+
         |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             50005
#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;
 
int n,dfsnum,dis[MAX];
vector<int> graph[MAX],rev_graph[MAX];
bool mp[MAX],mp1[MAX],mp2[MAX];
 
void allclear()
{
    loop(i,n+2)
    {
        graph[i].clear();
        rev_graph[i].clear();
        dis[i]=0;
        mp[i]=mp1[i]=mp2[i]=0;
    }
    dfsnum=0;
}
 
void dfs1(int u)
{
    mp[u]=1;
    dfsnum++;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!mp[v])
            dfs1(v);
    }
    dis[u]=++dfsnum;
}
 
void dfs2(int u)
{
    mp1[u]=1;
    loop(i,SZ(rev_graph[u]))
    {
        int v=rev_graph[u][i];
        if(!mp1[v])
            dfs2(v);
    }
}
 
void dfs3(int u)
{
    mp2[u]=1;
    dfsnum++;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!mp2[v])
            dfs3(v);
    }
}
 
void dfs4(int u)
{
    mp2[u]=0;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(mp2[v])
            dfs4(v);
    }
}
 
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
 
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sf(n);
        allclear();
        int a,b;
        loop(i,n)
        {
            sff(a,b);
            graph[a].pb(b);
            rev_graph[b].pb(a);
        }
        REP(i,1,n+1)
        {
            if(!mp[i])
                dfs1(i);
        }
 
        vector<pii>top_sort;
        REP(i,1,n+1)
        {
            top_sort.pb(MP(dis[i],i));
        }
 
 
        sort(top_sort.rbegin(),top_sort.rend());
        vector<int>scc;
 
        loop(i,SZ(top_sort))
        {
            int u=top_sort[i].ss;
            if(!mp1[u])
            {
                scc.pb(u);
                dfs2(u);
            }
        }
 
        top_sort.clear();
        loop(i,SZ(scc))
        {
            int u=scc[i];
            if(!mp2[u])
            {
                dfsnum=0;
                dfs3(u);
                top_sort.pb(MP(dfsnum,u));
                dfs4(u); // this DFS is clearing only visited node.
            }
        }
 
        sort(top_sort.rbegin(),top_sort.rend());
        PRINT_CASE;
        if(SZ(top_sort)==1)
            pf("%d\n",top_sort[0].ss);
        else
        {
            loop(i,SZ(top_sort)-1)
            {
                if(top_sort[i].ff!=top_sort[i+1].ff) // This is for determine the smallest one.
                {
                    pf("%d\n",top_sort[i].ss);
                    break;
                }
            }
        }
    }
    return 0;
}

Advertisements

Light OJ : 1406 – Assassin`s Creed

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


/*
         +-+ +-+ +-+
         |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>graph[16];
bool visited[16][1<<16];
bool check_vis[1<<16];
int dp[1<<16];
int n,m;

void dfs(int u, int mask)
{
    visited[u][mask]=1;
    check_vis[mask]=1;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!visited[v][mask|(1<<v)])
            dfs(v,mask|(1<<v));
    }
}

void allclear()
{
    loop(i,n+1)
    {
        graph[i].clear();
    }
    ms(visited,0);
    ms(check_vis,0);
    ms(dp,-1);
}

int dp_func(int mask)
{
    int &ret = dp[mask];
    if(mask==0) return 0;
    if(ret!=-1) return ret;
    ret=100;
    for(int i=mask; i>0; i=mask & (i-1))
    {
        if(check_vis[i])
        {
            ret=min(ret,1+dp_func(mask^i)); // XOR for remaining unvisited node
        }
    }
    return ret;
}

int main()
{
    ///freopen("in.txt","r",stdin);
   ///freopen("out.txt","w",stdout);
    int t,a,b;
    sf(t);
    TEST_CASE(t)
    {
        sff(n,m);
        allclear();
        loop(i,m)
        {
            sff(a,b);
            a--,b--;
            graph[a].pb(b);
        }

        loop(i,n)
        {
            dfs(i,1<<i);
        }
        PRINT_CASE;
        cout<<dp_func((1<<n)-1)<<endl;
    }
    return 0;
}

Articulation Point


/*
         +-+ +-+ +-+
         |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             100007
#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>graph[MAX];
int n,m,cnt;
bool visited[MAX],artpoint[MAX];
int back_edge[MAX],dis[MAX],degroot,dfsnum,root;

void Find_Art(int u)
{
    visited[u]=1;
    dfsnum++;
    back_edge[u]=dis[u]=dfsnum;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!visited[v])
        {
            Find_Art(v);
            if(u==root)
            {
                degroot++;
                if(degroot>=2)
                {
                    artpoint[root]=1;
                }
            }

            back_edge[u]=min(back_edge[u],back_edge[v]);
            if(back_edge[v]>=dis[u] && u!=root)
            {
                artpoint[u]=1;
            }

        }
        else
            back_edge[u]=min(back_edge[u],dis[v]);
    }
}

void allclear()
{
    loop(i,n+5)
    {
        graph[i].clear();
        visited[i]=0;
        artpoint[i]=0;
        back_edge[i]=0;
        dis[i]=0;
    }
    cnt=dfsnum=degroot=0;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sff(n,m);
        allclear();
        int a,b;
        loop(i,m)
        {
            sff(a,b);
            graph[a].pb(b);
            graph[b].pb(a);
        }

        root=1;
        Find_Art(1);

        REP(i,1,n+1)
        if(artpoint[i]) cnt++;
        PRINT_CASE;
        cout<<cnt<<endl;
    }
    return 0;
}


Strongly Connected Component (SCC Kosaraju)


#include <bits/stdc++.h>

using namespace std;

vector<int>G[50],G_reverse[50];
int finishing_time,finish[50],visited[50];

int DFS(int n)
{
    finishing_time++;
    int i,j,k;
    visited[n]=1;
    for(i=0,j=G[n].size(); i<j; i++)
    {
        k=G[n][i];
        if(visited[k]==0)
            DFS(k);
    }
    finish[n]=++finishing_time;
    return 0;
}

int dfs2(int n)
{
    printf(" %d",n);
    visited[n]=1;
    int i,j,k;
    for(i=0,j=G_reverse[n].size(); i<j; i++)
    {
        k=G_reverse[n][i];
        if(visited[k]==0)
            dfs2(k);
    }
    return 0;
}

int main()
{
    int i,j,k,m,n,e;
    printf("Enter no of nodes in the graph:");
    cin>>n;
    printf("Enter no of edges in the graph:");
    cin>>e;
    
    while(e--)
    {
        cin>>i>>j;
        G[i].push_back(j);
        G_reverse[j].push_back(i);
    }
    
    memset(visited,0,sizeof visited);
    finishing_time=0;
    
    for(i=1; i<=n; i++)
    {
        if(visited[i]==0)
        {
            DFS(i);
        }
    }
    
    vector<pair<int,int> >v;
    
    for(i=1; i<=n; i++)
    {
        v.push_back(make_pair(finish[i],i));
    }
    
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());//sorting in ascending order.
    
    int SCC=0;
    
    memset(visited,0,sizeof visited);
    
    for(i=0; i<n; i++)
    {
        m=v[i].second;
        if(!visited[m])
        {
            printf("Component of SCC no %d:",++SCC);
            dfs2(m);
            printf("\n");
        }
    }
    
    return 0;
}

Minimum Spanning Tree (Kruskal)


#include <bits/stdc++.h>

#define MAXN 100000

using namespace std;

struct edge
{
    int u,v,w;
    bool operator < ( const edge& p ) const
    {
        return w < p.w;
    }
};

int pr[MAXN];
vector<edge>e;

int find(int r)
{
    return pr[r]= (pr[r]==r) ? r:  find(pr[r]);
}

int mst(int n)
{
    sort(e.begin(),e.end());
    for(int i=1; i<=n; i++)pr[i]=i;

    int count=0,s=0;
    for(int i=0; i<(int)e.size(); i++)
    {
        int u=find(e[i].u);
        int v=find(e[i].v);
        if(u!=v)
        {
            pr[u]=v;
            count++;
            s+=e[i].w;
            if(count==n-1) break;
        }
    }
    return s;
}

int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        edge get;
        get.u=u;
        get.v=v;
        get.w=w;
        e.push_back(get);
    }
    cout<<mst(n)<<endl;
    return 0;
}

Segment Tree


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <map>

#define ll long long
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)

#define MAX 1000
using namespace std;

int ara[MAX];
int tree[MAX*3];

void init(int node,int b,int e)
{
    if(b==e)
       {
         tree[node]=ara[b];
         return;
       }
    int mid=(b+e)/2;
    int left=2*node;
    int right=2*node+1;
    init(left,b,mid);
    init(right,mid+1,e);
    tree[node]=tree[left]+tree[right];
}

int query(int node,int b,int e,int i, int j)
{
    if(i>e || j<b)
        return 0;
    if(b>=i && e<=j)
        return tree[node];
    int left=node*2;
    int right=node*2+1;
    int mid=(b+e)/2;
    int p1=query(left,b,mid,i,j);
    int p2=query(right,mid+1,e,i,j);
    return p1+p2;
}

void update(int node,int b,int e, int i, int new_value)
{
    if(i>e || i<b)
        return;
    if(b>=i && e<=i)
    {
        tree[node]=new_value;
        return;
    }
    int left=node*2;
    int right=node*2+1;
    int mid=(b+e)/2;
    update(left,b,mid,i,new_value);
    update(right,mid+1,e,i,new_value);
    tree[node]=tree[left]+tree[right];
}

int main()
{
    freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>ara[i];
    init(1,1,n);
    cout<<query(1,1,n,1,4)<<endl;
    update(1,1,n,2,0);
    cout<<query(1,1,n,2,2)<<endl;
    update(1,1,n,2,1);
    cout<<query(1,1,n,2,3)<<endl;
    return 0;
}

Light OJ 1063 – Ant Hills

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


/*
         +-+ +-+ +-+
         |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             100007
#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>graph[MAX];
int n,m,cnt;
bool visited[MAX],artpoint[MAX];
int back_edge[MAX],dis[MAX],degroot,dfsnum,root;

void dfs(int u)
{
    visited[u]=1;
    dfsnum++;
    back_edge[u]=dis[u]=dfsnum;
    loop(i,SZ(graph[u]))
    {
        int v=graph[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(u==root) degroot++;
            if(degroot>=2)
            {
                artpoint[root]=1;
            }

            back_edge[u]=min(back_edge[u],back_edge[v]);
            if(back_edge[v]>=dis[u] && u!=root)
            {
                artpoint[u]=1;
            }

        }
        else
            back_edge[u]=min(back_edge[u],dis[v]);
    }
}

void allclear()
{
    loop(i,n+5)
    {
        graph[i].clear();
        visited[i]=0;
        artpoint[i]=0;
        back_edge[i]=0;
        dis[i]=0;
    }
    cnt=dfsnum=degroot=0;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sff(n,m);
        allclear();
        int a,b;
        loop(i,m)
        {
            sff(a,b);
            graph[a].pb(b);
            graph[b].pb(a);
        }
       
        root=1;
        dfs(1);
        
        REP(i,1,n+1)
        if(artpoint[i]) cnt++;
        PRINT_CASE;
        cout<<cnt<<endl;
    }
    return 0;
}