UVa10596 - Morning Walk(并查集)

本文全面覆盖信息技术领域的各个方面,从基础的前端开发、后端开发到移动应用开发,再到游戏开发、大数据开发、AI音视频处理等细分领域。深入探讨了各种技术工具、开发环境、测试方法、运维实践,以及最新的云计算、区块链、隐私计算等前沿技术。无论是初学者还是资深开发者,都能从中获得宝贵的知识和灵感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值