Fiber Network
Time
Limit:
 1000MS
  Memory Limit: 65536K
Total Submissions: 3125   Accepted: 1436
Description
Several startup companies have decided to build a better Internet, called the "FiberNet". They have already installed many nodes that act as routers all around the world. Unfortunately, they started to quarrel about the connecting lines, and ended up with every company laying its own set of cables between some of the nodes. 
Now, service providers, who want to send data from node A to node B are curious, which company is able to provide the necessary connections. Help the providers by answering their queries.
Input
The input contains several test cases. Each test case starts with the number of nodes of the network n. Input is terminated by n=0. Otherwise, 1<=n<=200. Nodes have the numbers 1, ..., n. Then follows a list of connections. Every connection starts with two numbers A, B. The list of connections is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the unidirectional connection, respectively. For every connection, the two nodes are followed by the companies that have a connection from node A to node B. A company is identified by a lower-case letter. The set of companies having a connection is just a word composed of lower-case letters. 
After the list of connections, each test case is completed by a list of queries. Each query consists of two numbers A, B. The list (and with it the test case) is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the query. You may assume that no connection and no query contains identical start and end nodes.
Output
For each query in every test case generate a line containing the identifiers of all the companies, that can route data packages on their own connections from the start node to the end node of the query. If there are no companies, output "-" instead. Output a blank line after each test case.
Sample Input
3
1 2 abc
2 3 ad
1 3 b
3 1 de
0 0
1 3
2 1
3 2
0 0
2
1 2 z
0 0
1 2
2 1
0 0
0

Sample Output

ab
d
- z
-

题意:给出n个点,然后给出边a,b ,str代表str中的这些字母可以保证a到b的联通,然后有多组询问u和v,问u到v可以由那些字母保证连通性,若没有输出-

分析:一共26个字母可以使用位运算,g[a][b]代表那些可以联通,用传递闭包即可
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-8
#define inf 0x3f3f3f3f
#define M 250
using namespace std;
int g[M][M],vis[M];
char str[M];
int main()
{
int n,i,a,b,c,j,k,kk=0;
while(scanf("%d",&n),n)
{
memset(g,0,sizeof(g));
memset(vis,0,sizeof(vis));
while(scanf("%d%d",&a,&b),a||b)
{
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
c=str[i]-'a';
g[a][b]|=(1<<c);
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
g[i][j]|=(g[i][k]&g[k][j]);
}
}
}
if(kk)
printf("\n");
kk++;
while(scanf("%d%d",&a,&b),a||b)
{
int flag=0;
for(i=0;i<26;i++)
{
if(g[a][b]&(1<<i))
{
printf("%c",i+'a');
flag++;
}
}
if(flag)
printf("\n");
else
printf("-\n");
}
}
return 0;
}

传递闭包+二进制位运算+floyd(poj2570)的更多相关文章

  1. Japan 2005 Domestic Cleaning Robot /// BFS 状压 二进制位运算 结构体内构造函数 oj22912

    题目大意: 输入w h,接下来输入h行w列的图 ' . ':干净的点:  ' * ' :垃圾:  ' x ' : 墙:  ' o ' : 初始位置: 输出 清理掉所有垃圾的最短路径长度 无则输出-1 ...

  2. POJ--2570--Fiber Network【floyd+位运算】

    题意:一些公司决定搭建一些光纤网络.单向的,假设从第一点到第二点,有ab两个公司能够搭建,第二点到第三点有ac两个公司能够搭建,第一点到第三点有d公司能够搭建,则第一点到第三点有a.d两个公司能够搭建 ...

  3. 【BZOJ2208】【JSOI2010】连通数 传递闭包

    题目描述 定义一个图的连通度为图中可达顶点对的数目.给你一个\(n\)个点的有向图,问你这个图的连通度. \(n\leq 2000,m\leq n^2\) 题解 一个很简单的做法就是传递闭包:像flo ...

  4. 位运算 2013年山东省赛 F Alice and Bob

    题目传送门 /* 题意: 求(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1) 式子中,x的p次方的系数 二进制位运算:p ...

  5. Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

    1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved ...

  6. 单词words

    论一类脑筋急转弯题和奇技淫巧题的解题技巧 [题意] 给定n个长为m且只包含xyz的字符串,定义两个字符串的相似程度为它们对应位置相同字符个数(比如xyz和yyz的相似程度为2,后两位相同),分别求出相 ...

  7. 收集一些关于OI/ACM的奇怪的东西……

    一.代码: 1.求逆元(原理貌似就是拓展欧几里得,要求MOD是素数): int inv(int a) { if(a == 1) return 1; return ((MOD - MOD / a) * ...

  8. Java学习总结(二)----Java语言基础

    1.     Java语言基础 2.1 关键字 定义:被java语言赋予特殊含义的单词 特点:关键字中的字母都为小写 用于定义数据类型的关键字 class,interface,byte,short,i ...

  9. Java 关键字、标识符、注释、常量与变量、数据类型,算术、赋值、比较、逻辑、位、三元运算符和流程控制、break、continue【3】

    若有不正之处,请多多谅解并欢迎批评指正,不甚感激.请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing/ ...

随机推荐

  1. heapsort

    Introduction to Algorithms Third Edition The (binary) heap data structure is an array object that we ...

  2. php javascript C 变量环境 块级作用域

    <?php $w = 'w'; $wb = '123'.$w; $w = 'ww'; echo $wb; if(TRUE){ $wd = '123wd'; } echo $wd; if(FALS ...

  3. [转]GPS纠偏算法,适用于google,高德体系的地图

    此文是转的,算法没验证过,只是记录一下. GPS纠偏算法,适用于google,高德体系的地图,精确度还比较高.我试了一下比高德本身的纠偏还精确点. /** * gps纠偏算法,适用于google,高德 ...

  4. 【转】下载量最高的 100 个 Laravel 扩展包推荐

    说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...

  5. AndEngine

    AndEngine http://www.oschina.net/question/54100_16765

  6. zabbix basic concept

    tomcat/mysql/hadoop http://www.linuxidc.com/Linux/2014-06/103776p2.htm http://www.aikaiyuan.com/2993 ...

  7. 关于cocoa框架,你所要知道的一切(苹果官方文档,cocoa框架核心竞争力,必须收藏!)

    https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Accessib ...

  8. 【Android开发学习笔记】【高级】【随笔】插件化——Activity生命周期

    前言 如同第一章我们说的,宿主程序通过 dexclassloader 将插件的类加载进来,然后通过反射去调用它的方法,这样Activity就被当成了一个普通的类来执行了,因此系统不再接管它的生命周期, ...

  9. Android生命周期详细说明

    提供两个关于Activity的生命周期模型图示帮助理解:                                           图1 图2 从图2所示的Activity生命周期不难看出, ...

  10. Python PIL ImageDraw 和ImageFont模块学习

    ImageDraw 新建一个空白图片为本文作示例,新建空白文件的方法 见Image模块,Image.new: import Image blank = Image.new("RGB" ...