Problem Type : Graph(BFS)
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#define MAX 99900
using namespace std;
vector<long>vcc[MAX];
map<long,long>mymap;
long int color[MAX],cost[MAX] ,node,start,cost1,visited[MAX],cas=0,dif[MAX];
void bfs(long int start,long int c)
{
cas++;
// printf("%ld\n",c);
long int i,j,u;
color[start]=1;
cost[start]=0;
queue<long int>Q;
long int num=0;
Q.push(start);
while(!Q.empty())
{
u=Q.front();
Q.pop();
long int len = vcc[u].size();
for(i=0; i<len; i++)
{
if(color[vcc[u][i]]!=1)
{
cost[vcc[u][i]]=cost[u]+1;
if(cost[vcc[u][i]]>c)
{
break;
}
num++;
Q.push(vcc[u][i]);
color[vcc[u][i]]=1;
}
}
}
printf("Case %ld: %ld nodes not reachable from node %ld with TTL = %ld.\n",cas,(node-num-1),start,c);
}
int main()
{
long int i,j,k,l,n,m,t;
while(scanf("%ld",&t)==1)
{
if(t==0)
{
break;
}
node=0;
for(i=0; i<MAX; i++)
{
vcc[i].clear();
}
for(i=1; i<=t; i++)
{
scanf("%ld %ld",&n,&m);
if(dif[n]!=1)
{
dif[n]=1;
node=node+1;
}
if(dif[m]!=1)
{
dif[m]=1;
node=node+1;;
}
vcc[n].push_back(m);
vcc[m].push_back(n);
}
while(scanf("%ld %ld",&start,&cost1)==2)
{
if(start==0 && cost1==0)
{
break;
}
else
{
memset(cost,0,sizeof(cost));
memset(color,0,sizeof(color));
bfs(start,cost1);
}
}
memset(dif,0,sizeof(dif));
}
}
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#define MAX 99900
using namespace std;
vector<long>vcc[MAX];
map<long,long>mymap;
long int color[MAX],cost[MAX] ,node,start,cost1,visited[MAX],cas=0,dif[MAX];
void bfs(long int start,long int c)
{
cas++;
// printf("%ld\n",c);
long int i,j,u;
color[start]=1;
cost[start]=0;
queue<long int>Q;
long int num=0;
Q.push(start);
while(!Q.empty())
{
u=Q.front();
Q.pop();
long int len = vcc[u].size();
for(i=0; i<len; i++)
{
if(color[vcc[u][i]]!=1)
{
cost[vcc[u][i]]=cost[u]+1;
if(cost[vcc[u][i]]>c)
{
break;
}
num++;
Q.push(vcc[u][i]);
color[vcc[u][i]]=1;
}
}
}
printf("Case %ld: %ld nodes not reachable from node %ld with TTL = %ld.\n",cas,(node-num-1),start,c);
}
int main()
{
long int i,j,k,l,n,m,t;
while(scanf("%ld",&t)==1)
{
if(t==0)
{
break;
}
node=0;
for(i=0; i<MAX; i++)
{
vcc[i].clear();
}
for(i=1; i<=t; i++)
{
scanf("%ld %ld",&n,&m);
if(dif[n]!=1)
{
dif[n]=1;
node=node+1;
}
if(dif[m]!=1)
{
dif[m]=1;
node=node+1;;
}
vcc[n].push_back(m);
vcc[m].push_back(n);
}
while(scanf("%ld %ld",&start,&cost1)==2)
{
if(start==0 && cost1==0)
{
break;
}
else
{
memset(cost,0,sizeof(cost));
memset(color,0,sizeof(color));
bfs(start,cost1);
}
}
memset(dif,0,sizeof(dif));
}
}
No comments:
Post a Comment