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.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.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 -
公司使用小寫字母表示的,最多26個公司,故可以用二進制的位來表示公司。
#include"stdio.h" #include"string.h" #include"vector" #include"queue" #include"iostream" #include"algorithm" using namespace std; #define N 205 const int inf=(int)1e10; int g[N][N]; int main() { int i,j,k,n,a,b; char str[27]; while(scanf("%d",&n),n) { memset(g,0,sizeof(g)); while(scanf("%d%d",&a,&b),a||b) { scanf("%s",str); for(i=0;str[i]!='\0';i++) //某一位上為一代表該公司可以連接a,b g[a][b]|=1<<(str[i]-'a'); } 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]); } } } while(scanf("%d%d",&a,&b),a||b) { if(!g[a][b]) { printf("-\n"); continue; } for(char i='a';i<='z';i++) { if(g[a][b]&(1<