Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you want to do is a <a href="http://en.wikipedia.org/wiki/Eulerian_path" rel="nofollow">Eulerian Path</a>.. I had solved the same problem on <a href="http://www.codechef.com/problems/WORDS1" rel="nofollow">Codechef</a>. This is my Code if you wanna use.. Plz tell me if you need a explanation,it is very easy to understand though.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string.h&gt; #include &lt;string&gt; using namespace std; int visit[26]; int adj[26][26]; int count=0; void scc(int i) //Strongly COnnected Component { visit[i]=-1;//visiting for(int t=0;t&lt;26;t++) { if(adj[i][t]&gt;0 &amp;&amp; visit[t]==0)//not visited yet scc(t); } visit[i]=1; count++; } int main() { string in; int t,n,k,nv,counta,countb,flag; int a[26],b[26]; cin &gt;&gt; t; while(t--) { cin &gt;&gt; n; memset(a,0,26*sizeof(int)); memset(b,0,26*sizeof(int)); memset(visit,0,26*sizeof(int)); memset(adj,0,26*26*sizeof(int)); k=26;count=0;counta=0;countb=0;flag=0;nv=0; while(n &gt; 0) { n--; cin &gt;&gt; in; a[in[0]-'a']++; b[in[in.size()-1]-'a']++; adj[in[0]-'a'][in[in.size()-1]-'a'] = 1; adj[in[in.size()-1]-'a'][in[0]-'a'] = 1; } for(int i=0;i&lt;26;i++) if(a[i]&gt;0) { scc(i); break; } for(int i=0;i&lt;26;i++) if(a[i]!=0 || b[i]!=0) nv++; if(count!=nv) flag=1; while(k &gt; 0 &amp;&amp; flag!=1 ) { if(a[k-1]-b[k-1] == 1) counta++; else if(b[k-1]-a[k-1] == 1) countb++; else if(a[k-1]!=b[k-1]) flag = 1; k--; } if(flag==0 &amp;&amp; counta==countb &amp;&amp; ( counta==1 || counta ==0)) cout &lt;&lt; "Ordering is possible." &lt;&lt;endl; else cout &lt;&lt; "The door cannot be opened." &lt;&lt;endl; } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload