Species Tree uses HashTable to implement
The topic reappears
Question content:
Given a species evolution map, The relationship is expressed as follows: x y : expressxforyThe ancestors。 A species will have only one ancestor, An ancestor can have many evolved species, Please find out the grandfather species of each question to ask about species(先祖The ancestors), 每个物种会使用一个不重复的编号来express, If the species does not have a grandfather species or does not exist, Then please consider his grandfather's species0。(Born out of thin air) Ensure that all species are connected, And there will be no repeated evolution, That is, the evolution map will only be a tree。 Input format: Only one set of tests。 There will be two numbers on the first rowN、Q,Represents a total ofNSpecies andQA question。 NextN-1OK,每一OK有两个数字x、y, The meaning is as stated in the title。 Next的QOK,每一OK有一个数字Z, Represents the species number to be asked。 Measurement scope: 1 < N < 10000 0 < Q < 1000 0 < x, y, z < 1000000 Output format: For each inquired species number,Add their grandfather species numbers and output them。 Enter sample: 6 3 1000 2000 1000 3000 2000 4000 3000 5000 5000 6000 5000 6000 1234 Output sample: 4000 Time limit:100msMemory limit:16000kb
Algorithm implementation
1. Simple array subscript search method
Through the requirements of the question, the easiest method can be used here, because the value of y in the input x and y is determined and unchanged, so we can define an array with a large value range of y. The subscript is the value of y and the content is the value of x. This is very convenient to search, the time complexity is O(1), but more space will be wasted.
#include <> #include <> int main(){ int arrBucket[1000000]; int N, Q; int x, y, z; long long sum = 0; memset(arrBucket, 0, sizeof(arrBucket)); scanf("%d %d", &N, &Q); while(N -- > 1){ scanf("%d %d", &x, &y); arrBucket[y] = x; } while(Q --){ scanf("%d", &z); if(arrBucket[z] != 0){ if(arrBucket[arrBucket[z]] != 0){ sum += arrBucket[arrBucket[z]]; } } } printf("%lld", sum); return 0; }
2. Hash table implementation
Because in Method 1, space wasted is severe, the Hash table is implemented here.
#include <> #include <> #define NULLKEY -1 typedef struct { int *elem; //element int *elemP; // Parent element int count; }HashTable; int Hash(HashTable H, int k){ return k % ; } void InitHashTable(HashTable *H){ int i; H->elem = (int *)malloc(sizeof(int) * H->count); H->elemP = (int *)malloc(sizeof(int) * H->count); for(i = 0; i < H->count; i ++){ H->elem[i] = NULLKEY; H->elemP[i] = NULLKEY; } } void InsertHash(HashTable *H, int key, int value){ int addr; addr = Hash(*H, key); while(H->elem[addr] != NULLKEY){ addr = (addr + 1) % H->count; } H->elem[addr] = key; H->elemP[addr] = value; } int FindHash(HashTable *H, int key, int *addr){ *addr = Hash(*H, key); while(H->elem[*addr] != key){ *addr = (*addr + 1) % H->count; if(H->elem[*addr] == NULLKEY || *addr == Hash(*H, key)){ return 0; } } return 1; } int main(){ int N, Q; int x, y, z, addr; long long sum = 0; HashTable H; scanf("%d %d", &N, &Q); = N - 1; InitHashTable(&H); while(N -- > 1){ scanf("%d %d", &x, &y); InsertHash(&H, y, x); } while(Q --){ scanf("%d", &z); if(FindHash(&H, z, &addr)){ if(FindHash(&H, [addr], &addr)){ sum += [addr]; } } } printf("%lld", sum); return 0; }
3. Use C++ map to implement
Looking at the implementation of C, you have to write all the functions you implement by yourself. Here I will take a look at using maps in STL in C++ to implement the above question. It is very simple (I have to say that STL is really useful, but it is not as fast as HashTable, and the space is not as small as HashTable)
#include <iostream> #include <map> using namespace std; int main() { int n, q; cin >> n >> q; map<int,int> mp; map<int,int>::iterator it; int x, y, z; for (int i=1; i<n; ++i) { cin >> x >> y; (pair<int,int>(y,x)); } int sum = 0; for (int i=0; i<q; ++i) { cin >> z; it = (z); if (it != ()) { it = (it->second); if (it != ()) sum += it->second; } } cout << sum; return 0; }
Thank you for reading, I hope it can help you. Thank you for your support for this site!