Menu

Mail Delivery | CSES - Algoritmit ongelmanratkaisussa
📰

Mail Delivery | CSES - Algoritmit ongelmanratkaisussa

DEV Community: cpp·Guhan M·6 days ago
#80yw36EJ
#devtocpp#devto#dev#include#graph
Long article (330 words) — no AI summary yet.
Reading 0:00
15s threshold

Problem: https://cses.fi/alon/task/1691

#include <iostream> #include <vector> #include <utility>

using namespace std ;

vector < int > order ;

void dfs ( int u , vector < vector < pair < int , int >>>& graph , vector < bool >& visited ) { while ( ! graph [ u ] . empty ( ) ) { auto [ v , idx ] = graph [ u ] . back ( ) ; graph [ u ] . pop_back ( ) ;

if ( visited [ idx ] ) continue ;

visited [ idx ] = true ; dfs ( v , graph , visited ) ; } order . push_back ( u ) ; }

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

int n , m ; cin >> n >> m ;

vector < vector < pair < int , int >>> graph ( n + 1 ) ; for ( int i = 0 ; i < m ; i ++ ) { int a , b ; cin >> a >> b ; graph [ a ] . push_back ( { b , i } ) ; graph [ b ] . push_back ( { a , i } ) ; }

for ( int u = 0 ; u <= n ; u ++ ) { if ( graph [ u ] . size ( ) % 2 == 1 ) { cout << "IMPOSSIBLE\n" ; return 0 ; } }

vector < bool > visited ( m + 1 , false ) ; dfs ( 1 , graph , visited ) ;

if ( order .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.