tankwoks

CSU 1837

Mar 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <set>
  2. #include <map>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <vector>
  8. #include <bitset>
  9. #include <string>
  10. #include <cctype>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <cstdlib>
  14. #include <iostream>
  15. #include <algorithm>
  16. #include <unordered_map>
  17. #include <sstream>
  18.  
  19. using namespace std;
  20.  
  21. typedef long long ll;
  22. typedef unsigned long long ull;
  23. typedef pair<int, int> pii;
  24. typedef pair<ull, ull> puu;
  25.  
  26. #define inf (0x3f3f3f3f)
  27. #define lnf (0x3f3f3f3f3f3f3f3f)
  28. #define eps (1e-9)
  29. #define fi first
  30. #define se second
  31.  
  32. bool sgn(double a, string select, double b) {
  33.     if(select == "==")return fabs(a - b) < eps;
  34.     if(select == "!=")return fabs(a - b) > eps;
  35.     if(select == "<")return a - b < -eps;
  36.     if(select == "<=")return a - b < eps;
  37.     if(select == ">")return a - b > eps;
  38.     if(select == ">=")return a - b > -eps;
  39. }
  40.  
  41.  
  42. //--------------------------
  43.  
  44. const ll mod = 1000000007;
  45. const int maxn = 100010;
  46.  
  47. const int PA = 11111;
  48. const int PB = 31111;
  49.  
  50. int head[maxn];
  51. int son[maxn];
  52. int val[maxn];
  53. ull h[maxn];
  54. int ans;
  55. int cnt, n;
  56. int num;
  57. pii root;
  58. int rt_min;
  59. map<puu, int> tree;
  60.  
  61.  
  62.  
  63. struct Edge {
  64.     int to;
  65.     int next;
  66. };
  67.  
  68. Edge edge[2 * maxn];
  69.  
  70. void init() {
  71.     cnt = 0;
  72.     num = 1;
  73.     rt_min = inf;
  74.     memset(head, -1, sizeof(head));
  75. }
  76.  
  77. void add_edge(int u, int v) {
  78.     edge[cnt].to = v;
  79.     edge[cnt].next = head[u];
  80.     head[u] = cnt++;
  81. }
  82.  
  83.  
  84. void getroot(int u, int par) {
  85.     son[u] = 1;
  86.     int Max = 0;
  87.     for(int i = head[u]; ~i; i = edge[i].next) {
  88.         int v = edge[i].to;
  89.         if(v == par) continue;
  90.         getroot(v, u);
  91.         son[u] += son[v];
  92.         Max = max(Max, son[v]);
  93.     }
  94.     Max = max(Max, num - son[u]);
  95.     if(Max < rt_min) {
  96.         rt_min = Max;
  97.         root.fi = u;
  98.         root.se = u;
  99.     } else if(Max == rt_min) {
  100.         root.se = u;
  101.     }
  102. }
  103.  
  104. ull gethash(int u, int par) {
  105.     if(par == -1) h[u] = PA;
  106.     else h[u] = ((ull)(val[u] - val[par]))^PA;
  107.  
  108.     for(int i = head[u]; ~i; i = edge[i].next) {
  109.         int v = edge[i].to;
  110.         if(v == par)continue;
  111.         gethash(v, u);
  112.         h[u] *= h[v] ^ PB;
  113.     }
  114.     return h[u];
  115. }
  116.  
  117. string str;
  118.  
  119. void solve() {
  120.     cin >> n;
  121.     int u, v;
  122.     cin.get();
  123.     for(int i = 0; i < n; i++) {
  124.         init();
  125.         char en;
  126.         getline(cin,str);
  127.         stringstream in(str);
  128.         int u,v;
  129.         while(in>>u>>v) {
  130.             add_edge(u, v);
  131.             add_edge(v, u);
  132.             num++;
  133.         }
  134.         for(int j = 1; j <= num; j++) {
  135.             cin>>val[j];
  136.         }
  137.         cin.get();
  138.         getroot(1, -1);
  139.         ull a, b;
  140.         puu now;
  141.         now.fi =  gethash(root.fi, -1);
  142.         if(root.fi != root.se) {
  143.             now.se = gethash(root.se, -1);
  144.         }
  145.         if(now.fi>now.se)swap(now.fi,now.se);
  146.         tree[now]++;
  147.  
  148.     }
  149.     vector<int> ans;
  150.     for(auto it = tree.begin(); it != tree.end(); it++) {
  151.         ans.push_back(it->se);
  152.     }
  153.     sort(ans.begin(), ans.end());
  154.     for(int i = 0; i < ans.size() - 1; i++) {
  155.         printf("%d ", ans[i] );
  156.     }
  157.     printf("%d\n", ans[ans.size() - 1] );
  158.  
  159.  
  160.  
  161. }
  162.  
  163. int main() {
  164.  
  165. #ifndef ONLINE_JUDGE
  166.     freopen("1.in", "r", stdin);
  167. //    freopen("1.out", "w", stdout);
  168. #endif
  169.     iostream::sync_with_stdio(false);
  170.     solve();
  171.     return 0;
  172. }
  173.  
  174. /**********************************************************************
  175.     Problem: 1837
  176.     User: tankwoks
  177.     Language: C++
  178.     Result: RE
  179. **********************************************************************/
  180.  
  181. /**********************************************************************
  182.     Problem: 1837
  183.     User: tankwoks
  184.     Language: C++
  185.     Result: AC
  186.     Time:108 ms
  187.     Memory:6832 kb
  188. **********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment