题意:
给你一个无向图,找最长路.
俩份代码,感觉map[][]简单易懂啊
#include<stdio.h> #include<iostream> #include<sstream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 26 int n, m; int mp = -1; struct Edge { int num; int s; int e; }; int nn[N][N]; int ni[N]; Edge edges[N]; int vis[N]; int dfs(int cn, int cur) { for(int i = 0; i < ni[cn]; i++) { if(vis[nn[cn][i]]) continue; Edge edge = edges[nn[cn][i]]; int e = edge.e == cn ? edge.s : edge.e; vis[nn[cn][i]] = 1; dfs(e, cur + 1); vis[nn[cn][i]] = 0; } mp = mp < cur ? cur : mp; return cur; } int main(const int argc, char** argv) { //freopen("d:\1.txt", "r", stdin); while (cin >> n >> m && (n && m)) { memset(vis, 0, sizeof(vis)); memset(nn, 0, sizeof(nn)); memset(ni, 0, sizeof(ni)); for(int i = 0; i < m; i++) { Edge edge; edge.num = i; cin >> edge.s >> edge.e; edges[i] = edge; nn[edge.s][ni[edge.s]++] = i; nn[edge.e][ni[edge.e]++] = i; } for(int i = 0; i < n; i++) { dfs(i, 0); } cout << mp << endl; mp = 0; } return 0; }
#include<stdio.h> #include<iostream> #include<sstream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 30 int n, m; int mp = -1; int nn[N][N]; int dfs(int cn, int cur) { for(int i = 0; i < n; i++) { if(nn[cn][i]) { nn[cn][i]--; nn[i][cn]--; dfs(i, cur + 1); nn[cn][i]++; nn[i][cn]++; } } mp = mp < cur ? cur : mp; return cur; } int main(const int argc, char** argv) { //freopen("d:\1.txt", "r", stdin); while (cin >> n >> m ) { if(n==0&&m==0) return 0; memset(nn, 0, sizeof(nn)); mp = 0; int s, e; for(int i = 0; i < m; i++) { cin >> s >> e; nn[s][e]++; nn[e][s]++; } for(int i = 0; i < n; i++) { dfs(i, 0); } cout << mp << endl; } return 0; }