刚开始从0开始找。。TL了,换成从n-1开始找。AC了。。
#include <iostream>
#include <cstring>
using namespace std;
bool adj[200][200];
bool isVis[200];
int n;
bool dfs(int cur) {
if(cur == 0) return true;
for(int i = 0; i < n; i++) {
if(!isVis[i] && adj[cur][i]) {
isVis[i] = true;
if(dfs(i)) return true;
isVis[i] = false;
}
}
return false;
}
int main() {
int m;
while(cin >> n && n != 0) {
cin >> m;
memset(adj, false, sizeof(adj));
memset(isVis, false, sizeof(isVis));
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[b][a] = true;
}
isVis[n - 1] = true;
if(dfs(n - 1)) cout << "I can post the letter/n";
else cout << "I can't post the letter/n";
}
return 0;
}