Fiber Network
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3536 | Accepted: 1616 |
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.
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.
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 -
题意:
给出一些边:以0 0结束,每一条边可以由一些公司承担修理,这些公司用二十六个小写字母表示
每一个字母用一个二十六位的字母表示,如:
a 0000000000000000000000001
b 0000000000000000000000010
c 0000000000000000000000100
则abc表示为 0000000000000000000000111(由 | 运算可以由单独的abc二进制得到abc总的二进制)
这里需要对 | & 这两种运算了解
1 & 1 = 1
0 & 1 = 0
0 & 0 = 0
1 | 1 = 1
0 | 1 = 1
0 | 0 = 0
因此当:
一号到二号 000000000000000111
二号到三号 000000000000001001
则由&运算知道:
一号到三号 000000000000001001
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int graph[210][210];
int main()
{
int n;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
graph[i][j]=i==j?INF:0;
int u,v;
while(scanf("%d%d",&u,&v),u+v)
{
char str[30];
scanf("%s",str);
int len=strlen(str);
int temp=0;
for(int i=0;i<len;i++)
temp=temp|(1<<(str[i]-'a'));
graph[u][v]=temp;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
graph[i][j]|=graph[i][k]&graph[k][j];
//上叙等式:原有的字母依然保留,然后再新的路径中找相同的字母和并在一起
while(scanf("%d%d",&u,&v),u+v)
{
if(graph[u][v]==0)
puts("-");
else{
for(int i=0;i<26;i++)
if(graph[u][v]&(1<<i))//查找哪一个字母在图中
printf("%c",'a'+i);
puts("");
}
}
puts("");
}
return 0;
}