H - Roads not only in Berland

Time Limit:2000MS     Memory Limit:262144KB     64bit 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 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 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

这道题目是简单的并查集应用,直接给出AC代码
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int father[1005];
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
struct Node
{
int x,y;
}c[1005];
int num;
int tag[1005];
int res[1005];
int main()
{
int n;
int a,b;
scanf("%d",&n);
for(int i=1;i<=1000;i++)
father[i]=i;
int cnt=0;int num=0;
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&a,&b);
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
father[fa]=fb;
}
else
{
c[cnt].x=a;
c[cnt++].y=b;
}
}
memset(tag,0,sizeof(tag));
int cot=0;
for(int i=1;i<=n;i++)
{
if(!tag[find(i)])
{
res[cot++]=find(i);
num++;
tag[find(i)]=1;
}
}
printf("%d\n",num-1);
int tot=1;
for(int i=0;i<num-1;i++)
{
printf("%d %d",c[i].x,c[i].y);
printf(" %d %d\n",res[0],res[tot]);
tot++;
}
return 0; }

CodeForces Roads not only in Berland(并查集)的更多相关文章

  1. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  2. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  3. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  4. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  5. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  6. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  7. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

  8. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  9. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

随机推荐

  1. python json.dumps 中文字符乱码

      场景:微信公众号推送消息,中文乱码.  Date:2017-05-11 10:58:40.033000,\u4f60\u597d    解决方法: python dumps默认使用的ascii编码 ...

  2. 你是否有遇到过某个实体类字段(属性)过多的情况,不想每次点的话戳进来(C# 反射)

    贴上一段代码: bureaucraticEntities apply = new bureaucraticEntities(); Type tapp= app.GetType(); Type ttmp ...

  3. 使用定时器判断确保某个标签有值才执行方法, 控制js代码执行先后顺序

    使用定时器判断确保某个标签有值才执行方法: var wait = setInterval(function(){ var diqu = $("#diqu").val(); //确保 ...

  4. CentOS7服务器搭建百度贴吧云签到

    由无名智者开发的“百度贴吧云签到”应用是一个每天自动对百度贴吧定时进行云签到的程序.前面准备,已经有安装过mysql的linux服务器.mysql的安装在此不做介绍. 一.安装Apache yum i ...

  5. oracle中LAG()和LEAD()以及over (PARTITION BY)

    LAG()和LEAD()统计函数可以在一次查询中取出同一字段的前N行的数据和后N行的值.这种操作可以使用对相同表的表连接来实现,不过使用LAG和 LEAD有更高的效率.以下整理的LAG()和LEAD( ...

  6. ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...

  7. javascript变量声明前置

    变量声明前置: 所谓的变量声明前置就是在一个作用域块中,所有的变量都被放在块的开始出声明,下面举个例子你就能明白了 var a = 1; function main() { console.log(a ...

  8. 【java】 java 设计模式(1):工厂方法模式(Factory Method)

    工厂方法模式分为三种: 1.普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.首先看下关系图: 举例如下:(我们举一个发送邮件和短信的例子) 首先,创建二者的共同接口:   p ...

  9. linux 安装 nodejs

    原文地址:https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora 1)定位到nodejs的官方源(如果直 ...

  10. callable()

    callable() 用于判断一个对象是否是可调用的,函数或类都可以被调用 In [1]: callable('a') Out[1]: False In [2]: def fun(): ...: pa ...