UVa 336 - A Node Too Far
题目大意:在计算机网络中,每条信息都有一个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的更多相关文章
- babeljs源码
babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- UVA 1151
/* 题意:有n个点,现在需要联通所有,有q种套餐可以选择, 当然套餐之外也可以自己添加边,意为达到最短距离. 题意很明显,不知道需要使用哪一种套餐, 那么需要枚举每一种套餐的情况. 然后再进行对比. ...
- [题解]UVa 11082 Matrix Decompressing
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- UVA 12299 RMQ with Shifts(线段树:单点更新)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- Node.js的线程和进程
http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本章的学习,能够 ...
随机推荐
- mysql笔记6之数据类型
1 区别一: varchar:可变长度的字符串.根据添加的数据长度决定占用的字符数 char:固定长度的字符串 2区别二 int:没有限制 int(4):限制为4 3 区别三: 日期: date ...
- GB2312、Unicode编码等
抛出问题: 我在CPP文件中,打算输出一行阿拉伯字符:
- Codeforces Round #363 (Div. 2) C. Vacations(DP)
C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 改写BlogEngine.NET头像上传实现方式(使用baidu.flash.avatarMaker)
baidu.flash.avatarMaker 需要资源文件和javascript类库: 1 2 3 4 5 6 7 需要应用的script library: <scriptsrc=" ...
- emacs redo
c / c/ cg c/ cg c/ tramp: /sudo::/usr/
- Request 地址栏传值
request页面 protected void btnSearch_Click(object sender, EventArgs e) { Response.Redirect("Reque ...
- IMosaicWorkspaceExtensionHelper
Provides a helper for working with a mosaic dataset workspace extension. Product Availability Availa ...
- zf-关于触摸屏子系统(查询机)的页面
这个页面 一般是 touch/index.jsp 这个要记下来. -- 今天有个任务 是把查询机的UI页面给换掉
- .h .m切换
快捷键是:command + control +[↑|↓]
- python+mysql抓取百度新闻的标题存到数据库
#!usr/bin/python# -*- coding:utf-8 -*-import urllib2 import re import MySQLdb class BaiDuNews: def _ ...