Showing posts with label Graph. Show all posts
Showing posts with label Graph. Show all posts

Thursday, March 1, 2018

Happy Vertices (Python3)

def dfs(u):
    global ans
    for i in range(len(adj[u])):
        v = adj[u][i]
        if not visited[v]:
            visited[v] = len(adj[v])-1
            if visited[v] > visited[u]:
                ans += 1
            dfs(v)



n,m = map(int,input().split())
ans = 0
adj = []
for i in range(n+1): adj.append([])
for i in range(m):
    x,y = map(int,input().split())
    adj[x].append(y)
    adj[y].append(x)

visited = [0]*(n+1)
for i in range(1,n+1):
    if not visited[i]:
        visited[i] = len(adj[i])
        dfs(i)
print(ans)

Prison Break

#include< bits/stdc++.h >
using namespace std;

int data[21][21];

int dfs(int u,int v,int n){
    int ans = 0;

    if( u == n and v == n) return 1;
    data[u][v] = 1;
    if(u - 1 >= 1 and data[u-1][v] == 0)
         ans += dfs(u-1,v,n);
    if(v - 1 >= 1 and data[u][v-1] == 0)
         ans += dfs(u,v-1,n);
    if(u+1 <= n and data[u+1][v] == 0)
         ans += dfs(u+1,v,n);
    if(v+1 <= n and data[u][v+1] == 0)
         ans += dfs(u,v+1,n);
    data[u][v] = 0;
 return ans ;
}
int main()
{
    freopen("in.txt","r",stdin);
    int n, t;
    cin >> t;
    while(t--){
    cin >> n;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
           cin >> data[i][j];

    if(data[1][1] == 1 and data[n][n] == 1) cout << 0 << endl;
    else cout << dfs(1,1,n) << endl;

    }
    return 0;
}

Wednesday, February 28, 2018

Happy Vertices

/*                             Name : Arif Khan
                                Date : 1/3/18
                                Problem Title: Happy Vertices
Problem Link : https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/happy-vertices/

*/

#include< bits/stdc++.h >
using namespace std;

vector < int >adj[100001];
vector < int > visited(100001,0);
int ans = 0;

void dfs(int u){

    for(int i = 0; i < adj[u].size() ; i++){
        int v = adj[u][i];
        if(visited[v] == 0){
                visited[v] = adj[v].size() - 1;
            if(visited[v] > visited[u]) ans++;
            dfs(v);
        }
    }
}

int main()
{
    int n , m , x , y;
    cin >> n >> m;
    for(int i = 1; i <= m ; i++){
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    for(int i = 1; i <= n; i++){
        if(visited[i] == 0){
            visited[i] = adj[i].size();
            dfs(i);
        }
    }
    cout << ans << endl;
    return 0;
}

Bishu and his Girlfriend

#include < bits/stdc++.h >
using namespace std;

vector< int > adj[1001];
vector< bool >visited(1000,false);
int dsit[1001];

void dfs(int s)
{
    memset(dsit,0,sizeof(dsit));

    stack < int > st;
    st.push(s);
    dsit[s] = 0;

    while(!st.empty()){
        int u = st.top();
        st.pop();
        for(int i = 0; i < adj[u].size() ; i++){
            int v = adj[u][i];

            if(v > dsit[u]+1){
                dsit[v] = dsit[u]+1;
                st.push(v);
            }
        }
    }
}

int main()
{
    int n,m;
    cin >> n;
    for(int i = 0; i < n-1; i++){
        int x,y;
        cin >> x >> y;
        // if undirected graph
        adj[x].push_back(y);
        adj[y].push_back(x); // if directed graph not needed this line
    }
    int res = INT_MAX,q,ans=0;
    cin >> q;
    int mal;
    dfs(1);
    for(int i = 0;i < q; i++){
        cin >> mal;
        if(dsit[mal] < res){
            res = dsit[mal];
            ans = mal;
        }else if(res == dsit[mal]){
            ans = min(ans,mal);
        }
      }
    cout << ans << endl;

    return 0;
}