Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long lli;
- typedef pair<int, int> pii;
- const lli PB = 1e9 + 7;
- map<lli, int> mp;
- int nBlock, Q;
- lli vectorHash(vector<pii> &edge, int nVertex){
- lli hsh = 0;
- lli base = 1;
- for(int i = 0; i < (nVertex - 1) * 2; ++i){
- int tr = (i % 2 == 0)? edge[i / 2].first : edge[i / 2].second;
- hsh += tr * base;
- base *= PB;
- }
- return hsh;
- }
- int main(){
- scanf("%d %d", &nBlock, &Q);
- for(int i = 1; i <= nBlock; ++i){
- int nVertex;
- vector<pii> edge;
- scanf("%d", &nVertex);
- for(int j = 1; j < nVertex; ++j){
- int u, v;
- scanf("%d %d", &u, &v);
- if(u < v){
- edge.emplace_back(u, v);
- } else {
- edge.emplace_back(v, u);
- }
- }
- sort(edge.begin(), edge.end());
- lli hsh = vectorHash(edge, nVertex);
- if(mp.find(hsh) == mp.end()){
- mp.insert(make_pair(hsh, 1));
- } else {
- ++mp[hsh];
- }
- }
- for(int q = 1; q <= Q; ++q){
- int nVertex;
- vector<pii> edge;
- scanf("%d", &nVertex);
- for(int j = 1; j < nVertex; ++j){
- int u, v;
- scanf("%d %d", &u, &v);
- if(u < v){
- edge.emplace_back(u, v);
- } else {
- edge.emplace_back(v, u);
- }
- }
- sort(edge.begin(), edge.end());
- lli hsh = vectorHash(edge, nVertex);
- map<lli, int>::iterator itr = mp.find(hsh);
- if(itr == mp.end()){
- cout << "0\n";
- } else {
- cout << itr -> second << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement