NTA

 

Time Limit: 2000 msMemory Limit: 65536 KB

The NTA (Non-deterministic Tree Automata) is a kind of tree structure device. The device is built in a set of operating rules. With these rules the device can produce several signals, which will form a signal system. In such a system, one signal is the starting signal, several signals are the acceptable signals, and the others are the auxiliary ones. A pair of signals is said to be an acceptable pair if both two signals of the pair are acceptable.

The trees discussed here are all binary trees. Every non-leaf node has two successors. In any finite tree, each node has a signal-transmitting element. When a signal arrives at one node, the signal meets the signal transmitting substance, and triggers off signal reactions, which will produce several pairs of signals. Then the device selects a pair of signals non-deterministically and sends them to its successors. The first signal in the signal pair is sent to the left successive node and the second one is sent to the right successive node.

The whole operation for an NTA is as follows:

The device first sends the starting signal to the root node. According to the signal transmitting substance at the root node, the device selects a pair of signals non-deterministically and sends the first to the left son and the second to the right son. Each of the two signals then meets the signal transmitting substance at the corresponding node and produces another two signals. The course proceeds down until the signals arrive at the leaves.

If a signal reaches one leaf and the leaf can produce a pair of acceptable signals, we say the leaf is "shakable". A transmission of signals from the root to leaves is said to be valid if all leaves are "shakable". A tree structure with signal transmitting substance is valid if there exists such a valid transmission. A tree is invalid if all the transmissions are invalid.

For simplicity, we denote the signal transmitting elements by consecutive lowercase letters "a", "b", "c", etc.. The signals of an NTA are consecutive numbers 0,1,2, ..., and so on. The first signal 0 is always a starting signal. Thus the signals for a 4-signal NTA are "0" "1" "2" and "3". Accepting signals are arranged at the end of the number sequence so that if a 4-signal NTA has two accepting signals, the accepting signals are "2" and "3". The transition rules of signals are based on a transition table. For example, the following table describes a transition table with four signals "0", "1", "2", "3" and with three signal transmitting elements "a", "b" and "c".

 

T a b c
0 (1,2) (2,1) (1,0)
1 (2,2) (0,2),(1,0) (3,2)
2 (2,2) (2,3) (1,2)
3 (1,2) (2,1) (3,2)

 



In this transition table some reactions of signals on certain signal transmitting elements are deterministic, and others are non-deterministic. In the example above, if signal "1" reaches the node with the transmitting element "b", the reaction is non-deterministic.

Now your task is to write a program to judge if a tree structure with certain signal transmitting substance is valid.


Input

The input file contains several cases. Each case describes a sequence of NTA descriptions and some initial tree configurations. The first line for each case consists of three integers n, m and k. The integer n is the number of signals, m indicates the number of accepting signals, and k is number of signal transmitting elements. The following n k lines describe the transition table in row-major order. Each transition of a signal on signal transmitting element is given on a separate line. On such line every two numbers represent a possible transition.

This is followed by the description of tree structures. For every tree structure a number L is given on a separate line to indicate the level of the tree. The following L+1 lines containing a sequence of letters describe the tree structure. Each level is described in one line. There exist one space between two successive letters. The 0-th level begins firstly. In the tree structure, the empty nodes are marked by "*". The tree structure with L=-1 terminates the configurations of tree structures for that NTA, and this structure should not be judged.

The input is terminated by a description starting with n=0, m=0 and k=0. This description should not be processed.

Note: In each case, NTA will have at most l5 signals and 10 characters. The level of each tree will be no more than 10.


Output

For each NTA description, print the number of the NTA (NTAl, NTA2, etc.) followed by a colon. Then for each initial tree configuration of the NTA print the word "Valid" or "Invalid".

Print a blank line between NTA cases.


Sample Input

4 2 3
1 2
2 1
1 0
2 2
0 2 1 0
3 2
2 2
2 3
1 2
1 2
2 1
3 2
3
a
b c
a b c b
b a b a c a * *
2
b
a b
b c * *
-1
0 0 0


Output for the Sample Input

NTA1:
Valid
Invalid

 

 

NTA

 

NTA (Non-deterministic Tree Automata)是一种树形结构的设备。该设备内置在一套操作规则中。根据这些规则,设备可以产生多个信号,这些信号将形成一个信号系统。在这样的系统中,一个信号是启动信号,几个信号是可接受信号,其他的是辅助信号。如果一对信号中的两个信号都是可接受的,那么这一对信号就称为可接受的一对。

 

这里讨论的树都是二叉树。每个非叶节点都有两个后继节点。在任何有限树中,每个节点都有一个信号传输单元。当信号到达一个节点时,与传递信号的物质相遇,触发信号反应,产生数对信号。然后设备不确定地选择一对信号并将它们发送给它的后继器。信号对中的第一个信号被发送到左连续节点,第二个信号被发送到右连续节点。

 

NTA的整体操作如下:

 

