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

本文介绍了一种通过调整现有道路连接来确保从每个城市都能到达其他所有城市的算法。利用并查集数据结构,在保证最少改动的前提下实现全国范围内的道路网络联通。

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

 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 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 are n cities in Berland and neighboring countries in total and exactly n - 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. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers aibi (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 output t lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.

Examples
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

有n个点n-1条边,删除某些边再增加某些边使图联通。只用并查集即可,输入边的时候把两个点合并,如果两个点已在一个集合,则删除这条边。然后把所有和1不是一个集合的点连一条向1的边,并合并。

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

const int maxn=1000+10;
int n;
int l[1005],r[1005],l1,r1,a[1005],b[1005];
int f[1005];
int fd(int i)
{
    if(f[i]==i) return i;
    return f[i]=fd(f[i]);
}
int main()
{
    int i,j,k,cnt=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++) f[i]=i;
    for(i=1;i<n;i++)
    {
        scanf("%d%d",&l1,&r1);
        int ll=fd(l1),rr=fd(r1);
        if(ll==rr)
        {
            l[++cnt]=l1;r[cnt]=r1;
        }
        else
            f[rr]=ll;
    }
    int now=fd(1),tot=0;
    for(i=2;i<=n;i++)
    {
        if(fd(i)!=now)
        {
            a[++tot]=i;
            b[tot]=1;
            f[fd(i)]=now;
        }
    }
    printf("%d\n",cnt);
    for(i=1;i<=cnt;i++)
    {
        printf("%d %d %d %d\n",l[i],r[i],a[i],b[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值