HTML & JS
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll ;
typedef vector<ll> vl ;
typedef vector<pll> vll ;
typedef vector<vll> vvll ;

#define sz(v)  (static_cast<long long int>( v.size() ) )
#define all(v)  v.begin(), v.end()
#define WHITE (0)
#define  GREY (1)
#define  BLACK (2)

ll n, m;
vvll g ;
vl vis;
ll k ;
vl egs ;
bool isCycl=false ;
map< pll, ll> mp ;

void dfs(ll src, vvll &gx) {
       
       vis[src]=GREY ;
    
       for(ll i=0 ; i< sz(gx[src]) ; i++)
            if( vis[ gx[src][i].first ] == WHITE && !isCycl)
                dfs( gx[src][i].first, gx );
            else if( vis[ gx[src][i].first ]==GREY )
                { isCycl = true ; return ; }
       
       vis[src]=BLACK;
}

bool isOk() {
    
    isCycl=false;
    vis.erase( all(vis) );
    vis.assign(n, WHITE);
    egs.erase(all(egs));

    vvll gx( n, vll() ) ;
    for(ll i=0 ; i <n ; i++)
       for(ll j=0 ; j< sz(g[i]) ; j++)
          if( g[i][j].second > k )
              gx[i].push_back( g[i][j] );  
          else 
                 egs.push_back( mp[pll(i, g[i][j].first)] )  ;
  
 for(int i=0 ; i< sz(gx) ; i++)
    if( vis[i]== WHITE && !isCycl)
        dfs(i, gx) ;  
    
    
    return !isCycl;
}

int main()
{
    cin>>n>>m ;
    
    g.assign(n, vll());
    set<ll> st ;
    
    for(ll i=0; i<m ; i++)
    {
        ll from, to, c ;
        cin>>from>>to>>c  ;
        g[--from].push_back( pll(--to,  c) ) ;
        st.insert(c) ;
        mp[ pll(from, to) ]=(i + 1) ;
    }
    
    vl C(st.begin(), st.end() ) ;
   
   
   ll l=0, r=sz(C)-1, ans=-1 ; vl x ;
   while( l <= r) 
   {
       ll m =  l +  (  (r-l) >> 1 ) ;
       k = C[m] ;
       
       if( isOk() )
            { ans=k ; x.erase(all(x)); x = egs ; r=m-1 ; }
       else 
          l=m+1;
   }
   
  sort(all(x));
  ll esz = sz(x) ; 
  cout << ans << ' ' << esz << '\n' ;
  for(auto z: x)cout << z << ' ' ;
  return 0 ;   
}

Generated URL:

Output:

7 35
3 4 5 6 7 8 10 11 12 14 15 16 17 18 19 20 22 23 24 25 27 28 29 30 31 32 33 34 35 36 39 42 43 44 45 
Report Bug

Free Online Course By GeeksforGeeks