Home c++ Step by step bypass graph

Step by step bypass graph

Author

Date

Category

Step by step bypass of the graph V Clause the sequence of the vertices U1, U2, …, ur such that:

u1 = ur = v,

Each vertex of the graph, achieving from V, occurs in it at least once,

between any two adjacent vertices of the sequence in the column there is a rib.

A connected un-ore-oriented graph and its vertex V is set. Display any step by step bypassing this graph.

input format

In the first line of the input file, N, M and V numbers are given through a space – the number of vertices and edges in the column and the initial vertex bypass (1 & lt; = n & lt; = 100, 0 & lt; = m & lt; = 10,000, 1 & lt; = V & lt; = n). The following m strings contain two numbers UI and VI through a space (1 ° C = UI, VI & LT; = N); Each such string means that in the graph there is a rib between the vertices UI and VI.

output format

In the first line of the input file, output the number R is the number of vertices in the step by step bypass (; it is guaranteed that bypass that satisfies these restrictions). In the second line, output the numbers U1, U2, …, UR through a space.

Example 1

Enter

3 2 1
12
2 3.

output

5
1 2 3 2 1

Example 2

Enter

4 4 1
12
2 3.
3 4.
4 1.

output

5
1 2 3 4 1

Here is my code:

# include & lt; iostream & gt;
#Include & lt; vector & gt;
Using Namespace STD;
int v;
Vector & lt; int & gt; Way;
Void DFS (int start, vector & lt; bool & gt; & amp; visited, vector & lt; vector & lt; int & gt; & gt; & amp; g) {
  Visited [Start] = True;
  For (int i = 0; i & lt; g [start] .size (); I ++) {
    int u = g [start] [i];
    If (U! = 0 & amp; & amp;! Visited [i]) {
      WAY.PUSH_BACK (I + 1);
      if (i == V-1) {
        Return;
      }
      DFS (I, Visited, G);
    }
  }
}
// Home Function
INT MAIN () {
  Vector & lt; vector & lt; int & gt; & gt; g;
  int n, m;
  CIN & GT; & GT; n & gt; & gt; M & GT; & gt; v;
  g.assign (n, vector & lt; int & gt; (n, 0));
  For (int i = 0; i & lt; m; i ++) {
    int a, b;
    CIN & GT; & GT; a & gt; & gt; b;
    a--; b--;
    g [a] [b] = 1;
    G [b] [a] = 1;
  }
  WAY.PUSH_BACK (V);
  Vector & lt; bool & gt; Visited (N + 1, FALSE);
  // Vector & lt; int & gt; PREV (N + 1, -1);
  DFS (V-1, Visited, G);
  For (int i = 0; i & lt; = n; i ++) {Visited [i] = false;}
  DFS (WAY [WAY.SIZE () - 1] -1, Visited, G);
  COUT & LT; & LT; WAY.SIZE () & lt; & lt; "\ n";
  For (Int i: Way) {
    COUT & LT; & LT; i & lt; & lt; "";
  }
}

The testing system issued an error already on the third test. Help please fix the code.


Answer 1

# include & lt; bits / stdC++. H & GT;
Using Namespace STD;
Vector & lt; int & gt; Used, Paths;
Vector & lt; vector & lt; int & gt; & gt; Graph;
Void DFS (int v) {
  USED ​​[V] = TRUE;
  paths.push_back (V);
  For (Int to: Graph [v]) {
    If (! Used [to]) {
      DFS (TO);
      paths.push_back (V);
    }
  }
}
INT MAIN () {
  STD :: IOS_BASE :: SYNC_WITH_STDIO (0); STD :: CIN.TIE (0); STD :: COUT.TIE (0);
  INT N, M, V;
  CIN & GT; & GT; n & gt; & gt; M & GT; & gt; v;
  Graph = vector & lt; vector & lt; int & gt; & gt; (n + 1);
  Used = vector & lt; int & gt; (n + 1, false);
  For (int i = 0; i & lt; m; i ++) {
    int f = 0, t = 0;
    CIN & GT; & GT; F & GT; & gt; t;
    Graph [F] .Push_back (T);
    Graph [T] .Push_back (F);
  }
  DFS (V);
  COUT & LT; & LT; paths.size () & lt; & lt; '\ n';
  For (int v: paths) {
    COUT & LT; & LT; V & LT; & LT; '';
  }
  Return 0;
}

Here is the test code

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions