#include <bits/stdc++.h>
using namespace std;

// //------------------------------------------------------------------------
// //for POLICY BASED DATA STRUCTURE

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;

// //stores unique integers in increasing order
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// //stores unique integers in descreasing order
// typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// //store integers in increasing order(can store duplicate integers also like multiset)
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set1;

// //store integers in decreasing order(can store duplicate integers also like multiset)
// typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set2;

// // Declaring ordered_set for pair<int,int>
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> ordered_set_pair;

// //------------------------------------------------------------------------

#define forn(i, n) for (int i = 0; i < n; i++)
#define um unordered_map
#define us unordered_set
#define sz(v) (int)v.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define rep(i, a, b) for (int i = a; i <=b; ++i)
#define pii pair<int, int>
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;

const ll md = 1e9 + 7;
const ll inf = LLONG_MAX;

// Graph Grid //
int dirs[] = {0, 1, 0, -1, 0};
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};

// print matrix
void print(vector<vector<int>> &v)
{
  for (auto &c : v)
  {
    for (auto i : c)
      cout << i << " ";
    cout << "\n";
  }
}

// print 1-D vector
void pri(vi &v)
{
  for (int i : v)
    cout << i << ' ';
  cout << '\n';
}

// pow(x,n), INT_MIN <=n<= INT_MAX, 'n' is an int and 'x' is a double
double myPow(double x, int n)
{
  double ans = 1.0;
  long long nn = n;
  if (nn < 0)
    nn *= (-1);

  while (nn)
  {
    if (nn % 2) // if n is odd
    {
      ans *= x;
      nn -= 1;
    }
    else // n is even
    {
      x *= x;
      nn /= 2;
    }
  }

  if (n < 0) // power is negative
    ans = 1 / ans;

  return ans;
}

// pow(x,n)%mod
int binaryExponentiation(int x, int n, int mod)
{
  int res = 1;
  while (n > 0)
  {
    if (n & 1)
      res = (res* 1LL * x) % mod;
    x = (x * 1LL * x) % mod;
    n >>= 1;
  }
  return res;
}

// bool sieve[1e8];

// void createSieve(int N) 
// {
//     for(int i=0;i<1e8;i++)
//     sieve[i]=true; // initially assuming all numbers are prime
    
//     sieve[1]=false;
    
//     for(int i=2;i*i<=N;i++)
//     if(sieve[i])
//     for(int j=i*i;j<=N;j+=i)
//     sieve[j]=false;
        
// }


vector<bool>visited(1e5+1,false);
vi order(1e5+1,0);
vi hh;

bool solve(int src,vi adj[])
{
  visited[src]=true;
  order[src]=1;
  hh.pb(src);
  for(auto x:adj[src])
  {
    if(!visited[x])
    {
      bool conf=solve(x,adj);
      if(conf) return true;
    }
    else if(order[x]) return true;
  }
  order[src]=0;
  hh.pop_back();
  return false;
}

bool isCycle(int V,vi adj[])
{
  for(int i=1;i<=V;i++)
  {
    if(!visited[i])
    {
      bool c=solve(i,adj);
      if(c) return true;
    }
  }
  return false;
}

void solve()
{
  int n,m;
  cin>>n>>m;
  
  vi adj[n+1];
  forn(i,m)
  {
    int u,v;
    cin>>u>>v;
    adj[u].pb(v);
  }
  
  bool res=isCycle(n,adj);
  if(!res)
  {
    cout<<"IMPOSSIBLE";
    return;
  }
  
  int cnt=0;
  for(auto i:order)
  if(i) cnt++;
  
  cout<<cnt<<'\n';
  // for(int i=1;i<=n;i++)
  // if(order[i]) cout<<i<<' ';
  pri(hh);
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // int t;
  // cin >> t;
  // while (t--)
    solve();

  return 0;
} 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}