chefcurry30

Untitled

Sep 2nd, 2018
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mp make_pair
  5. #define pb push_back
  6. #define f first
  7. #define s second
  8. #define MOD 1000000007
  9. #define PI 3.14159265
  10. #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  11. ll max(ll a,ll b)
  12. {
  13. if(a>b)
  14. return a;
  15. return b;
  16. }
  17.  
  18. ll min(ll a,ll b)
  19. {
  20. return a+b-max(a,b);
  21. }
  22. ll mod(ll a,ll b)
  23. {
  24. if(a>b)
  25. return a-b;
  26. else
  27. return b-a;
  28. }
  29. ll power(ll x,ll y, ll p)
  30. {
  31. ll res = 1;
  32. x = x % p;
  33. while (y > 0)
  34. {
  35. if (y & 1)
  36. res = (res*x)%p;
  37. y = y>>1;
  38. x = (x*x)%p;
  39. }
  40. return res;
  41. }
  42. ll dist[200005];
  43. ll maxdist=0;
  44. void dfs(list<ll> g[],ll s,vector<bool> &visited)
  45. {
  46. visited[s]=true;
  47. for(auto it:g[s])
  48. if(!visited[it])
  49. {
  50. dist[it]=1+dist[s];
  51. maxdist=max(maxdist,dist[it]);
  52. dfs(g,it,visited);
  53. }
  54. }
  55. int main()
  56. {
  57. fastio;
  58. ll n;
  59. cin>>n;
  60. ll x,y;
  61. list<ll> g[n+5];
  62. for(ll i=0;i<n-1;i++)
  63. {
  64. cin>>x>>y;
  65. g[x].pb(y);
  66. g[y].pb(x);
  67. }
  68. memset(dist,0,sizeof(dist));
  69. vector<bool> visited(n+5,false);
  70. dfs(g,1,visited);
  71. vector<ll> alls[n+5];
  72. for(ll i=1;i<=n;i++)
  73. alls[dist[i]].pb(i);
  74. ll arr[n+5];
  75. for(ll i=0;i<n;i++)
  76. cin>>arr[i];
  77. ll arr1[n+5];
  78. ll tot=0;
  79. for(ll i=0;i<=maxdist;i++)
  80. {
  81. ll ctr=0;
  82. for(ll j=tot;j<tot+alls[i].size();j++)
  83. {
  84. arr1[ctr]=arr[j];
  85. ctr++;
  86. }
  87. sort(arr1,arr1+alls[i].size());
  88. for(ll j=0;j<alls[i].size();j++)
  89. if(arr1[j]!=alls[i][j])
  90. {
  91. cout<<"No";
  92. return 0;
  93. }
  94. tot+=alls[i].size();
  95. }
  96. cout<<"Yes";
  97. }
RAW Paste Data