Skip to content

Commit 4f2ef6c

Browse files
authored
Create uva336.cpp
1 parent 4c3604a commit 4f2ef6c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

uva336.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/******************************************************************************
2+
3+
Online C++ Compiler.
4+
Code, Compile, Run and Debug C++ program online.
5+
Write your code in this editor and press "Run" button to compile and execute it.
6+
7+
*******************************************************************************/
8+
9+
#include <iostream>
10+
#include <cstdio>
11+
#include <map>
12+
#include <vector>
13+
#include <queue>
14+
using namespace std;
15+
typedef vector<int> vi;
16+
17+
int main()
18+
{
19+
int n,c=0;
20+
while(scanf("%d", &n),n){
21+
map<int, vi> AdjList;
22+
while(n--){
23+
int a,b;
24+
scanf("%d %d", &a,&b);
25+
AdjList[a].push_back(b);
26+
AdjList[b].push_back(a);
27+
}
28+
int f, s;
29+
while(scanf("%d %d", &f, &s), f, s){
30+
c++;
31+
map<int,bool> vis;
32+
map<int, int> dist;
33+
queue<int> q;
34+
q.push(f);
35+
dist[f] = 0;
36+
vis[f] = 1;
37+
while(!q.empty()){
38+
int node = q.front(); q.pop();
39+
40+
for(int j =0; j < AdjList[node].size(); ++j){
41+
int u = AdjList[node][j];
42+
if (!vis[u]){
43+
vis[u] = 1;
44+
dist[u] = dist[node] + 1;
45+
q.push(u);
46+
47+
}
48+
}
49+
}
50+
int count = 0;
51+
for(std::map<int,int>::iterator iter = dist.begin(); iter != dist.end(); ++iter)
52+
{
53+
//printf("%d -> %d ,",iter->first, iter->second);
54+
if(iter->second > s){
55+
count ++;
56+
}
57+
58+
}
59+
printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", c,count,f,s);
60+
61+
}
62+
63+
}
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)