Advertisement
mickypinata

TOI14: Blockchain

Nov 20th, 2020
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5. typedef pair<int, int> pii;
  6.  
  7. const lli PB = 1e9 + 7;
  8.  
  9. map<lli, int> mp;
  10. int nBlock, Q;
  11.  
  12. lli vectorHash(vector<pii> &edge, int nVertex){
  13.     lli hsh = 0;
  14.     lli base = 1;
  15.     for(int i = 0; i < (nVertex - 1) * 2; ++i){
  16.         int tr = (i % 2 == 0)? edge[i / 2].first : edge[i / 2].second;
  17.         hsh += tr * base;
  18.         base *= PB;
  19.     }
  20.     return hsh;
  21. }
  22.  
  23. int main(){
  24.  
  25.     scanf("%d %d", &nBlock, &Q);
  26.     for(int i = 1; i <= nBlock; ++i){
  27.         int nVertex;
  28.         vector<pii> edge;
  29.         scanf("%d", &nVertex);
  30.         for(int j = 1; j < nVertex; ++j){
  31.             int u, v;
  32.             scanf("%d %d", &u, &v);
  33.             if(u < v){
  34.                 edge.emplace_back(u, v);
  35.             } else {
  36.                 edge.emplace_back(v, u);
  37.             }
  38.         }
  39.         sort(edge.begin(), edge.end());
  40.         lli hsh = vectorHash(edge, nVertex);
  41.         if(mp.find(hsh) == mp.end()){
  42.             mp.insert(make_pair(hsh, 1));
  43.         } else {
  44.             ++mp[hsh];
  45.         }
  46.     }
  47.  
  48.     for(int q = 1; q <= Q; ++q){
  49.         int nVertex;
  50.         vector<pii> edge;
  51.         scanf("%d", &nVertex);
  52.         for(int j = 1; j < nVertex; ++j){
  53.             int u, v;
  54.             scanf("%d %d", &u, &v);
  55.             if(u < v){
  56.                 edge.emplace_back(u, v);
  57.             } else {
  58.                 edge.emplace_back(v, u);
  59.             }
  60.         }
  61.         sort(edge.begin(), edge.end());
  62.         lli hsh = vectorHash(edge, nVertex);
  63.         map<lli, int>::iterator itr = mp.find(hsh);
  64.         if(itr == mp.end()){
  65.             cout << "0\n";
  66.         } else {
  67.             cout << itr -> second << "\n";
  68.         }
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement