http://acm.timus.ru/problem.aspx?space=1&num=1176
把所有的小环路 并成一个大环路
输出路径 注意超栈问题
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<map> #include<queue> #include<stack> #include<cmath> #define LL long long #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF=0x3f3f3f3f; const int N=1005; vector<int>str[N]; vector<bool>visited[N]; vector<int>ans; void dfs(int x,int k) {//cout<<x<<" "<<k<<endl; for(int i=0;i<str[x].size();++i) {//cout<<str[x].size()<<" "<<visited[x].size()<<endl; if(!visited[x][i]) { visited[x][i]=true; ans.insert(ans.begin()+k,str[x][i]); dfs(str[x][i],k+1); } } } int main() { //freopen("data.txt","r",stdin); int n,s; while(cin>>n>>s) { for(int i=1;i<=n;++i) {str[i].clear();visited[i].clear();} for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { int tmp; cin>>tmp; if(tmp==0&&i!=j) {str[i].push_back(j);visited[i].push_back(false);} } } ans.clear(); ans.push_back(s); dfs(s,1); for(int i=0;i<ans.size()-1;++i) { cout<<ans[i]<<" "<<ans[i+1]<<endl; } } return 0; }