PAT 两点间有路径吗? (并查集) C++

题目:

对于给定的无向图以及图中的两个顶点,计算两个顶点所在的连通分量中的顶点数,并且判断这两个顶点之间是否有路径。

输入格式:
第一行是不超过20的正整数N,表示图有N个顶点,顶点的编号即0~N-1;

接下来N行,是N*N的邻接矩阵,矩阵的元素间用空格分隔;

最后一行是用空格隔开的两个顶点编号v和w

输出格式:
第一行输出v所在的连通分量的顶点数

第二行输出w所在的连通分量的顶点数

第三行,若v和w之间有路径,则输出Yes,否则输出No

注意:当v和w是同一个顶点时,认为v和w之间是有路径的。

输入样例:

对于这个图:在这里插入图片描述

8
0 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0
1 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 1 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1 3

输出样例:

5
2
No

思路:

这道题发现可以用并查集来进行求解,不过这里唯一需要注意的是counts这个计数的数组,在join函数中一定要用max函数求出这两个counts中的最大值,要不然无法将其合并。

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
const int maxn = 30;
int pre[maxn];
int counts[maxn];
int matrix[maxn][maxn];

int find(int x)
{
	return pre[x] == x ? x : (pre[x] = find(pre[x]));
}

void join(int x, int y)
{
	int find1 = find(x);
	int find2 = find(y);
	if (find1 != find2)
	{ 
		pre[find1] = find2;
		//counts[find2]++;
		counts[find2] = max(counts[find1], counts[find2]) + 1;
	}

}
int main()
{
	for (int i = 0; i < maxn; ++i)
		pre[i] = i;
    fill(counts,counts+maxn,1);
	cin >> n;
	int t;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cin >> t;
			matrix[i][j] = t;
			if (t == 1 && matrix[j][i] == 0)
				join(i, j);
		}
	}
	int n1, n2;
	cin >> n1 >> n2;
	int findn1 = find(n1);
	int findn2 = find(n2);
	cout << counts[findn1] << endl << counts[findn2] << endl;
	if (findn1 == findn2)
		cout << "Yes";
	else
		cout << "No";

	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想读取.pat文件并添加内容,可以按照以下步骤操作: 1. 使用C++的fstream库打开.pat文件,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 2. 读取文件内容并添加新内容,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; // 读取文件内容 std::string line; while (std::getline(file, line)) { std::cout << line << '\n'; } // 添加新内容 std::ofstream outfile("example.pat", std::ios::app); if (outfile.is_open()) { outfile << "New content\n"; std::cout << "New content added successfully!\n"; } else { std::cerr << "Failed to add new content!\n"; } } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 3. 关闭文件,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; // 读取文件内容 std::string line; while (std::getline(file, line)) { std::cout << line << '\n'; } // 添加新内容 std::ofstream outfile("example.pat", std::ios::app); if (outfile.is_open()) { outfile << "New content\n"; std::cout << "New content added successfully!\n"; } else { std::cerr << "Failed to add new content!\n"; } // 关闭文件 file.close(); outfile.close(); } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 这样您就可以读取.pat文件并添加新内容了。请注意,在添加新内容时,我们使用了std::ofstream库和std::ios::app参数来打开文件以追加内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值