fork download
  1. #include <bits/stdc++.h>
  2. #define FNAME "circuit"
  3. using namespace std;
  4. typedef long long ll;
  5. const int MAXN = (int)2e5 + 1;
  6. #define xd '\n'
  7. #define pii pair<int, int>
  8. #define pil pair<int, long long>
  9. const long long MOD = 1e9 + 15092007;
  10.  
  11. void fastip() {
  12. ios_base::sync_with_stdio(0);
  13. cin.tie(0); cout.tie(0);
  14. if (fopen(FNAME".inp", "r")) {
  15. freopen(FNAME".inp", "r", stdin);
  16. freopen(FNAME".out", "w", stdout);
  17. }
  18. }
  19.  
  20. struct Edge{
  21. int u, v;
  22. bool eID;
  23. };
  24.  
  25. int n, s;
  26. vector<Edge> edge;
  27. vector<vector<int>> adj;
  28. vector<int> Ans;
  29. vector<int> List;
  30.  
  31. void Euler(int s){
  32. stack<int> st;
  33. st.push(s);
  34.  
  35. while(!st.empty()){
  36. int u = st.top();
  37. bool check = false;
  38.  
  39. for(int id : adj[u]){
  40. bool cID = edge[id].eID;
  41. if(!cID){
  42. edge[id].eID = true;
  43. check = true;
  44. st.push((edge[id].u == u) ? edge[id].v : edge[id].u);
  45. break;
  46. }
  47. }
  48.  
  49. if(!check) {
  50. Ans.push_back(u);
  51. st.pop();
  52. }
  53. }
  54. }
  55.  
  56. signed main(){
  57. fastip();
  58.  
  59. cin >> n >> s;
  60.  
  61. adj.resize(n + 1);
  62.  
  63. int u, v;
  64.  
  65. while(cin >> u >> v) {
  66. edge.push_back({u, v, false});
  67. int id = edge.size() - 1;
  68. adj[u].push_back(id);
  69. adj[v].push_back(id);
  70. }
  71.  
  72. Euler(s);
  73. reverse(Ans.begin(), Ans.end());
  74.  
  75. for(int i = 0; i < Ans.size() ; i++){
  76. if(Ans[i] == s && i != 0){
  77. cout << "YES";
  78. return 0;
  79. }
  80. }
  81.  
  82. cout << "NO";
  83.  
  84. return 0;
  85. }
  86.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
NO