CF-25D - Roads not only in Berland(并查集或者搜索)

Token 费用爆炸?教你用 Semantic Cache(语义缓存)把 API 成本降低 80% 本文介绍了语义缓存(Semantic Cache)技术在大模型应用中的关键作用。传统Redis缓存基于精确匹配,无法处理用户提问的语义相似性,导致重复调用昂贵的LLM API。语义缓存通过将问题向量化并在向量空间中进行相似度匹配,可识别30%-50%的重复提问,直接返回历史答案。文章详细解析了技术原理(Embedding+向量搜索)、架构设计,并提供了基于Python和ChromaDB的50行核心实现代码。同时指出了生产环境中的三大挑战:阈值设定、隐私保护和多轮对话处理,并给出相应解决方案。该技术能以极低成 阅读详情

D - Roads not only in Berland
Crawling in process... Crawling failed Time Limit:2000MSMemory Limit:262144KB64bit IO Format:%I64d & %I64u

Description

Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There aren cities in Berland and neighboring countries in total and exactlyn - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.

Input

The first line contains integer n (2 ≤ n ≤ 1000) — amount of cities in Berland and neighboring countries. Nextn - 1 lines contain the description of roads. Each road is described by two space-separated integersai,bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.

Output

Output the answer, number t — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then outputt lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the formati j u v — it means that road between citiesi andj became closed and a new road between citiesu andv is built. Cities are numbered from 1. If the answer is not unique, output any.

Sample Input

Input
2
1 2
Output
0
Input
7
1 2
2 3
3 1
4 5
5 6
6 7
Output
1
3 1 3 7

思路1:最早的思路是搜索,每次递归传递当前的点,和它的父节点,而后它的子节点不能为父节点,这样搜到已经搜过的点就说明有环,那就去点这条边,搜下没搜过的点,把边连过去,这样就OK了。

思路2:用并查集,每次找读入两个点的根,如果根一样则说明有环,记录这条边,如果根不一样就合并,因为当前两点是相连的。

             然后遍历一遍,有几个集合,就需要改几条边,每次从环里删条边就连接两集合。

失误:不知道怎么回事,用搜索老是乱整内存,意外终止,调不清楚,vector使用的时候也出很大问题,为什么it!=mp[u].end();它不终止而报错

           耽误了好长时间调试,最后才用并查集搞定了。

#include <stdio.h>
#include <vector>
using namespace std;

int f[1005];
int find(int x)
{
  return f[x]==x?f[x]:f[x]=find(f[x]);
}
void he(int x, int y)
 {
   f[find(x)]=find(y);
 }

int main(void) 
{
	int n, i;
	scanf("%d", &n);
	for(i=1;i<=n;i++)
		f[i] = i;
	vector<pair<int, int> > p;
	vector<int> q;
	for(i=1;i<n;i++)
    {
		int x, y;
		scanf("%d%d", &x, &y);
		if(find(x)==find(y)) 
		p.push_back(make_pair(x, y));///有环记录这条边,待会删去
		else he(x, y);
    }
	for(i=1;i<=n;i++)///找有几个不同集合,就需要改几调边
		if(find(i)==i) q.push_back(i);
	printf("%d\n", q.size()-1);
	for(i=1;i<(int)q.size();i++)///去个环,合并两个集合
		printf("%d %d %d %d\n", p[i-1].first, p[i-1].second, q[i-1], q[i]);
	return 0;
}




SAP RAP 里的 EML 全解:从语言定位到事务语义,再到真实案例的落地方式 摘要:EML(Entity Manipulation Language)是ABAP RAP模型的核心组件,作为ABAP的子集用于操作业务对象(BO)。它通过声明式语法(READ/MODIFY/EXECUTE/COMMIT)统一访问BO的行为与数据,并由RAP运行时自动处理校验、锁定、草稿管理等生命周期。EML将业务语义融入代码,确保与Fiori应用共享同一套校验逻辑,并通过FAILED/REPORTED/MAPPED管道传递错误与消息。其事务控制(IN LOCAL MODE与COMMIT ENTITIES) 阅读详情

相关推荐

最新版Node.js下载安装及环境配置教程(非常详细)

选择对应你系统的Node.js版本。

DN金猿的博客 12万+

Codeforces 25D - Roads not only in Berland(并查集判环)

D. Roads not only in Berland time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roa

在读NLP硕士 428

冒泡排序的并行计算

本人也是并行计算的小白,在这里记录下自己的学习笔记。 今天做了一下冒泡排序的并行计算比较: 串行冒泡排序如下: #include <stdio.h> #include <stdlib.h> #include <windows.h> void BubbleSort(int * pData, int nSize) { if (!pData) { ...

gaoyi的专栏 4490

CodeForces 25D Roads not only in Berland【并查集】

CodeForces 25D Roads not only in Berland【并查集】 CodeForces 25D Roads not only in Berland【并查集】 CodeForces 25D Roads not only in Berland【并查集】 CodeForces 25D Roads not only in Berland【并查集】 CodeForces 25D Roads not only in Berland【并查集】

mengxiang000000 1012

LangChain + PlayWright实战:5分钟搞定网页自动化测试(附完整代码)

本文详细介绍了如何利用LangChain与PlayWright构建智能网页自动化测试系统。通过环境配置、核心架构设计、实战Agent构建及性能优化等步骤,开发者可以快速实现高效的Web操作自动化,提升测试效率。文章还提供了完整代码示例和典型问题解决方案。

weixin_27791839的博客 239

matlab内读取文件目录时斜杠和反斜杠的使用

