个人赛2C

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, bin, aibi) — 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 iand 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.
题意:给n个点,n-1条边,每天通过加边减边得到一颗覆盖所有点的树,问最少多少天。
并查集裸题,遇到环就拆,然后在不同集合中建边
下面是代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define INF 0x3fffffff
#define maxn 100005
#define mod 998244353
int n,m,k,t,z=0;
int fa[maxn];
bool flag[maxn]={0};
vector<int>a,aa,b;
int fi(int x){
    if(fa[x]==x)
        return x;
    return fa[x]=fi(fa[x]);
}
int x,y,w;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        int fx=fi(x),fy=fi(y);
        if(fx==fy){
            a.push_back(x);
            aa.push_back(y);
            z++;
        }
        else fa[fy]=fx;
    }
    k=fi(1);
    flag[k]=1;
    for(int i=2;i<=n;i++)
        if(flag[fi(i)]==0){
            b.push_back(fi(i));
            flag[fi(i)]=1;
        }
    printf("%d\n",z);
    for(int i=0;i<z;i++)
        printf("%d %d %d %d\n",a[i],aa[i],k,b[i]);
}

全部评论

相关推荐

头像
2025-12-27 13:01
三峡大学 C++
点赞 评论 收藏
分享
02-01 12:05
复旦大学 Java
腾讯的提前批大概率应该是没有笔试的,但是这个时候有相当部分的同学简历估计都没有准备好,没准备好的同学也不用急,大部分都是3月之后开,这个时候开的绝大多数都是神仙打架,问的东西也比较难,打算投递的同学也多看下计算机网络和操作系统,腾讯对这部分的知识问的比较多。另外多刷下牛客的热门题库,刷题注意刷ACM模式,和牛客的周赛题,腾讯有的部门会从这里面出原题。我是@程序员花海关注我,带你了解更多校招资讯!
程序员花海:还没有来得及准备的同学可以看下学习路线:https://www.nowcoder.com/discuss/824693499982315520?sourceSSR=users算法题:https://www.nowcoder.com/feed/main/detail/20e7a999fa04485b88340a274411ca0d?sourceSSR=users八股文:https://www.nowcoder.com/discuss/833102362771251200?sourceSSR=users简历书写方式:https://www.nowcoder.com/discuss/839907820706205696?sourceSSR=users都是以前在牛客发的文章~
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务