UVa10596 - Morning Walk(并查集)
来源:互联网 发布:保持数据库数据一致性 编辑:程序博客网 时间:2024/05/21 15:47
Kamal is a Motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong from Dinajpur. He was getting fatter inDinajpur as he had no work in his hand there. So, moving to Chittagong has turned to be a blessing for him. Every morning he takes a walk through the hilly roads of charming city Chittagong. He is enjoying this city very much. There are so many roads in Chittagong and every morning he takes different paths for his walking. But while choosing a path he makes sure he does not visit a road twice not even in his way back home. An intersection point of a road is not considered as the part of the road. In a sunny morning, he was thinking about how it would be if he could visit all the roads of the city in a single walk. Your task is to help Kamal in determining whether it is possible for him or not.
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 Rdenotes 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
import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.PrintWriter;import java.io.OutputStreamWriter;import java.io.StreamTokenizer;class Main{public static final boolean DEBUG = false;public static int N = 30;public BufferedReader cin;public PrintWriter cout;public StreamTokenizer tokenizer;public int n, r;public int[] p, d;public void init(){try {if (DEBUG) {cin = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\OJ\\uva_in.txt")));} else {cin = new BufferedReader(new InputStreamReader(System.in));}cout = new PrintWriter(new OutputStreamWriter(System.out));tokenizer = new StreamTokenizer(cin);} catch (Exception e) {e.printStackTrace();}}public String next(){try {tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_EOF)return null;else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {return String.valueOf((int) tokenizer.nval);}return null;} catch (Exception e) {e.printStackTrace();return null;}}public int find(int x){return p[x] == x ? x : (p[x] = find(p[x]));}public boolean input(){String s = next();if (s == null) return false;n = Integer.parseInt(s);s = next();r = Integer.parseInt(s);p = new int[n];d =new int[n];for (int i = 0; i < n; i++) {p[i] = i;d[i] = 0;}for (int i = 0; i < r; i++) {s = next();int a = Integer.parseInt(s);s = next();int b = Integer.parseInt(s);p[find(b)] = find(a);d[a]++;d[b]++;}return true;}public void solve(){boolean ok = true;if (n == 0 || r <= 1) ok = false;int root = find(0);for (int i = 0; i < n && ok; i++) {if (d[i] != 0) {if (find(i) != root || d[i] % 2 != 0) {ok = false;break;}}}if (ok) {cout.println("Possible");} else {cout.println("Not Possible");}cout.flush();}public static void main(String[] args){Main solver = new Main();solver.init();while (solver.input()) {solver.solve();}}}
- UVa10596 - Morning Walk(并查集)
- UVA10596- Morning Walk
- UVa10596 Morning Walk
- uva10596 - Morning Walk
- UVa10596 Morning Walk
- UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)
- UVA - 10596 - Morning Walk(并查集判断连通图)
- Uva 10596 - Morning Walk 欧拉回路基础水题 并查集实现【uva数据修改,代码已更新】
- uva10596 Morning Walk (无向图,欧拉回路,存在性判断)
- uva 10596 - Morning Walk
- UVaOJ 10596 - Morning Walk
- uva 10596 - Morning Walk
- uva 10596 - Morning Walk
- UVa 10596 - Morning Walk
- UVa 10596 - Morning Walk
- 10596 - Morning Walk
- UVA 10596 Morning Walk
- UVaOJ10596---Morning Walk
- sublime自动对齐插件
- ubumtu花屏了
- C/C++变量在内存中的分布
- SQL Server自定义函数
- 本次新生赛部分题解
- UVa10596 - Morning Walk(并查集)
- bcdedit命令
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- PHP解析Excel文件
- 今天新路口面试的题目
- extern关键字总结
- BeanUtils工具包下载及应用
- 设计模式C++实现——观察者模式
- 未能找到元数据文件