Is This a Real Tree? You should write a program or function which receives a string as input and outputs or returns if the input is an ASCII tree.

  _
\/  /
 \_/
  |
  |


ASCII trees consist of characters / \ | _ spaces and newlines.

The non-whitespace characters connect two edge points of their cells by a line segment:


/ connects the bottom left and top right corners
\ connects the bottom right and top left corners
| connects the middle points of the bottom edge and top edge
_ connects the bottom left and bottom right corners and the middle point of the bottom edge


(Note that this means that | can only connect with | or _ but not with / or \.)

An ASCII picture is called a tree if the following rules apply:


Exactly one point (the root) of exactly one character touches to the bottom edge of the last row.
You can reach any point of any line segment by:


starting from the root
using only the line segments
never going into a downward direction (not even sideways downward)   



Input


A string consisting of the characters / \ | _ space and newline containing at least one non-whitespace character.
You can choose of two input format:


No unnecessary whitespace around the tree (as seen in the examples).
No unnecessary whitespace around the tree (as seen in the examples) except spaces on the right side of the rows to make all rows the same length.

Trailing newline is optional.


Output


A consistent truthy value if the input is an ascii tree.
A consistent falsy value if the input isn't an ascii tree.


Examples

Valid trees:

|




  _
\/  /
 \_/
  |
  |




/ /    \/
\ \____/
 \/
 /
/




 \___/
 /   \
 \___/
   |
   |




   __/
 _/
/




____
  \  ___
 \ \/
  \/\_____/
 \/  \/
  \__/
    |
    |


Invalid trees (with extra explanations which are not parts of the inputs):

\/
 \_______/
  \__   /
   | \_/    &lt;- reachable only on with downward route
   |




_           &lt;- multiple roots




\/          &lt;- multiple root characters




/\          &lt;- multiple roots




|           &lt;- unreachable part

|




 __/
/           &lt;- unreachable parts
|




\____/
 |  |       &lt;- multiple roots




_\__/       &lt;- unreachable parts (_ and \ don't connect to each other)
|


This is code-golf so the shortest entry wins.
Mathematica, 345 300 bytes

Still quite long, but I guess it's a start...

(s=StringSplit;v=Reverse;#=="|"||#=="\\"||#=="/"&amp;[""&lt;&gt;s@#]&amp;&amp;(g={};i=0;(c={i,++j};d={i,j+1/2};e=2d-c;g=Join[g,Switch[#,"|",{d-&gt;{1,0}+d},"/",{c-&gt;c+1},"\\",{e-&gt;{i+1,j}},"_",{c-&gt;d,d-&gt;e,e-&gt;c},_,{}]])&amp;/@Characters[++i;j=0;#]&amp;/@{##};Sort@VertexOutComponent[Graph@g,g[[1,1]]]==Union@@List@@@g)&amp;@@v@s[#,"
"])&amp;


Here is a slightly ungolfed version:

(
  s = StringSplit;
  v = Reverse;
  # == "|" || # == "\\" || # == "/" &amp;[
      "" &lt;&gt; s@#
      ] &amp;&amp; (
      g = {};
      i = 0;
      (
           c = {i, ++j};
           d = {i, j + 1/2};
           e = 2 d - c;
           g = Join[g, Switch[#,
              "|", {d -&gt; {1, 0} + d},
              "/", {c -&gt; c + 1},
              "\\", {e -&gt; {i + 1, j}},
              "_", {c -&gt; d, d -&gt; e, e -&gt; c},
              _, {}
              ]]
           ) &amp; /@ Characters[
          ++i;
          j = 0;
          #
          ] &amp; /@ {##};
      Sort@VertexOutComponent[Graph@g, g[[1, 1]]] == 
       Union @@ List @@@ g
      ) &amp; @@ v@s[#, "\n"]
) &amp;


This defines an unnamed function which takes the string as input and returns True or False. 

The basic idea is first to check that there's a single root, and then to build an actual (directed) Graph object to check if all vertices can be reached from the root. Here is how we build the graph:

Imagine an integer grid overlaid onto the ASCII art, where integer coordinates correspond to the corners of the character cells. Then on each of the cells there are six relevant points that can be connected. Here is an example, where I've also labelled the points a to f:

     |                 |
     |                 |
---(2,3)---(2.5,3)---(3,2)---
     | d      e      f |
     |                 |
     |                 |
     |                 |
     |                 |
     |                 |
     |                 |
     | a      b      c |
---(2,2)---(2.5,2)---(3,2)---
     |                 |
     |                 |


So we can build a graph whose vertices are these half-integer coordinates, and whose edges are determined by the non-space characters in the input. | connects b to e, / connects a to f and \ connects c to d. Note that these must be directed edges to ensure that we're never moving downwards while traversing the graph later. For _ we can go either way, so in theory we need four edges a -&gt; b, b -&gt; a, b -&gt; c, c -&gt; b. However, we can notice that all that matters is that there's a cycle containing all three vertices, so we can shorten this to three edges: a -&gt; b, b -&gt; c, c -&gt; a.

Building this graph is quite simple in Mathematica, because any object can act as a vertex, so I can actually build a graph whose vertices are the coordinate pairs.

Finally, we check that every vertex can be reached from the root. The root's coordinate is easily found as the very first vertex we added to the graph. Then the shortest way I have found to check if all vertices can be reached is to check if the VertexOutComponent of the root (i.e. the set of all vertices reachable from the root) is identical to the set of all vertices in the graph.