CodeForces 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 ai, 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 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
2
1 2
0
7
1 2
2 3
3 1
4 5
5 6
6 7
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(并查集)的更多相关文章
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
- CodeForces 698B Fix a Tree (并查集应用)
当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
随机推荐
- e638. 向剪切板获取和粘贴图像
// If an image is on the system clipboard, this method returns it; // otherwise it returns null. pub ...
- (转)I 帧和 IDR 帧的区别
I 帧和 IDR 帧的区别:http://blog.csdn.net/skygray/article/details/6223358 IDR 帧属于 I 帧.解码器收到 IDR frame 时,将所 ...
- Ubuntu telnet
首先在Ubuntu中安装xinetd(它是inetd替代品): sudo apt-get install xinetd 再安装telnetd,在Ubuntu中没有telnetd这个软件包,它是包含在i ...
- TF42064: The build number already exists for build definition error in TFS2010
In TFS2008, deleting a build removes it from the database itself. If you delete a build called Build ...
- 《C++ Without Fear》 第1章 第一个C++程序
机器码,CPU的“母语”,每条计算机指令就是一个由1和0构成的独一无二的组合(或代码). endl是“endl line”的缩写,所以它应该念作“end ELL”而不是“end ONE”.
- [Java并发包学习七]解密ThreadLocal
概述 相信读者在网上也看了非常多关于ThreadLocal的资料,非常多博客都这样说:ThreadLocal为解决多线程程序的并发问题提供了一种新的思路:ThreadLocal的目的是为了解决多线程訪 ...
- wamp下修改mysql root用户的登录密码方法
wamp环境安装之后mysql的root密码为空的,我们希望给它设置一个密码; 1.安装好wamp后,运行WampServer程序,进入MYSQL控制台: 2.进入控制台后,提示输入密码(不用输入任何 ...
- [spring] 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 26 in XML document from ...
- PVS 7.6 部署教程
PVS 7.6 部署教程 1 PVS介绍 Citrix Provisioning Services採用流技术通过网络将单一标准桌面镜像,包含操作系统和软件按需交付给物理虚拟桌面.一方面实现同型号机器单 ...
- Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...