@matlab内读取文件目录时斜杠和反斜杠的使用TOC matlab内读取文件目录时,斜杠和反斜杠的使用 最近被斜杠和反斜杠弄晕了,于是总结了一下,在此和大家分享一下。 代码 clear; clc; close all; I1= imread('..\zs1.jpg'); %和代码文件夹同目录的图片 (纯反斜杠) I2= imread('../zs2/zs2.j...

weixin_44456694的博客 6572

Codeforces 190E Counter Attack【思维+Bfs】好题!

E. Counter Attack time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Berland has managed to repel the flatland

mengxiang000000 732

中微8S6990外设开发实战:从GPIO配置到低功耗休眠的完整指南

本文提供了中微8S6990增强型8051单片机从GPIO配置到低功耗休眠的完整实战指南。详细讲解了开发环境搭建、GPIO灵活配置、定时器中断、ADC采样优化、PWM电机控制等核心外设的编程要点与避坑技巧,并重点阐述了实现超低功耗休眠的系统性外设管理策略,帮助开发者快速上手并优化产品续航。

weixin_29162439的博客 533

Codeforces 25D Roads not only in Berland

题目链接 题目大意: 有 n 个城市 有 n-1 条路 但不满足任意一个城市可以到达另一个城市 所以要修 新路 顺便 拆旧路 修一条新路的同时需要拆一条旧路 用时一天 问最少需要几天 解题思路: 并查集 输入旧路的同时 找父亲节点 如果这俩城市同一个父亲节点 那这条旧路是可拆的 不是同一个父亲节点 就合并下 这样以后也是同一个人父亲节点了 输入结束后 看有几个 组织 也就是有几个点的父亲节点是自己 它们都是独立的 在它们两两之间修路即可 ...

weixin_43819762的博客 209

如何从 VMware 官网下载最新版本的 VMware Workstation

如何从 VMware 官网下载 VMware Workstation

XiaoqiangClub的博客 5895

在 Windows 11 下运行 OminiParse V2,详细教程【含问题解决细节】

OmniParser V2 是微软推出的 GUI 智能体解析工具,可将屏幕截图转换为结构化元素,帮助 LLM 理解和操作界面。它在检测小图标和推理速度上显著提升,延迟降低 60%,并支持多种 LLM。

简简单单Onlinezuozuo 1090

CodeForces 25D Roads not only in Berland 并查集

题目大概意思是给定n个城市,n-1条边,询问是否可以从其中任何一个点到达其它所有的点。如果不满足的话,你可以删到一些没有用的边,即去掉这些边也可使图连通,然后再加一些边,使得满足题意。 可以一边输入一边划分集合,如果发现加入这条边之前。该两点已经属于同一集合,则这条边明显可以删掉,然后把这些可以删掉的边保存起来。 都输入完之后,对并查集进行处理,如果发现fa[i]==i,则

qqcyclone的专栏 676

2023年全国青少年信息素养大赛复赛真题(C++智能算法复赛真题:带解析)

2023年全国青少年信息素养大赛复赛真题(C++智能算法复赛真题:带解析)

王老师青少年编程 2114

CF-25C - Roads in Berland(水题)

C - Roads in Berland Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 25C Descriptio

nealgavin的专栏 1083

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

[Luogu] CF25D Roads not only in Berland

https://www.luogu.com.cn/problem/CF25D 目录前言思路CODE 前言 乱做的 做的时候还怀疑能不能满足 但是就是过了样例 思路 一开始 以为是图论 这是需要dfs遍历吗? 怎么判断集合呢 一说到集合 就 搞到了 并查集 然后就没有然后了 代码很简单 CODE #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3+10; int p[N],n

qq_34364611的博客 203

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

CF25D Roads not only in Berland

题目链接 样例输入1: 2 1 2 输出1: 0 样例输入2: 7 1 2 2 3 3 1 4 5 5 6 6 7 输出2: 1 3 1 3 7 分析:并查集 code: #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <de

Gold_Medal的博客 214

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Codeforces 25D-Roads not only in Berland 并查集

Roads not only in Berland  Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neig

sxh759151483的博客 256

CF25C Roads in Berland

题目: 给定一个矩阵,g[i][j]表示从点i到点j最短距离 然后,给定k次修改,如果本次修改可以使得最短距离变得更短,请输出所有最短路径的长度 2<=n<=300 1<=d i,j <=1000 k(1<=k<=300 解决: 每次只有点a和b的距离修改了,那么,我们只要更新与a和b有关的点的距离即可。 代码: #include <bits/stdc++.h> using namespace std; const int N = 310; int n

qq_37978559的博客 658

Codeforces Beta Round #25 C题 Roads in Berland

让我WA了无数次的最短路神题。。。。

546

cf25c

There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is ...

weixin_30622107的博客 220

Codeforces 25D-Roads not only in Berland(并查集)

D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Berland Government decided to

Sizaif's 小屋 415

Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland

D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Berland Government decided to ...

weixin_30549175的博客 173

CodeForces 25D Roads not only in Berland

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 25D Description Berland Government decided to improve relations with neighboring

每一次AC都是一种感动 541

CodeForces Roads not only in Berland(并查集)

H -Roads not only in Berland Time Limit:2000MSMemory Limit:262144KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 25D Description Berland Government decided ...

weixin_30371469的博客 181

【CodeForces - 190E】Counter Attack (补图bfs,卡常,dsu)

题干: 无向图中给定n个顶点,m条不存在的边(除了这m条边,其余都存在),求图的连通分量,及每个连通分量的大小。 解题报告: https://codeforces.com/blog/entry/4556 AC代码: #include<cstdio> #include<iostream> #include<algorithm> #include&l...

xuanweiace的博客 607
上一篇: CF-25C - Roads in Berland(水题)
下一篇: CF-25E - Test(KMP)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值