Advertisement
zydhanlinnar11

Info Soal?

May 31st, 2022
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. // Tested by zydhanlinnar11 on May 31, 2022
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <set>
  6. #include <cassert>
  7. using namespace std;
  8. typedef vector<int> vi;
  9. typedef vector<vi> vvi;
  10. typedef set<int> si;
  11. typedef vector<si> vsi;
  12.  
  13. bool dfs(vi &clr, vsi &adjList, int n, int u, int depth) {
  14.     if(clr[u] >= 1) return depth == n;
  15.     clr[u] = 1;
  16.     bool res = false;
  17.     for(auto &v: adjList[u]) res |= dfs(clr, adjList, n, v, depth + 1);
  18.     clr[u] = 0;
  19.     return res;
  20. }
  21.  
  22. int main() {
  23.     #ifdef ZYD_WSL
  24.         freopen("/home/zydhanlinnar11/prakfinal-qa/in", "r", stdin);
  25.     #endif
  26.     int n, m; cin>>n>>m;
  27.     assert(0 <= n && n <= 10);
  28.     assert(0 <= m && m <= 45);
  29.     vvi adjList(n); vsi adjList2(n);
  30.     for(int i=0; i<m; i++) {
  31.         int u, v; cin>>u>>v;
  32.         assert(0 <= u && u < n);
  33.         assert(0 <= v && v < n);
  34.         adjList[u].push_back(v);
  35.         adjList[v].push_back(u);
  36.     }
  37.     for(int i=0; i<n; i++)
  38.         for(auto &j: adjList[i])
  39.             for(auto &v: adjList[j]) {
  40.                 if(v == i) continue;
  41.                 adjList2[i].insert(v); adjList2[v].insert(i);
  42.             }
  43.     bool ans = false;
  44.     for(int i=0; i<n; i++) {
  45.         vi clr(n, 0);
  46.         ans |= dfs(clr, adjList2, n, i, 0);
  47.     }
  48.     cout<<(ans ? "YES" : "NO")<<"\n";
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement