5-32 "Seven-bridge Problem" (25 points) in Fort Lauderdale

Source: Internet
Author: User

Title Description:

Fort Königsberg is a city on the river, which contains two islands and seven bridges that connect them, as shown in.

Can you walk through such seven bridges and only once per bridge? The Swiss mathematician Euler (Leonhard euler,1707-1783) finally solved the problem and created topology.

This problem can now be described as the question of whether Euler loops exist. The Euler circuit refers to a loop that does not leave the pen on paper, but only once per side of the picture and can return to the starting point. Now given an no-map, ask if there is a Euler circuit?

Input format:

Enter the first line to give two positive integers, each of which is a node countNN1\le N\le 10001≤N≤1000) and number of sidesm m; then mm line corresponds to mm edge, each row gives a pair of positive integers, respectively, the number of two nodes connected directly to the bar (node from 1 to nn number).

Output format:

The Joaura loop exists then outputs 1, otherwise outputs 0.

Input Sample 1:
6 101 22 33 14 55 66 41 41 63 43 6
Output Example 1:
1
Input Sample 2:
5 81 21 32 32 42 55 35 43 4
Output Example 2:
0



第一次我不自量力采用了BFS深度搜索的方式解决,果然最后两个测试点超时,下面 是我第一次尝试的代码:

#include <iostream>
#include <vector>
#define MAX 1000
using namespace Std;
int G[max][max];
int from;
int Been[max][max];
int cnt=1;
void Clear (int n)
{
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
BEEN[I][J] = 0;
}
BOOL BFS (int fr,int n,int m)
{
int Been[max][max];
for (int j=1; j<=n;j++)
{
if (j!=fr && been[fr][j]==0 && g[fr][j]==1)
{
cout<< "fr:" <<fr<< "to" <<j<<endl;
cout<< "CNT is" <<cnt<<endl;
if (J==from)
{
if (cnt==m) return true;
cout<< "Minus" <<endl;
cnt--;
Continue
}
cnt++;
Been[fr][j]=been[j][fr]=1;
if (cnt==m) return true;
Return BFS (J,N,M);
}
}
return false;
}
int main ()
{
int n,m,a,b;
cin>>n>>m;
int temp = m;
while (temp--)
{
cin>>a>>b;
G[a][b]=g[b][a] = 1;
}
for (int i=1;i<=n;i++)
{
Clear (n);
CNT = 1;
from = i;
if (BFS (i,n,m)) {
cout<<1;
return 0;
}
}
cout<<0;
return 0;
}

 

5-32 "Seven-bridge Problem" (25 points) in Fort Lauderdale

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.