Roads not only in Berland CodeForces 25D 并查集

17 篇文章 0 订阅

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 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.

Sample test(s)
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

虽然是个水题,不过也是自己周赛第一次拿一血的题目啊大笑 纪念一下喽

E题题意:告诉n个城市,然后告诉n-1条已经连通的道路,为使n个城市全都连通,即可以从任意城市到另外一个城市,现在每天可以新修一条路并且关闭一条路,问最少需要多少天可以使n个城市连通。

 

思路:并查集。在输入的时候把每个点之间的关系建立起来,并记录下有多少条多余的路,即已经添加到同一个集合中。然后遍历每一个fa[i]记录集合数num,那么num-1就是需要建立的道路数。

 

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

#define N 10009
using namespace std;

int fa[N];

struct Node
{
    int a,b;
}f[N];

int findfa(int x)
{
    if(x==fa[x])
    return fa[x];
    else
    return findfa(fa[x]);
}

int n;
int g[N];

int main()
{
    while(~scanf("%d",&n))
    {
        int a,b;

        for(int i=1;i<=n;i++)
        fa[i]=i;

        int ans1=0;

        for(int i=1;i<n;i++)
        {
            scanf("%d %d",&a,&b);
            int aa=findfa(a);
            int bb=findfa(b);

            if(aa==bb)
            {
                f[ans1].a=a;
                f[ans1].b=b;
                ans1++;
            }

            if(aa<bb)
            fa[bb]=aa;
            else
            fa[aa]=bb;
        }

//        for(int i=0;i<ans1;i++)
     //   cout<<f[0].a<<" "<<f[0].b<<endl;

        int num=0;

        for(int i=1;i<=n;i++)
        {
            if(fa[i]==i)
            {
                g[num]=i;
                num++;
            }
        }

        printf("%d\n",num-1);

        int i=0;
       while(1)
        {
            if(i>=num-1) break;
            printf("%d %d %d ",f[i].a,f[i].b,g[i]);
            i++;
            printf("%d\n",g[i]);
        }


    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值