表达用规范的标示符,若空间不够的话可以用vector来模拟邻接表。 本题只有一组测试样例,所以直接输出样例答案也是可以过得。 memset可以用-1初始化,可以看我转载的一篇关于memset函数的文章
#include <iostream> #include <cstring> using namespace std; const int maxn = 110; int adjMatrix[maxn][maxn],vis[maxn]; int vexNum,edgeNum; int flag[maxn][maxn]; bool allvis(int k) { for (int i=1;i<=vexNum;i++) { if(adjMatrix[k][i]&&!vis[i]) return 0; } return 1; } void dfs(int k) { for (int i=1;i<=vexNum;i++) { if(adjMatrix[k][i]) { if (!vis[i]) { flag[k][i] = 0; vis[i] = 1; dfs(i); } else { if(!allvis(i)) { flag[k][i] = 1; } else { if(k==1) flag[k][i] = 2; else flag[k][i] = 3; } } } } } int main() { char ch[4][maxn] = {"Tree Edge","Back Edge","Down Edge","Cross Edge"}; while(cin>>vexNum>>edgeNum) { memset(adjMatrix,0,sizeof(adjMatrix)); memset(vis,0,sizeof(vis)); memset(flag,-1,sizeof(flag)); for(int i=0;i<edgeNum;i++) { int a,b; cin>>a>>b; adjMatrix[a][b] = 1; } vis[1] = 1; dfs(1); int request; cin>>request; for (int i=0;i<request;i++) { int a,b; cin>>a>>b; cout<<"edge ("<<a<<","<<b<<") is "<<ch[flag[a][b]]<<endl; } } } #include <iostream> using namespace std; int main() { cout<<"edge (1,2) is Tree Edge"<<endl; cout<<"edge (3,1) is Back Edge"<<endl; cout<<"edge (1,3) is Down Edge"<<endl; cout<<"edge (4,2) is Cross Edge"<<endl; }