Euler's path is the path that can be "one stroke" from one point to the whole picture;
In a non-oriented graph: If the degree of each point is an even number then the graph is a Euler loop, and if there are up to 2 odd points, then the starting and arriving points must be
The 2 point, then the path is the Euler road; (if the diagram is connected)
In the direction graph: If each store has the same degree and degree of entry, then it is the Euro-loop, if only 2 points of the out degree is not equal to the degree of penetration, and where
The penetration of a point = out of +1, another point in the degree of +1= out, then the European pull Road; (Prerequisite diagram connectivity)
HDU 1116
Since the characters are from the first to the last one, so use a forward graph
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace Std;
Char ch[1003];
int map[1003][1003],n,m,pa[10003];
int r[10003],c[10003],vis[10003];
stack<int>s;
void Inint ()
{
int i;
for (i=1;i<=26;i++)
{
Pa[i]=i;
}
}
int find (int x)
{
if (X!=pa[x])
Pa[x]=find (Pa[x]);
return pa[x];
}
int main ()
{
int i,j;
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &m);
memset (vis,0,sizeof (VIS));
Inint ();
Memset (r,0,sizeof (R));
Memset (C,0,sizeof (c));
memset (map,0,sizeof (map));
for (i=0;i<m;i++)
{
scanf ("%s", &ch);
int L=strlen (CH);
map[ch[0]-' A ' +1][ch[l-1]-' a ' +1]=1;
r[ch[0]-' A ' +1]++;c[ch[l-1]-' a ' +1]++;
int x, y;
X=find (ch[0]-' a ' + 1);
Y=find (ch[l-1]-' a ' + 1);
vis[ch[0]-' a ' +1]=1;
vis[ch[l-1]-' a ' +1]=1;
if (x!=y)
Pa[x]=y;
}
int sum=0;
for (i=1;i<=26;i++)
{
if (Pa[i]==i&&vis[i])
{
sum++;
}
if (sum>1)
Break
}
if (sum>1)//Not connected
{
printf ("The door cannot be opened.\n");
Continue
}
sum=0;
for (i=1;i<=26;i++)
{
if (vis[i]&& (c[i]!=r[i))//search for points with different degrees of degree
{
sum++;
S.push (i);
}
}
if (sum>2)//Extra 2
printf ("The door cannot be opened.\n");
else if (sum==0)///out-of-degree in the same
printf ("Ordering is possible.\n");
else if (sum==2)
{
int x1,x2;
X1=s.top ();
S.pop ();
X2=s.top ();
S.pop ();
if (c[x1]+1==r[x1) && (c[x2]==r[x2]+1) | | (C[x2]+1==r[x2]) && (c[x1]==r[x1]+1))//Determine whether the condition is established
{
printf ("Ordering is possible.\n");
}
else printf ("The door cannot be opened.\n");
}
Else
printf ("Ordering is possible.\n");
}
}
Learned a bit about Euler's road today.