Find them, Catch them

本文详细介绍了并查集算法的实现细节,通过一个具体的代码示例展示了如何使用并查集解决连接关系判断的问题。该算法主要用于处理一些不相交集合的合并及查询问题,能够有效地判断元素是否属于同一集合。

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

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34236#problem/B

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
#define N 100005
int p[N],r[N];
int n,m;
int find(int a)
{
	if(p[a]==a) return a;
	int t=find(p[a]);
	r[a]=(r[p[a]]+r[a])%2;
	p[a]=t;
	return t;
}
/*int find( int a ){
	r[ a ] = ( r[ p[ a ] ]+ r[ a ] ) % 2;
	return p[ a ] = p[ a ] == a ? a : find( p[ a ] );
}*/
 
void unin(int a,int b,int f1,int f2)
{
	p[f2]=f1;
	r[f2]=(r[a]+1+r[b])%2;
}
int main()
{
	int t,i,a,b,k1,k2;
	char ch;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		scanf("%c",&ch);
		for(i=1;i<=n;i++)
		{
			p[i]=i;
			r[i]=0;
		}
		while(m--)
		{
			cin>>ch;
			scanf("%d%d",&a,&b);
			if(ch=='A')
			{
				k1=find(a);k2=find(b);
				if(k1!=k2)
					printf("Not sure yet.\n");
				else 
				{
					if(r[a]!=r[b])
						printf("In different gangs.\n");
					else printf("In the same gang.\n");
				}
			}
			else
			{
				k1=find(a);k2=find(b);
				if(k1!=k2)
					unin(a,b,k1,k2);
			}
		}
	}
	return 0;
}


Okay, I understand the instructions. Here's what I'll do: First, I'll add a new method called `saveData` to the `Model` class, which will save the integer coordinates x and y of each point in the arraylist of points to a text file called "points.txt". Here's the code for that: ``` public void saveData() { try { FileWriter fw = new FileWriter("points.txt"); for (Point p : points) { fw.write(p.x + "," + p.y + "\n"); } fw.close(); } catch (IOException e) { e.printStackTrace(); } } ``` Next, I'll modify the constructor of the `Model` class to read the integer coordinates of all the points from the same text file, if it exists, and put them into the arraylist of points. Here's the updated constructor code: ``` public Model() { points = new ArrayList<Point>(); try { File file = new File("points.txt"); if (file.exists()) { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String[] coords = scanner.nextLine().split(","); int x = Integer.parseInt(coords[0]); int y = Integer.parseInt(coords[1]); points.add(new Point(x, y)); } scanner.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } ``` Then, I'll add a protected `shutdown` method to the `Controller` superclass, which will call the `saveData` method of the model and manually terminate the program using `System.exit(0)`. Here's the code for that: ``` protected void shutdown() { model.saveData(); System.exit(0); } ``` Finally, I'll modify the `View` superclass to hide the frame when the user clicks on the "close" button, and add a "window closing" event handler (using an anonymous window adapter) that calls the controller's `shutdown` method. Here's the updated `View` code: ``` public abstract class View extends JFrame { protected Controller controller; public View(Controller controller) { this.controller = controller; addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { controller.shutdown(); } }); } public void close() { setVisible(false); dispose(); } } ``` I'll run the tests using the `Test` class to make sure everything still works, and then I'll run the software using the `Start` class to check that closing the software correctly saves all the point coordinates in the file "points.txt" and that all the points from the previous run are correctly displayed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值