Grafalgo
Library of useful data structures and algorithms
|
00001 // usage: 00002 // test_nca 00003 // 00004 // Test_nca reads a tree from stdin and a list of vertex pairs, 00005 // then invokes the nca routine to, computes the nearest common 00006 // ancestor for each pair. Vertex 1 is treated as the tree root. 00007 // On completion, test_nca prints the list of pairs and their nca. 00008 00009 #include "Nca.h" 00010 00011 using namespace grafalgo; 00012 00013 const int maxP = 100; 00014 00015 int main() { 00016 int np; vertex x, y; 00017 Graph tree; tree.read(cin); 00018 VertexPair pairs[maxP]; 00019 vertex ncav[maxP]; 00020 00021 np = 0; 00022 while (1) { 00023 Util::skipBlank(cin); 00024 if (!Util::verify(cin,'(') || !Util::readNode(cin,x,tree.n()) || 00025 !Util::verify(cin,',') || !Util::readNode(cin,y,tree.n()) || 00026 !Util::verify(cin,')') || np >= maxP) 00027 break; 00028 pairs[np].v1 = x; pairs[np++].v2 = y; 00029 } 00030 00031 Nca(tree,1,pairs,np,ncav); 00032 string s; 00033 for (int i = 0; i < np; i++) { 00034 cout << "nca(" << Util::node2string(pairs[i].v1,tree.n(),s); 00035 cout << "," << Util::node2string(pairs[i].v2,tree.n(),s); 00036 cout << ")=" << Util::node2string(ncav[i],tree.n(),s); 00037 if (i%5 == 4) cout << endl; 00038 else cout << " "; 00039 } 00040 if (np%5 != 0) cout << endl; 00041 }