UVA Solution 336 - A Node Too Far - Solution in C, C++ | Volume 3 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 5, 2017

UVA Solution 336 - A Node Too Far - Solution in C, C++ | Volume 3

UVA Solution 336 - A Node Too Far - Solution in C, C++ | Volume 3


UVA Online Judge Solution 336 - A Node Too Far | Volume 3
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=272

Problem Name: 336 - A Node Too Far
Problem Number : UVA - 336 - A Node Too Far
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 336 - A Node Too Far Code in C, CPP:


#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int main() {
    int n, test = 0, TTL;
    while(scanf("%d", &n) == 1 && n) {
        int i, j, size = 0, x, y;
        map<int, int> r;
        vector<int> Link[40];
        while(n--) {
            scanf("%d %d", &i, &j);
            x = r[i];
            if(x == 0) {
                r[i] = ++size;
                x = size;
            }
            y = r[j];
            if(y == 0) {
                r[j] = ++size;
                y = size;
            }
            Link[x].push_back(y);
            Link[y].push_back(x);
        }
        while(scanf("%d %d", &x, &TTL) == 2) {
            if(x == 0 && TTL == 0)
                break;
            int used[40] = {}, dp[40] = {}, cnt = 0;
            y = x, x = r[x];
            queue<int> Q;
            Q.push(x), used[x] = 1, dp[x] = 0;
            while(!Q.empty()) {
                x = Q.front();
                if(dp[x] > TTL)
                    break;
                cnt++;
                Q.pop();
                for(vector<int>::iterator i = Link[x].begin(); i != Link[x].end(); i++) {
                    if(used[*i] == 0) {
                        used[*i] = 1;
                        dp[*i] = dp[x]+1;
                        Q.push(*i);
                    }
                }
            }
            printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", ++test, size-cnt, y, TTL);
        }
    }
    return 0;
}

No comments:

Post a Comment