[Daimayuan] pSort(C++,强连通分量)

题目描述

有一个由 n n n 个元素组成的序列 a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an;最初,序列中的每个元素满足 a i = i a_i=i ai=i

对于每次操作,你可以交换序列中第 i i i 个元素和第 j j j 个元素当且仅当满足 ∣ i − j ∣ = d i |i−j|=d_i ij=di

题目给出序列 b 1 , b 2 , … , b n b_1,b_2,…,b_n b1,b2,,bn d 1 , d 2 , … , d n d_1,d_2,…,d_n d1,d2,,dn,询问是否可以通过若干次交换,使得序列 a a a 和序列 b b b 完全相同。

输入格式

1 1 1 行一个正整数 n n n,含义如上。

2 2 2 n n n 个正整数表示 b 1 , b 2 , … , b n b_1,b_2,…,b_n b1,b2,,bn

3 3 3 n n n 个正整数表示 d 1 , d 2 , … , d n d_1,d_2,…,d_n d1,d2,,dn

输出格式

若能,输出 YES;否则输出 NO

输入 #1
7
4 2 5 1 3 7 6
4 6 6 1 6 6 1
输出 #1
YES
输入 #2
7
4 3 5 1 2 7 6
4 6 6 1 6 6 1
输出 #2
NO
数据范围

1 ≤ n , d i ≤ 100 1≤n,d_i≤100 1n,di100。保证序列 b b b 中元素不重复。

补充说明

原题:CF28B pSort | 洛谷

解题思路

对于题意的理解如下:

if (abs(i - j) == d[i] || abs(i - j) == d[j]) {
	/* i,j是可交换的 */
}

那么我们可以把 a a a序列抽象为一张无向图,可交换关系为无向边。

则强连通分量之内的节点可以随意交换。

也就是说,如果需要交换所有节点都在同一个强连通分量之中,就输出YES

反之,如果需要交换的任意一对节点不在一个强连通分量中,就输出NO

接下来实现代码:

我们采用染色法标记不同的强连通分量,用广度优先搜索对整张图进行染色,时间复杂度为 O ( n 2 ) O(n^2) O(n2)

void bfs(int bg) {
	color++;
	q.push(bg);

	int head, next, i;
	while (!q.empty()) {
		head = q.front();
		q.pop();

		if (book[head]) continue;
		book[head] = true;
		colors[head] = color;

		for (i = 1; i <= n; i++) {
			if (i == head) continue;
			next = abs(head - i);
			if (next == ds[head] || next == ds[i]) q.push(i);
		}
	}
}

我们需要遍历每一个节点进行尝试,所以算法总时间复杂度为 O ( n 3 ) O(n^3) O(n3),可以接受。

for (i = 1; i <= n; i++) {
	if (!colors[i]) bfs(i);
}

最后,AC代码如下:

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
const int max_n = 100;

int bs[max_n], ds[max_n];
int colors[max_n], color = 0;
queue<int>q;
int book[max_n];
int n;

void bfs(int bg) {
	color++;
	q.push(bg);

	int head, next, i;
	while (!q.empty()) {
		head = q.front();
		q.pop();

		if (book[head]) continue;
		book[head] = true;
		colors[head] = color;

		for (i = 1; i <= n; i++) {
			if (i == head) continue;
			next = abs(head - i);
			if (next == ds[head] || next == ds[i]) q.push(i);
		}
	}
}

int main() {
	cin >> n;
	int i;
	for (i = 1; i <= n; i++) cin >> bs[i];
	for (i = 1; i <= n; i++) cin >> ds[i];
	for (i = 1; i <= n; i++) {
		if (!colors[i]) bfs(i);
	}

	for (i = 1; i <= n; i++) {
		if (colors[bs[i]] != colors[i]) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
import time import openpyxl from selenium import webdriver from bs4 import BeautifulSoup # 设置请求头,模拟真实浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36', } # 使用Selenium启动浏览器 driver = webdriver.Chrome() data = [] counter = 1 # 序列号计数器 # 打开网页 for i in range(1,6): url = 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&psort=3&wq=%E6%89%8B%E6%9C%BA&psort=3&pvid=0faa3ec65d444d68a66161cdf464d451&psort=3&page={}&s=61&click=0'.format( (i * 2) - 1) driver.get(url) # 模拟滚动页面,以触发异步请求加载更多商品信息 driver.execute_script('window.scrollTo(0, document.body.scrollHeight);') time.sleep(2) # 获取完整页面内容 html = driver.page_source # 解析网页内容,提取商品名称和价格信息 soup = BeautifulSoup(html, 'html.parser') products = soup.select('.gl-item') for product in products: product_id = product['data-sku'] # 提取产品ID name = product.select('.p-name em')[0].text.strip() product_url = 'https:' + product.select('.p-name a')[0]['href'] # 修改产品URL price = product.select('.p-price strong i')[0].text.strip() data.append([counter, product_id, name, product_url, price]) # 将产品数据添加到列表中 counter += 1 # 每个产品的增量计数器 # 关闭浏览器 driver.quit() # 创建Excel文件并保存数据 wb = openpyxl.Workbook() ws = wb.active ws.append(['top', '商品ID', '商品名称', '商品链接', '价格']) # 添加已修改列顺序的标题行 for item in data: ws.append(item) wb.save('jd_top300.xlsx') print("数据已保存到jd_top300.xlsx文件。")
06-02

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WitheredSakura_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值