1、題目:
Problem Description
Input
Output
Sample Input
4 4 ABCD 0 1 0 3 1 2 1 3 0 3 4 4 ABCD 0 1 0 3 1 2 1 3 0 2
Sample Output
1 0
2、參考代碼:
#include <iostream>
using namespace std;
struct ArcNode{
int adjvex;
ArcNode* next;
};
struct VertexNode{
char vertex;
ArcNode* firstedge;
};
class ALGraph{
private:
int vertexNum;
VertexNode adjlist[111];
public:
ALGraph(char* a,int n);
~ALGraph();
void InsertArc(int i,int j);
int judge(int i,int j);
};
ALGraph::ALGraph(char* a,int n){
vertexNum=n;
for(int i=0;i<vertexNum;i++){
VertexNode temp;
temp.vertex=a[i];
temp.firstedge=NULL;
adjlist[i]=temp;
}
}
ALGraph::~ALGraph(){
for(int i=0;i<vertexNum;i++){
ArcNode* p=adjlist[i].firstedge;
while(p){
adjlist[i].firstedge=p->next;
delete p;
p=adjlist[i].firstedge;
}
}
}
void ALGraph::InsertArc(int i,int j){
ArcNode* p=new ArcNode;
p->adjvex=j;
p->next=adjlist[i].firstedge;
adjlist[i].firstedge=p;
}
int ALGraph::judge(int i,int j){
ArcNode* p=adjlist[i].firstedge;
while(p){
if(p->adjvex==j)
return 1;
else
p=p->next;
}
return 0;
}
int main()
{
int i,j,n,e;
char a[111];
while(cin>>n>>e)
{
cin>>a;
ALGraph w(a,n);
while(e--){
cin>>i>>j;
w.InsertArc(i,j);
w.InsertArc(j,i);
}
cin>>i>>j;
cout<<w.judge(i,j)<<endl;
}
return 0;
}