## 336 - A Node Too Far

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Yusuf_sust
New poster
Posts: 2
Joined: Wed Dec 16, 2015 3:03 am

### Re: 336 - A Node Too Far

Why WA anybody help me. my code
#include <bits/stdc++.h>
using namespace std;

int visit[100000], dist[100000];

void BFS(int start, vector <int> *a);

int main()
{
int n, t = 1;
while(scanf("%d", &n) && n)
{
int j = 0;
vector <int> a[100000];
set <int> s;
map <long, int> m;
for(int i = 0; i < n; ++i)
{
long f, t;
cin>>f>>t;
if(m[f] == 0)
m[f] = ++j;
if(m[t] == 0)
m[t] = ++j;
s.insert(m[f]);
s.insert(m[t]);
a[m[f]].push_back(m[t]);
a[m[t]].push_back(m[f]);
}
long x, y;
while(scanf("%d %d", &x, &y) && x || y)
{
int sum = 0;
BFS(m[x],a);
set <int> :: iterator it;
for(it = s.begin(); it != s.end(); it++)
{
if(dist[*it] > y)
sum++;
}
printf("Case %d: %d nodes not reachable from node %ld with TTL = %ld.\n", t++, sum, x, y);
memset(visit, 0, sizeof(visit));
memset(dist, 0, sizeof(dist));

}
}

return 0;
}

void BFS(int start, vector <int> *a)
{
visit[start] = 1;
dist[start] = 0;
queue <int> p;
p.push(start);
while(!p.empty())
{
int u = p.front();
p.pop();
for(int i = 0; i < a.size(); i++)
{
int v = a;
if(visit[v] == 0)
{
visit[v] = 1;
dist[v] += dist + 1;
p.push(v);
}
}
}
}

nayon31
New poster
Posts: 2
Joined: Tue Nov 24, 2015 12:41 am

### Re: 336 - A Node Too Far

getting Runtime Error please help

#include<iostream>
#include<queue>
using namespace std;
long long int arr[100],arr2[100],sorted[100],graph[100][100],compressed[100];
long long int vis[100];
long long int d[100];
long long int node,edges,k;
long long int TTL,coun;
void bfs(int s)
{
queue<int>q;
q.push(s);
vis[s]=1;
d[s]=0;
while(!q.empty())
{
int top=q.front();q.pop();
for(int i=0;i<k;i++)
{
if(graph[top]==1&&vis==0)
{
vis=1;
d=d[top]+1;
q.push(i);
}
}
}
return;
}
int main()
{
int ca=0;
while(1)
{
for(int i=0;i<=100;i++)
{
arr=0;
arr2=0;
compressed=0;
sorted=0;

for(int x=0;x<=100;x++)graph[x]=0;

}
k=0;
cin>>edges;
if(edges==0)
break;
int j=0;
for(int i=1;i<=edges;i++)
{
int n1,n2;
cin>>n1>>n2;
arr[j]=n1;
arr[j+1]=n2;
j+=2;
}
for(int i=0;i<j;i++)
arr2=arr[i];
for(int i=0;i<j;i++)
{
for(int m=i;m<j;m++)
{
if(arr[i]>arr[m])
{
int temp=arr[i];
arr[i]=arr[m];
arr[m]=temp;
}
}
}
long long int mini=-10000;
for(int i=0;i<j;i++)
{

if(arr[i]>mini)
{
mini=arr[i];
sorted[k]=mini;
k++;
}

}
for(int i=0;i<j;i++)
{
for(int l=0;l<k;l++)
{
if(arr2[i]==sorted[l])
{compressed[i]=l;

break;
}
}
}
for(int i=0;i<j;i+=2)
{
int n3=compressed[i];
int n4=compressed[i+1];
graph[n3][n4]=1;
graph[n4][n3]=1;
//cout<<n3<<" "<<n4<<" "<<graph[n3][n4]<<endl;
}
int s;
while(1)
{
for(int i=0;i<k;i++)
{
vis[i]=0;
d[i]=0;
}
cin>>s>>TTL;
if(s==0&&TTL==0)
break;
int a;
int y=0;
for(int i=0;i<k;i++)
{
if(sorted[i]==s)
{
a=i;
break;
}
y++;
}
ca++;
if(y==k){
coun=k;
k++;
}

else{
bfs(a);

coun=0;
for(int i=0;i<k;i++)
{
if(d[i]>TTL||vis[i]==0)
{
coun++;
}
}
}
cout<<"Case "<<ca<<": "<<coun<<" nodes not reachable from node "<<s<<" with TTL = "<<TTL<<"."<<endl;
}
}
return 0;
}