题目大意:在计算机网络中,每条信息都有一个TTL值,在信息到达一个节点时,TTL值首先减1,如果TTL为0,则丢弃该信息报文。给一个网络的配置,给定源点和TTL值,判断该网络中有多少节点不可到达。

  无权图(无向)上的单源最短路问题,可用BFS解决。

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
#define NODEN 35 map<int, int> vertex; void new_vertex(int x)
{
if (!vertex.count(x))
{
int t = vertex.size() + ;
vertex[x] = t;
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n, kase = ;
while (scanf("%d", &n) && n)
{
int x, y;
vertex.clear();
vector<int> G[NODEN];
for (int i = ; i < n; i++)
{
scanf("%d%d", &x, &y);
new_vertex(x);
new_vertex(y);
G[vertex[x]].push_back(vertex[y]);
G[vertex[y]].push_back(vertex[x]);
}
int s, ttl, cnt;
int dist[NODEN];
while (scanf("%d%d", &s, &ttl) && (s || ttl))
{
int st = vertex[s];
queue<int> q;
memset(dist, -, sizeof(dist));
dist[st] = ;
q.push(st);
cnt = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (dist[v] != -) continue;
dist[v] = dist[u] + ;
if (dist[v] > ttl) goto s;
q.push(v);
cnt++;
}
}
s: int ans = vertex.size() - cnt;
printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", ++kase, ans, s, ttl);
}
}
return ;
}

UVa 336 - A Node Too Far的更多相关文章

  1. babeljs源码

    babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...

  2. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  5. UVA 1151

    /* 题意:有n个点,现在需要联通所有,有q种套餐可以选择, 当然套餐之外也可以自己添加边,意为达到最短距离. 题意很明显,不知道需要使用哪一种套餐, 那么需要枚举每一种套餐的情况. 然后再进行对比. ...

  6. [题解]UVa 11082 Matrix Decompressing

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  7. UVA 12299 RMQ with Shifts(线段树:单点更新)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  9. Node.js的线程和进程

    http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本章的学习,能够 ...

随机推荐

  1. MaterialEditText 控件学习

    这个视图原始框架地址:https://github.com/rengwuxian/MaterialEditText 指导手册:http://www.rengwuxian.com/post/materi ...

  2. PAT (Advanced Level) 1101. Quick Sort (25)

    树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  3. 优化之sitemap+RSS

    RSS也叫聚合, RSS是在线共享内容的一种简易方式,也叫聚合内容,Really Simple Syndication. 通常在时效性比较强的网站或网络平台上应用RSS订阅功能可以更快速获取信息,网站 ...

  4. hdu_1007_Quoit Design(最近点对)

    题目连接:hdu_1007_Quoit Design 题意: 给你平面上的一些点,让你找出这些点的最近点对的距离 题解: 采用分治,达到O(nlognlogn)的时间复杂度就能艹过去了 #includ ...

  5. js的阻塞特性

    JS具有阻塞特性,当浏览器在执行js代码时,不能同时做其它事情,即<script>每次出现都会让页面等待脚本的解析和执行(不论JS是内嵌的还是外链的),JS代码执行完成后,才继续渲染页面. ...

  6. JSP内置对象--application对象(getRealPath(),getAttributeNames(),getContextPath())

    application对象是javax.servlet.ServletContext接口的实例化对象.是整个servlet的上下文,代表了整个web容器的操作. 常用方法: 1.java.lang.S ...

  7. TIdTCPClient 详解

    转发地址:http://blog.csdn.net/cowcga/article/details/6198382 关于TIdTCPClient的几种方法 收藏 其实Indy比较简单,但是可以提供的方法 ...

  8. hrbustoj 1125 循环小数 II(小数变分数+极限思想)

    #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #i ...

  9. POJ 3648 Wedding

    2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以. A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在:B在新郎一 ...

  10. Linux操作系统入门教程

    http://www.linuxidc.com/Linux/2015-07/120815p8.htm