设备首先向根节点发送启动信号。设备根据根节点的信号传输物质,非确定性地选择一对信号发送给左子节点,发送给右子节点。然后,这两个信号中的每一个都在相应的节点上与信号传输物质相遇,并产生另外两个信号。这个过程继续下去,直到信号到达树叶。

 

如果一个信号到达一个叶子,而这个叶子可以产生一对可接受的信号,我们就说这个叶子是“可摇动的”。如果所有的叶子都“摇动”,信号从根到叶子的传输就被认为是有效的。如果存在这样一种有效的传输,那么具有信号传输物质的树状结构是有效的。如果所有的传输都无效,则树是无效的。

 

为简单起见,我们用连续的小写字母“a”、“b”、“c”等来表示信号传输元素。网流分析器的信号为0、1、2、…等等。第一个信号0总是一个起始信号。因此,4信号网流分析器的信号是“0”、“1”、“2”和“3”。接收信号被安排在数字序列的末尾,这样,如果一个4信号网流有两个接收信号,接收信号是“2”和“3”。信号的转换规则是基于转换表的。例如,下表描述了一个包含四个信号“0”、“1”、“2”、“3”和三个信号发射元素“a”、“b”和“c”的转换表。

 

T a b c
0 (1,2) (2,1) (1,0)
1 (2,2) (0,2),(1,0) (3,2)
2 (2,2) (2,3) (1,2)
3 (1,2) (2,1) (3,2)

 

在这个转换表中,某些信号在某些信号发射元件上的反应是确定的,而另一些则是不确定的。在上面的例子中,如果信号“1”到达发射单元“b”的节点,反应是不确定的。

 

现在,你的任务是编写一个程序来判断具有某种信号传输物质的树状结构是否有效。

 

输入

输入文件包含几种情况。每个案例描述了一系列的NTA描述和一些初始的树配置。每一种情况的第一行由3个整数n、m、k组成,整数n为信号个数,m为接收信号个数,k为信号发射单元个数。下面的n k行按行主顺序描述了转换表。信号传输元件上信号的每次过渡都在单独的线路上给出。在这一行,每两个数字代表一个可能的转换。

 

接下来是对树结构的描述。对于每一个树结构,在单独的一行中给出一个数字L,表示树的级别。下面的L+1行包含一个字母序列来描述树形结构。每一层都用一行来描述。两个连续的字母之间有一个空格。第0级首先开始。在树结构中,空节点用“*”标记。L=-1的树形结构终止了该NTA的树形结构构型,不应对该结构进行判断。

 

输入以n=0, m=0和k=0开始的描述结束。不应处理此描述。

 

注意:在每种情况下,NTA最多有l5个信号和10个字符。每棵树的级别将不超过10。

 

输出

对于每个NTA描述,在冒号后面打印NTA的编号(NTAl, NTA2,等等)。然后,对于NTA的每个初始树配置,打印单词“Valid”或“Invalid”。

 

在NTA大小写之间打印空行。

 

Sample Input

4 2 3
1 2
2 1
1 0
2 2
0 2 1 0
3 2
2 2
2 3
1 2
1 2
2 1
3 2
3
a
b c
a b c b
b a b a c a * *
2
b
a b
b c * *
-1
0 0 0


Output for the Sample Input

NTA1:
Valid
Invalid

 

ZOJ 1011 NTA_C语言

 

题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点。最后判断所有叶子节点是否满足给定的规则。题目有点长,具体可参见原题。

  首先是表格中数据的存储,由于会有多个元素,用vector进行保存。其次,树是完全二叉树,可以用数组存储树。然后就是遍历二叉树了。

 

代码:

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
struct Signal
{
int left, right;
};
vector<Signal> table[20][20];
char tree[2100];
int n, m, k;
int l;
int nodeN;
void readTable()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
{
table[i][j].clear();
Signal pair;
while (true)
{
scanf("%d%d", &pair.left, &pair.right);
table[i][j].push_back(pair);
char ch = getchar();
if (ch == '\n')
break;
}
}
}

void readTree()
{
char ch;
nodeN = 0;
for (int i = 0; i <= l; i++)
for (int j = 0; j < (1<<i); j++)
{
cin >> ch;
tree[++nodeN] = ch;
}
}

bool judge(int signal, int node)
{
if (tree[node] == '*' || node > nodeN)
{
if (signal < n-m)
return false;
else
return true;
}
int t = tree[node] - 'a';
for (int i = 0; i < table[signal][t].size(); i++)
{
int signal1 = table[signal][t][i].left;
int signal2 = table[signal][t][i].right;
if (judge(signal1, node*2) && judge(signal2, node*2+1))
return true;
}
return false;
}

int main()
{
int kase = 0;
while(scanf("%d%d%d", &n, &m, &k) && (n || m || k))
{
if (kase++)
printf("\n");
printf("NTA%d:\n", kase);
readTable();
while (scanf("%d", &l) && (l != -1))
{
readTree();
if(judge(0, 1))
printf("Valid\n");
else
printf("Invalid\n");
}
}
return 0;
}