标签:can more solution miss Once onclick ros eve lin
Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 8841 | Accepted: 3854 | Special Judge |
Description
Input
Output
Sample Input
4 5 1 2 1 4 2 3 2 4 3 4
Sample Output
1 2 3 4 2 1 4 3 2 4 1
代码分为递归和非递归版本,POJ磁盘满了代码还没交过。。。2018-6-18
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 7 using namespace std; 8 const int MAXN = 1e4 + 7; 9 const int MAXM = 5e4 + 7; 10 11 int n, m, first[MAXN], sign, vis[MAXN]; 12 13 struct Edge { 14 int to, w, next; 15 } edge[MAXM * 4]; 16 17 inline void init() { 18 for(int i = 0; i <= n; i++ ) { 19 first[i] = -1; 20 vis[i] = 0; 21 } 22 sign = 0; 23 } 24 25 inline void add_edge(int u, int v, int w) { 26 edge[sign].to = v; 27 edge[sign].w = w; 28 edge[sign].next = first[u]; 29 first[u] = sign++; 30 } 31 32 void dfs(int x) { 33 for(int i = first[x]; ~i; i = edge[i].next) { 34 int to = edge[i].to; 35 if(!vis[i]) { 36 vis[i] = 1; 37 dfs(to); 38 } 39 } 40 printf("%d\n", x); 41 } 42 43 int main() 44 { 45 while(~scanf("%d %d", &n, &m)) { 46 init(); 47 for(int i = 1; i <= m; i++ ) { 48 int u, v; 49 scanf("%d %d", &u, &v); 50 add_edge(u, v, 1); 51 add_edge(v, u, 1); 52 } 53 dfs(1); 54 } 55 56 return 0; 57 }
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 7 using namespace std; 8 const int MAXN = 1e4 + 7; 9 const int MAXM = 5e4 + 7; 10 11 int n, m, first[MAXN], sign, vis[MAXN]; 12 13 struct Edge { 14 int to, w, next; 15 } edge[MAXM * 4]; 16 17 inline void init() { 18 for(int i = 0; i <= n; i++ ) { 19 first[i] = -1; 20 vis[i] = 0; 21 } 22 sign = 0; 23 } 24 25 inline void add_edge(int u, int v, int w) { 26 edge[sign].to = v; 27 edge[sign].w = w; 28 edge[sign].next = first[u]; 29 first[u] = sign++; 30 } 31 32 stack<int>st, ans; 33 34 void eulur(int start) { 35 while(!st.empty()) { 36 st.pop(); 37 } 38 while(!ans.empty()) { 39 ans.pop(); 40 } 41 st.push(start); 42 while(!st.empty()) { 43 int x = st.top(), i = first[x]; 44 while(~i && vis[i]) { 45 i = edge[i].next; 46 } 47 if(~i) { 48 st.push(edge[i].to); 49 //vis[i] = vis[i ^ 1] = 1; 50 vis[i] = 1; 51 first[x] = edge[i].next; 52 } else { 53 st.pop(); 54 ans.push(x); 55 } 56 } 57 } 58 59 60 int main() 61 { 62 while(~scanf("%d %d", &n, &m)) { 63 init(); 64 for(int i = 1; i <= m; i++ ) { 65 int u, v; 66 scanf("%d %d", &u, &v); 67 add_edge(u, v, 1); 68 add_edge(v, u, 1); 69 } 70 eulur(1); 71 while(!ans.empty()) { 72 printf("%d\n", ans.top()); 73 ans.pop(); 74 } 75 } 76 77 return 0; 78 }
标签:can more solution miss Once onclick ros eve lin
原文地址:https://www.cnblogs.com/Q1143316492/p/9194298.html