Advertisement
Infinite_S

Untitled

Jul 14th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. //In the name of ALLAH
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef vector<int> vi;
  8. typedef vector<ll> vl;
  9. typedef vector<vi> vvi;
  10. typedef vector<vl> vvl;
  11. typedef pair<int,int> pii;
  12. typedef pair<double, double> pdd;
  13. typedef pair<ll, ll> pll;
  14. typedef vector<pii> vii;
  15. typedef vector<pll> vll;
  16. typedef double dl;
  17.  
  18. #define PB push_back
  19. //#define PB emplace_back
  20. #define F first
  21. #define S second
  22. #define MP make_pair
  23. #define endl '\n'
  24. #define all(a) (a).begin(),(a).end()
  25. #define sz(x) (int)x.size()
  26. #define mid(l,r) ((r+l)/2)
  27. #define left(node) (node*2)
  28. #define right(node) (node*2+1)
  29. #define mx_int_prime 999999937
  30.  
  31. const double PI = acos(-1);
  32. const double eps = 1e-9;
  33. const int inf = 2000000000;
  34. const ll infLL = 9000000000000000000;
  35. #define MOD 1000000007
  36. //#define harmonic(n) 0.57721566490153286l+log(n)
  37.  
  38. #define mem(a,b) memset(a, b, sizeof(a) )
  39. #define gcd(a,b) __gcd(a,b)
  40. #define sqr(a) ((a) * (a))
  41.  
  42. #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  43. #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
  44. #define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  45.  
  46. typedef vector<int>::iterator vit;
  47. typedef set<int>::iterator sit;
  48.  
  49. inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
  50. inline ll setBit(ll n, int i) { return n|(1LL<<i); }
  51. inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
  52.  
  53. struct edge {
  54. int u, v, w;
  55. };
  56.  
  57. bool cmp ( const edge &a, const edge &b )
  58. {
  59. return a.w < b.w;
  60. }
  61.  
  62. string to_s(int t)
  63. {
  64. stringstream ss;
  65. ss << t;
  66. return ss.str();
  67. }
  68.  
  69.  
  70. int dx[] = {0, 0, +1, -1};
  71. int dy[] = {+1, -1, 0, 0};
  72. //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
  73. //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
  74.  
  75. inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
  76. inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
  77. inline ll lcm ( ll a, ll b ) { return ( a * ( b / gcd (a, b) ) ); }
  78. inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
  79. inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
  80. inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
  81. inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
  82. inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
  83. inline ll modInverse(ll a) { return modPow(a, MOD-2); }
  84. inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
  85.  
  86. /*
  87. bool seive[1010000];
  88. vi prime;
  89.  
  90. void seiveGen(int limit) {
  91. limit += 100;
  92. int sqrtn = sqrt(limit);
  93. for(int i = 3; i <= sqrtn; i += 2) {
  94. if(!seive[i>>1]) {
  95. for(int j = i * i; j < limit; j += i + i) {
  96. seive[j>>1] = 1;
  97. }
  98. }
  99. }
  100. prime.PB(2);
  101. for(int i = 3; i < limit; i += 2) {
  102. if(!seive[i>>1]) prime.PB(i);
  103. }
  104. }
  105. */
  106.  
  107. //
  108. //debug
  109. //#ifdef
  110. template < typename F, typename S >
  111. ostream& operator << ( ostream& os, const pair< F, S > & p ) {
  112. return os << "(" << p.first << ", " << p.second << ")";
  113. }
  114.  
  115. template < typename T >
  116. ostream &operator << ( ostream & os, const vector< T > &v ) {
  117. os << "{";
  118. for(auto it = v.begin(); it != v.end(); ++it) {
  119. if( it != v.begin() ) os << ", ";
  120. os << *it;
  121. }
  122. return os << "}";
  123. }
  124.  
  125. template < typename T >
  126. ostream &operator << ( ostream & os, const set< T > &v ) {
  127. os << "[";
  128. for(auto it = v.begin(); it != v.end(); ++it) {
  129. if( it != v.begin() ) os << ", ";
  130. os << *it;
  131. }
  132. return os << "]";
  133. }
  134.  
  135. template < typename T >
  136. ostream &operator << ( ostream & os, const multiset< T > &v ) {
  137. os << "[";
  138. for(auto it = v.begin(); it != v.end(); ++it) {
  139. if( it != v.begin() ) os << ", ";
  140. os << *it;
  141. }
  142. return os << "]";
  143. }
  144.  
  145. template < typename F, typename S >
  146. ostream &operator << ( ostream & os, const map< F, S > &v ) {
  147. os << "[";
  148. for(auto it = v.begin(); it != v.end(); ++it) {
  149. if( it != v.begin() ) os << ", ";
  150. os << it -> first << " = " << it -> second ;
  151. }
  152. return os << "]";
  153. }
  154.  
  155. #define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
  156.  
  157. clock_t tStart = clock();
  158. #define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
  159.  
  160. void faltu () {
  161. cerr << endl;
  162. }
  163.  
  164. template <typename T>
  165. void faltu( T a[], int n ) {
  166. for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
  167. cerr << endl;
  168. }
  169.  
  170. template <typename T, typename ... hello>
  171. void faltu( T arg, const hello &... rest) {
  172. cerr << arg << ' ';
  173. faltu(rest...);
  174. }
  175. //#else
  176. //#define dbg(args...)
  177.  
  178. const int mx = 5e5+123;
  179. int level[mx], p[mx][30], n, parent[mx], shade[mx];
  180. vi Aj[mx];
  181. set < int > node[mx];
  182.  
  183. void dfs ( int u, int lev )
  184. {
  185. level[u] = lev;
  186. for ( auto v : Aj[u] ) {
  187. if ( parent[u] != v ) {
  188. parent[v] = u;
  189. dfs ( v, lev+1 );
  190. }
  191. }
  192. }
  193.  
  194. void preprocess()
  195. {
  196. for ( int i = 0; i < n; i++ ) p[i][0] = parent[i];
  197.  
  198. for ( int j = 1; ( 1 << j ) <= n; j++ ) {
  199. for ( int i = 0; i < n; i++ ) {
  200. if ( p[i][j-1] != -1 ) p[i][j] = p[p[i][j-1]][j-1];
  201. }
  202. }
  203. }
  204.  
  205.  
  206. int LCA ( int u, int v )
  207. {
  208. if ( level[u] < level[v] ) swap ( u, v );
  209.  
  210. int dist = level[u] - level[v];
  211. while ( dist > 0 ) {
  212. int rise = log2 ( dist );
  213. u = p[u][rise];
  214. dist -= ( 1 << rise );
  215. }
  216.  
  217. if ( u == v ) return u;
  218.  
  219. for ( int i = 20; i >= 0; i-- ) {
  220. if ( p[u][i] != p[v][i] && p[u][i] != -1 ) {
  221. u = p[u][i];
  222. v = p[v][i];
  223. }
  224. }
  225.  
  226. return parent[u];
  227. }
  228.  
  229. bitset < 300 > vis[mx];
  230. void dfs2 ( int u )
  231. {
  232. vis[u][shade[u]] = 1;
  233. for ( auto v : Aj[u] ) {
  234. if ( v != parent[u] ) {
  235. dfs2 ( v );
  236. vis[u] |= vis[v];
  237. }
  238. }
  239.  
  240. }
  241.  
  242. int main()
  243. {
  244. optimize();
  245.  
  246. int u, v, t, q, r;
  247.  
  248. cin >> t;
  249. while ( t-- ) {
  250.  
  251. mem ( p, -1 );
  252. mem ( vis, 0 );
  253. for ( int i = 0; i <= 5e5; i++ ) {
  254. Aj[i].clear();
  255. node[i].clear();
  256. parent[i] = i;
  257. }
  258.  
  259. cin >> n >> q >> r;
  260. for ( int i = 0; i < n; i++ ) cin >> shade[i];
  261.  
  262. for ( int i = 1; i < n; i++ ) {
  263. cin >> u >> v;
  264. Aj[u].PB ( v );
  265. Aj[v].PB ( u );
  266. }
  267.  
  268. dfs ( r, 0 );
  269. dfs2 ( r );
  270. preprocess();
  271.  
  272. while ( q-- ) {
  273. int cnt = 0;
  274. cin >> u >> v;
  275. int lca = LCA ( u, v );
  276. for ( int i = 1; i <= 250; i++ ) cnt += vis[lca][i];
  277. cout << cnt << endl;
  278. }
  279. }
  280.  
  281. return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement