uva 10596 - Morning Walk
2014-08-15 13:52
435 查看
Problem H | Morning Walk |
Time Limit | 3 Seconds |
Input
Input will consist of several test cases. Each test case will start with a line containing two numbers. The first number indicates the number of road intersections and is denoted by N (2 ≤ N ≤ 200). The road intersections are assumed to be numbered from 0 to N-1. The second number R denotes the number of roads (0 ≤ R ≤ 10000). Then there will be R lines each containing two numbers c1 and c2 indicating the intersections connecting a road.
Output
Print a single line containing the text “Possible” without quotes if it is possible for Kamal to visit all the roads exactly once in a single walk otherwise print “Not Possible”.
Sample Input | Output for Sample Input |
2 2 0 1 1 0 2 1 0 1 | Possible Not Possible |
#include <iostream> #include <stack> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <fstream> #include <stack> #include <list> #include <sstream> #include <cmath> using namespace std; #define ms(arr, val) memset(arr, val, sizeof(arr)) #define mc(dest, src) memcpy(dest, src, sizeof(src)) #define N 205 #define INF 0x3fffffff #define vint vector<int> #define setint set<int> #define mint map<int, int> #define lint list<int> #define sch stack<char> #define qch queue<char> #define sint stack<int> #define qint queue<int> int degree , fa , visit ; int n, r; int find(int x) { return fa[x] == -1 ? x : fa[x] = find(fa[x]); } void _union(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) { fa[fx] = fy; } } bool check() { int res = 0; for (int i = 0; i < n; i++) { if (visit[i] && fa[i] == -1) { res++; } } if (res != 1) { return false; } for (int i = 0; i < n; i++) { if (visit[i] && (degree[i] & 1)) { return false; } } return true; } void init() { ms(degree, 0); ms(fa, -1); ms(visit, 0); } int main() { int s, e; while (~scanf("%d", &n))//其实的无向图的欧拉回路,看网上说想了好久才想明白 { init(); scanf("%d", &r); while (r--) { scanf("%d%d", &s, &e); _union(s, e); degree[s]++; degree[e]++; visit[e] = visit[s] = 1;//可能存在孤立的点,所以需要标记 } if (check()) puts("Possible"); else puts("Not Possible"); } return 0; }
相关文章推荐
- Morning Walk UVA 10596
- UVA 10596 - Morning Walk
- UVa 10596 Morning Walk ( 欧拉回路 )
- UVA 10596 - Morning Walk
- UVA 10596 Morning Walk
- UVa 10596 - Morning Walk, 赤裸裸的欧拉回路
- UVa10596 Morning Walk
- uva 10596 Morning Walk (欧拉回路)
- UVA 10596 Morning Walk
- UVa 10596: Morning Walk
- Morning Walk - UVa 10596 欧拉回路
- uva 10596 - Morning Walk
- uva 10596 Morning Walk
- uva 10596 Morning Walk
- UVA - 10596 Morning Walk
- uva 10596 Morning Walk
- UVA 10596 - Morning Walk
- uva 10596 - Morning Walk
- UVa 10596 - Morning Walk
- uva 10596 Morning Walk