Problem H | Morning Walk |
Time Limit | 3 Seconds |
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1537
Problemsetter: Muhammad Abul Hasan
International Islamic University Chittagong
解题思路:
感觉跟上一题一样,都是欧拉回路,这次不用打印较为简单,感觉欧拉回路开始有个模板了,判断结点度数的奇偶,DFS或者BFS或者并查集判断连通。
刚开始理解错题目了,以为可以是欧拉回路或通路,而且看他的输入以为是有向图,差的太远了,故WA了几次~~但改了之后还是WA了很多次,逃了一节课找了很久还是没找出来,最后干脆重新写过 AC了,感觉弱爆了!
1 #include<stdio.h> 2 #include<string.h> 3 #define MAXN 220 4 int road[MAXN][MAXN]; 5 int visit[MAXN]; 6 int note[MAXN]; 7 int sum; 8 9 int DFS(int current, int n) 10 { 11 int j; 12 sum++; 13 visit[current] = 1; 14 for(j=0; j<n; ++j) 15 { 16 if(road[current][j] && !visit[j]) 17 DFS(j, n); 18 } 19 return 1; 20 } 21 22 int main() 23 { 24 int i, j, n, r, flag, x, y; 25 while(scanf("%d%d", &n, &r) != EOF) 26 { 27 memset(road, 0, sizeof(road)); 28 memset(note, 0, sizeof(note)); 29 for(i=0; i<r; ++i) 30 { 31 scanf("%d%d", &x, &y); 32 road[x][y]++; 33 road[y][x]++; 34 note[x]++, note[y]++; 35 } 36 flag = 0; 37 for(i=0; i<n; ++i) 38 { 39 if(note[i]%2) 40 { 41 flag = 1; 42 break; 43 } 44 } 45 if(flag) 46 { 47 printf("Not Possible\n"); 48 } 49 else 50 { 51 memset(visit, 0, sizeof(visit)); 52 sum = 0; 53 DFS(0, n); 54 if(sum != n) 55 printf("Not Possible\n"); 56 else printf("Possible\n"); 57 } 58 } 59 return 0; 60 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<stdio.h> #include<string.h> #define MAXN 202 int road[MAXN][MAXN]; int note[MAXN][2]; int visit[MAXN]; int Dfs(int current, int n) { int j; visit[current] = 1; for(j=0; j<n; ++j) if(note[current][j] != 0 && !visit[j]) Dfs(j, n); } int main() { freopen("input.txt", "r", stdin); int n, r, i, j, x, y, flag, cnt, temp,store; while(scanf("%d%d", &n, &r) != EOF) { memset(road, 0, sizeof(road)); memset(note, 0, sizeof(note)); for(i=0; i<r; ++i) { scanf("%d%d", &x, &y); road[x][y]++; note[x][0]++, note[y][1]++; } cnt = flag = store = 0; for(i=0; i<n; ++i) if(note[i][0] != 0) { memset(visit, 0, sizeof(visit)); Dfs(i, n); for(j=0; j<n; ++j) if(!visit[j] && (note[j][0] != 0 || note[j][1] != 0)) flag = 1; break; } for(i=0; i<n && flag == 0; ++i) { if(note[i][0] != 0 || note[i][1] != 0) { temp = note[i][0] - note[i][1]; if(temp == -1 || temp == 1) { cnt++; store += temp; } else if(temp != 0) { flag = 1; break; } } if(flag == 1) break; } if(flag == 1 || cnt > 2 || store != 0) { printf("Not Possible\n"); } else printf("Possible\n"); } return 0; }