wenbao与搜索
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
http://agc013.contest.atcoder.jp/tasks/agc013_b
给n个点,m条边,找一条路径,要求与首尾相连的所有点必须在路径里面
两次dfs搞定(仔细体会为什么?)
很好的一个题
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 const int maxn = 1e5+10; 5 vector<int> v[maxn]; 6 bool vis[maxn]; 7 int a[maxn], num; 8 void d(int x){ 9 for(int i = 0; i < v[x].size(); ++i){ 10 int xx = v[x][i]; 11 if(vis[xx]) continue; 12 vis[xx] = true, a[++num] = xx; 13 d(xx); return ; 14 } 15 } 16 int main(){ 17 int n, m, x, y, num2; 18 scanf("%d%d", &n, &m); 19 for(int i = 0; i < m; ++i){ 20 scanf("%d%d", &x, &y); 21 v[x].push_back(y), v[y].push_back(x); 22 } 23 vis[1] = true; 24 a[num = 1] = 1; 25 d(1), num2 = num, d(1); 26 printf("%d\n", num); 27 for(int i = num2; i >= 1; --i){ 28 if(i == num2) printf("%d", a[i]); 29 else printf(" %d", a[i]); 30 } 31 for(int i = num2+1; i <= num; ++i){ 32 printf(" %d", a[i]); 33 } 34 printf("\n"); 35 return 0; 36 }
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
只有不断学习才能进步!