fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6. vector<int>adj[1000001];
  7. // vector<int>cc(1000001,0);
  8. int cc[1000001];
  9. int cc_cnt;
  10. bool vis[1000001];
  11. void dfs(int x){
  12. vis[x] = true;
  13. cc[x] = cc_cnt;
  14. for(int neigh:adj[x])
  15. if(!vis[neigh])
  16. {dfs(neigh);}
  17. }
  18.  
  19. int main(){
  20. int t;
  21. cin>>t;
  22. ios_base::sync_with_stdio(false);
  23. cin.tie(NULL);
  24. while(t--){
  25. int n,k;
  26. cin>>n>>k;
  27. bool flag = 1;
  28. vector<pair<int,int>>queries;
  29. for(int i=0;i<=n;i++){
  30. vis[i] = 0;
  31. adj[i].clear();
  32. cc[i]=0;
  33. }
  34. cc_cnt =0;
  35. while(k--){
  36. int x1,x2;
  37. string op;
  38. cin>>x1>>op>>x2;
  39. if(op=="="){
  40. adj[x1].push_back(x2);
  41. adj[x2].push_back(x1);
  42. }
  43. else
  44. queries.push_back({x1,x2});
  45. }
  46.  
  47. for(int i = 1;i<=n;i++){
  48. if(vis[i]==0){
  49. cc_cnt++;
  50. dfs(i);
  51. }
  52. }
  53. for(int i =0;i<queries.size();i++){
  54. int a = queries[i].first;
  55. int b = queries[i].second;
  56.  
  57. if(cc[a]==cc[b]){
  58. flag = false;
  59. break;
  60. }
  61. }
  62.  
  63. if(flag)
  64. cout<<"YES"<<endl;
  65. else
  66. cout<<"NO"<<endl;
  67.  
  68.  
  69. }
  70.  
  71. }
Runtime error #stdin #stdout 0.01s 27008KB
stdin
Standard input is empty
stdout
Standard output is empty