fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 2e5+5;
  5. int n , m, p[N][3],sz[N][3];
  6. vector<pair<int,int>> checklist;
  7.  
  8. struct edge{
  9. int u,v,w;
  10. };
  11. vector<edge> e;
  12.  
  13. int get(int u,int t){
  14. if(u != p[u][t]){
  15. p[u][t] = get(p[u][t],t);
  16.  
  17. }
  18. return p[u][t];
  19. }
  20.  
  21. bool join(int u,int v,int t){
  22. u = get(u,t);
  23. v = get(v,t);
  24. if(u == v) return false;
  25. if(sz[u][t] > sz[v][t]) swap(u,v);
  26. sz[v][t] +=sz[u][t];
  27. p[u][t] =v;
  28.  
  29. return true;
  30. }
  31.  
  32.  
  33.  
  34. int main(){
  35. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  36. cin >> n >> m;
  37. for(int i=0 ;i <=n;i++) for(int j=1;j<=2;j++) {p[i][j]=i; sz[i][j] =1;};
  38.  
  39. for(int i=1;i<=m;i++){
  40. int x,u,v;
  41. cin >> x >> u >> v;
  42. bool check = true;
  43. join(u,v,x);
  44. checklist.push_back({u,v});
  45.  
  46.  
  47. for(int j = checklist.size()-1;j >=0;j--){
  48. u = checklist[j].first; v = checklist[j].second;
  49. int u1= get(u,1); int v1 = get(v,1);
  50. int u2 = get(u,2); int v2 = get(v,2);
  51.  
  52. if(u1 == v1 && u2 ==v2){
  53. checklist.pop_back();
  54. }
  55. else break;
  56.  
  57. }
  58. if(checklist.size()){
  59. cout << "No\n";
  60. }
  61. else cout << "Yes\n";
  62. }
  63.  
  64.  
  65.  
  66. }
  67.  
Success #stdin #stdout 0s 5652KB
stdin
Standard input is empty
stdout
Standard output is empty