Advertisement
mohammedehab2002

Untitled

Jun 13th, 2020
3,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int pos[100005];
  4. bool ex[100005];
  5. vector<int> v[100005],col[100005],s;
  6. vector<pair<int,int> > e;
  7. deque<int> cyc;
  8. void dfs(int node)
  9. {
  10.     pos[node]=s.size();
  11.     col[pos[node]%2].push_back(node);
  12.     s.push_back(node);
  13.     for (int u:v[node])
  14.     {
  15.         if (pos[u]==-1)
  16.         dfs(u);
  17.         else if (cyc.empty() && pos[node]-pos[u]>1)
  18.         {
  19.             for (int i=pos[u];i<=pos[node];i++)
  20.             {
  21.                 cyc.push_back(s[i]);
  22.                 ex[s[i]]=1;
  23.             }
  24.         }
  25.     }
  26.     s.pop_back();
  27. }
  28. int main()
  29. {
  30.     int n,m,k;
  31.     scanf("%d%d%d",&n,&m,&k);
  32.     while (m--)
  33.     {
  34.         int a,b;
  35.         scanf("%d%d",&a,&b);
  36.         v[a].push_back(b);
  37.         v[b].push_back(a);
  38.         e.push_back({a,b});
  39.     }
  40.     memset(pos,-1,sizeof(pos));
  41.     dfs(1);
  42.     if (cyc.empty())
  43.     {
  44.         if (col[0].size()<col[1].size())
  45.         swap(col[0],col[1]);
  46.         printf("1\n");
  47.         for (int i=0;i<(k+1)/2;i++)
  48.         printf("%d ",col[0][i]);
  49.     }
  50.     else
  51.     {
  52.         for (auto p:e)
  53.         {
  54.             if (ex[p.first] && ex[p.second] && abs(pos[p.first]-pos[p.second])!=1)
  55.             {
  56.                 while (cyc.front()!=p.first && cyc.front()!=p.second)
  57.                 {
  58.                     ex[cyc.front()]=0;
  59.                     cyc.pop_front();
  60.                 }
  61.                 while (cyc.back()!=p.first && cyc.back()!=p.second)
  62.                 {
  63.                     ex[cyc.back()]=0;
  64.                     cyc.pop_back();
  65.                 }
  66.             }
  67.         }
  68.         if (cyc.size()<=k)
  69.         {
  70.             printf("2\n%d\n",cyc.size());
  71.             for (int i:cyc)
  72.             printf("%d ",i);
  73.         }
  74.         else
  75.         {
  76.             printf("1\n");
  77.             for (int i=0;i<(k+1)/2;i++)
  78.             printf("%d ",cyc[2*i]);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement