fork download
  1. #include <bits/stdc++.h>
  2. #define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  3. #define ll long long
  4. #define ld long double
  5. #define pb push_back
  6. using namespace std;
  7. vector<vector<int>>v;
  8. map<pair<int,int>,int>cost;
  9. bool vis[(int)1e5+5];
  10. map<int,int>source;
  11. int a,b;
  12. bool o=0;
  13. void dfs1(int node,int x)
  14. {
  15. vis[node]=1;
  16. for(auto child:v[node])
  17. {
  18. if(!vis[child])
  19. {
  20. if(child!=b) // not reaching dest from source
  21. {
  22. source[x^cost[{node,child}]]=1;
  23. dfs1(child,x^cost[{node,child}]);
  24. }
  25. }
  26. }
  27. }
  28. void dfs2(int node,int x)
  29. {
  30. vis[node]=1;
  31. for(auto child:v[node])
  32. {
  33. if(!vis[child])
  34. {
  35. if(source[x^cost[{node,child}]]|| (x^cost[{node,child}])==0)
  36. o=1;
  37. dfs2(child,x^cost[{node,child}]);
  38. }
  39. }
  40. }
  41. int main()
  42. {fast
  43. int t;
  44. cin>>t;
  45. while(t--)
  46. {
  47. int n;
  48. cin>>n>>a>>b;
  49. v.clear();
  50. v.resize(n+1);
  51. source.clear();
  52. cost.clear();
  53. o=0;
  54. for(int i=1;i<=n;i++) vis[i]=0;
  55. int x,y,z;
  56. for(int i=0;i<n-1;i++)
  57. {
  58. cin>>x>>y>>z;
  59. v[x].pb(y);
  60. v[y].pb(x);
  61. cost[{x,y}]=z;
  62. cost[{y,x}]=z;
  63. }
  64. dfs1(a,0); //go from source
  65. for(int i=1;i<=n;i++) vis[i]=0;
  66. dfs2(b,0) ;//go from dest
  67. (o)?cout<<"YES\n":cout<<"NO\n";
  68. }
  69. return 0;
  70. }
Runtime error #stdin #stdout #stderr 0.01s 5504KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc