please give me some critical inputs or help me to find the bugs.
- Code: Select all
ACC
Moderator: Board moderators
ACC
#include<iostream>
#include<cstdio>
#include<queue>
#define MAX 100
using namespace std;
int path[MAX][MAX];
int distinct[MAX];
int flag[MAX];
int n,index;
int ccount;
long dist[MAX];
queue<int>map;
void initiation(){
int i,j;
for(i=0; i<MAX; i++){
for(j=0; j<MAX; j++){
path[i][j] = 0;
}
distinct[i] = 0;
flag[i] = 0;
}
}
void distini(){
int i;
for(i=0; i<MAX; i++)
dist[i] = 2147483647;
}
void bfs(int start){
int temp;
int flag = 0;
while(!map.empty()){
map.pop();
}
map.push(start);
dist[start] = 0;
while(!map.empty()){
temp = map.front();
map.pop();
for(int i=0; i<index; i++){
if(distinct[i] != temp){
if(path[temp][distinct[i]]==1){
if(dist[distinct[i]] > dist[temp] + 1){
dist[distinct[i]] = dist[temp] + 1;
map.push(distinct[i]);
}
}
}
}
}
}
int main(){
int i,j;
int a,b;
ccount = 0;
int c = 0;
int init = 1;
int nrc;
initiation();
while(scanf("%d",&n)==1&&n){
index = 0;
for(i=0; i<n; i++){
scanf("%d %d",&a,&b);
if(flag[a] == 0){
distinct[index] = a;
flag[a] = 1;
index++;
}
if(flag[b] == 0){
distinct[index] = b;
flag[b] = 1;
index++;
}
path[a][b] = path[b][a] = 1;
}
int dest,ttl;
while(scanf("%d %d",&dest,&ttl)==2){
if(dest==0 && ttl==0)break;
distini();
bfs(dest);
nrc = 0;
for(j=0; j<index; j++){
if(dist[distinct[j]]>ttl){
nrc++;
}
}
printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",init,nrc,dest,ttl);
init++;
}
for(j=0; j<MAX; j++){distinct[j] = 0;flag[j] = 0;}
}
return 0;
}
graph "start = 1 TTL = 5" { 100 [fillcolor=blue,style=filled]; 2 [fillcolor=blue,style=filled]; 3 [fillcolor=blue,style=filled]; 4 [fillcolor=blue,style=filled]; 5 [fillcolor=blue,style=filled]; 6 [fillcolor=blue,style=filled]; 7 [fillcolor=blue,style=filled]; 8 [fillcolor=blue,style=filled]; 9 [fillcolor=blue,style=filled]; 1000 [fillcolor=blue,style=filled]; 100100 [fillcolor=blue,style=filled]; 1002 [fillcolor=red,style=filled]; 1003 [fillcolor=red,style=filled]; 1004 [fillcolor=blue,style=filled]; 1005 [fillcolor=blue,style=filled]; 1006 [fillcolor=blue,style=filled]; 1007 [fillcolor=blue,style=filled]; 1008 [fillcolor=blue,style=filled]; 1009 [fillcolor=blue,style=filled]; 20 [fillcolor=blue,style=filled]; 2100 [fillcolor=blue,style=filled]; 22 [fillcolor=blue,style=filled]; 23 [fillcolor=blue,style=filled]; 100 -- 2; 100 -- 4; 100 -- 7; 2 -- 3; 3 -- 4; 3 -- 9; 5 -- 6; 5 -- 22; 6 -- 7; 7 -- 8; 7 -- 2100; 8 -- 9; 8 -- 1006; 8 -- 100100; 9 -- 1000; 1000 -- 100100; 1000 -- 20; 1002 -- 1003; 1003 -- 1004; 1004 -- 1005; 1005 -- 1006; 1006 -- 1007; 1007 -- 1008; 1008 -- 1009; 1009 -- 20; 20 -- 2100; 2100 -- 22; 22 -- 23;
}
28
100 2 2 3 3 4 4 100
5 6 6 7 7 8 8 9 9 1000 1000 100100 9 3 100 7
1002 1003 1003 1004 1004 1005 1005 1006 1006 1007 1007 1008 1008 1009 1009 20 20 2100 2100 22 22 23
1006 8 1000 20 22 5 7 2100 8 100100
100 5 7 3 1005 3 1004 5 2100 6 1000 99 0 0
0
Case 1: 2 nodes not reachable from node 100 with TTL = 5.
Case 2: 4 nodes not reachable from node 7 with TTL = 3.
Case 3: 12 nodes not reachable from node 1005 with TTL = 3.
Case 4: 6 nodes not reachable from node 1004 with TTL = 5.
Case 5: 1 nodes not reachable from node 2100 with TTL = 6.
Case 6: 0 nodes not reachable from node 1000 with TTL = 99.
#include<stdio.h>
int a,t,front,rear,level,nc,q[100000],i,j,k,l,m,n,x[10000][10000],y[10000],z[100000],state[10000];
int main()
{
scanf("%d",&nc);
for(l=1;nc!=0;l+=j-1)
{
for(i=0;i<10000;i++) x[i][0]=1;
t=0;
for(i=1;i<=nc;i++)
{
scanf("%d%d",&m,&n); //I have store all node I can go directly from a node(index) in two dimentional array
//x[m][0] indicates how many node I can go from node m.
if(x[m][0]==1) t++;
x[m][x[m][0]++]=n;
if(x[n][0]==1) t++;
x[n][x[n][0]++]=m;
}
scanf("%d%d",&y[1],&z[1]);
for(k=1;y[k]!=0||z[k]!=0;) scanf("%d%d",&y[k],&z[++k]);
for(j=1;j<k;j++)
{
for(i=0;i<10000;i++) state[i]=0; //initializing the state
rear=1;
front=1;
level=1;
a=0;
q[rear]=y[j];
n=y[j];
for(i=1;rear>=front&&a!=z[j];i++) //traversing the node.
{
state[n]++;
for(m=1;m<x[n][0];m++)
{
if(state[x[n][m]]==0)
{
q[++rear]=x[n][m],state[x[n][m]]++;
}
}
if(front==level) level=rear,a++;
n=q[++front];
}
printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",l+j-1,t-rear,y[j],z[j]);
}
scanf("%d",&nc);
}
return 0;
}
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main()
{
int nc,nEdge,a,b,i,root,ttl,cases=1;
while(1)
{
vector<int>edges[10000];
cin>>nc;
if(nc==0)break;
for(i=0;i<nc;i++)
{
cin>>a>>b;
if(a==0&&b==0)break;
edges[a].push_back(b);
edges[b].push_back(a);
}
while(1)
{
int v,visited[10000]={0},dist[10000]={0},u,count=0,flag=0;
cin>>root>>ttl;
if(root==0&&ttl==0)
{
flag=1;
break;
}
queue<int>q;
q.push(root);
while(!q.empty())
{
v=q.front();
visited[v]=1;
for(i=0;i<edges[v].size();i++)
{
u=edges[v][i];
if(!visited[u])
{
visited[u]=1;
dist[u]=dist[v]+1;
if(dist[u]>ttl)count++;
q.push(u);
}
}
q.pop();
}
if(!flag)
cout<<"Case "<<cases<<": "<<count<<" nodes not reachable from node "<<root<<" with TTL = "<<ttl<<"."<<'\n';
cases++;
}
}
return 0;
}#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<deque>
#include<queue>
#include<map>
#include<utility>
#define L long long int
#define U unsigned long long int
using namespace std;
int arr[111][111];
map <int,int>m;
vector < pair<int,int> >v;
main()
{
int t = 1;
while(1)
{
int n,p=0;
cin>>n;
m.clear();
v.clear();
if(!n)
break;
for(int i = 0;i<n;i++)
{
int a,b;
cin>>a>>b;
if(!(a|b))
break;
//cout<<a<<b<<endl;
if(m.find(a) == m.end())
m.insert(make_pair(a,p++));
if(m.find(b) == m.end())
m.insert(make_pair(b,p++));
v.push_back(make_pair(a,b));
}
for(int i = 0;i<m.size();i++)
{
for(int j = 0;j<m.size();j++)
arr[i][j] = -1;
}
for(int i = 0;i<n;i++)
{
int a = m.find(v[i].first)->second ;
int b = m.find(v[i].second)->second;
arr[a][b] = 1;
arr[b][a] = 1;
}
for(int k = 0;k<m.size();k++)
{
for(int i= 0;i<m.size();i++)
{
for(int j = 0;j<n;j++)
{
if(i == j)
arr[i][j] = 0;
else if(arr[i][j] == -1)
{
if(arr[i][k] != -1 && arr[k][j] != -1)
arr[i][j] = arr[i][k] + arr[k][j];
}
else
{
if(arr[i][k] != -1 && arr[k][j] != -1)
arr[i][j] = min(arr[i][j], arr[i][k] + arr[k][j]);
}
}
}
}
int src,ttl;
while(1)
{
cin>>src>>ttl;
if(!(src|ttl))
break;
int c = 0;
for(int i = 0;i<m.size();i++)
{
if(src == i)
continue;
int x = m.find(src)->second;
if(arr[x][i] > ttl || arr[x][i] == -1)
c++;
}
printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",t++,c,src,ttl);
}
}
}

if(src == i)
continue;
Users browsing this forum: No registered users and 0 guests