#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#define M 100003
using namespace std;
vector <int> node[M];
int cs = 1, mx, cnt;
void bfs(int src, int TTL, int cnt){
queue <int> container;
int visited[M] = {0}, ans = 1, power[M] = {0};
visited[src] = 1;
container.push(src);
while(!container.empty()){
int u = container.front();
int sz = node[u].size();
for(int i = 0; i < sz; i++){
int v = node[u][i];
if(!visited[v]){
visited[v] = 1;
power[v] = power[u]+1;
if(power[v] <= TTL)ans++;
container.push(v);
}
}
container.pop();
}
cout << "Case " << cs << ": " << (cnt - ans) << " nodes not reachable from node " << src << " with TTL = " << TTL << "." << endl;
}
int main(){
int N, E, x, y, src, TTL, m, num[M] = {0};
while(cin >> N ){
mx = 0, cnt = 0;
if(!N)break;
for(int i = 0; i < N; i++){
cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
m = max(x, y);
mx = max(mx, m);
}
for(int i = 0; i < mx+1; i++){
int sz = node[i].size();
if(sz > 0)cnt++;
}
while(cin >> src >> TTL){
if(!src && !TTL)break;
bfs(src, TTL, cnt);
cs++;
}
for(int i = 0; i <= M; i++)node[i].clear();
}
return 0;
}
#include <cstring>
#include <vector>
#include <queue>
#define M 100003
using namespace std;
vector <int> node[M];
int cs = 1, mx, cnt;
void bfs(int src, int TTL, int cnt){
queue <int> container;
int visited[M] = {0}, ans = 1, power[M] = {0};
visited[src] = 1;
container.push(src);
while(!container.empty()){
int u = container.front();
int sz = node[u].size();
for(int i = 0; i < sz; i++){
int v = node[u][i];
if(!visited[v]){
visited[v] = 1;
power[v] = power[u]+1;
if(power[v] <= TTL)ans++;
container.push(v);
}
}
container.pop();
}
cout << "Case " << cs << ": " << (cnt - ans) << " nodes not reachable from node " << src << " with TTL = " << TTL << "." << endl;
}
int main(){
int N, E, x, y, src, TTL, m, num[M] = {0};
while(cin >> N ){
mx = 0, cnt = 0;
if(!N)break;
for(int i = 0; i < N; i++){
cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
m = max(x, y);
mx = max(mx, m);
}
for(int i = 0; i < mx+1; i++){
int sz = node[i].size();
if(sz > 0)cnt++;
}
while(cin >> src >> TTL){
if(!src && !TTL)break;
bfs(src, TTL, cnt);
cs++;
}
for(int i = 0; i <= M; i++)node[i].clear();
}
return 0;
}
No comments:
Post a Comment