Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
2 seconds
256 megabytes
standard input
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.
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 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.
2
1 2
0
7
1 2
2 3
3 1
4 5
5 6
6 7
1
3 1 3 7
并查集
先跑一遍并查及,分成几个集合。在查询父节点的时候,如果x=findfa(a),y=findfa(b) x=y那么,这条道路a-b就应该封闭
再跑一遍并查集,把这几个集合连起来....就得到要修的道路编号了
/* ***********************************************
Author :pk29
Created Time :2015/8/23 11:19:23
File Name :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; bool cmp(int a,int b){
return a>b;
}
int fa[maxn],n;
int a,b;
vector<pair<int ,int> >v1;
vector<pair<int,int> >v2; void init(){
for(int i=;i<=n;i++){
fa[i]=i;
}
v1.clear();
v2.clear();
}
int findfa(int x){
if(x==fa[x])return x;
else return fa[x]=findfa(fa[x]);
}
void Union(int a,int b){
int x=findfa(a);
int y=findfa(b);
if(x!=y){
if(x<y)fa[y]=x;
else fa[x]=y;
return ;
}
else v1.push_back(make_pair(a,b));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout); while(cin>>n){
init();
for(int i=;i<=n-;i++){
scanf("%d %d",&a,&b);
Union(a,b);
}
for(int i=;i<=n;i++){
int x=findfa(i);
for(int j=+i;j<=n;j++){
int y=findfa(j);
if(x!=y){
fa[y]=x;
v2.push_back(make_pair(i,j));
}
} }
cout<<v1.size()<<endl;
for(int i=;i<v1.size();i++){
cout<<v1[i].first<<" "<<v1[i].second<<" ";
cout<<v2[i].first<<" "<<v2[i].second<<endl;
}
}
return ;
}
Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland的更多相关文章
- Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland
C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Beta Round #25 (Div. 2 Only)
Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...
- codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)
题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...
- Codeforces Beta Round #25 (Div. 2)--A. IQ test
IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Beta Round #25 (Div. 2 Only) A. IQ test【双标记/求给定数中唯一的奇数或偶数】
A. IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces Beta Round #25 (Div. 2 Only)E. Test
E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)
题目链接:http://codeforces.com/problemset/problem/118/E 思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #49 (Div. 2)
Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...
随机推荐
- poj 2115 二元一次不定方程
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14765 Accepted: 3719 Descr ...
- ORACLE的impdp和expdp命令【登录、创建用户、授权、导入导出】
使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...
- Scrapy学习-10-Request&Response对象
请求URL流程 Scarpy使用请求和响应对象来抓取网站 通常情况下,请求对象会在spider中生成,并在系统中传递,直到到达downloader,它执行请求并返回一个响应对象,该对象返回发送请求的 ...
- MySQL什么时候会使用内部临时表?
1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...
- UOJ 41. 矩阵变换
Discription 给出一个 N 行 M 列的矩阵A, 保证满足以下性质: 1.M>N. 2.矩阵中每个数都是 [0,N] 中的自然数. 3.每行中, [1,N] 中 ...
- SPOJ 26108 TRENDGCD - Trending GCD
Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...
- VMware 虚拟机下载链接
VMware 14 链接: https://pan.baidu.com/s/1mBeyX2Z6hGpbFc8_UC-sEw 提取码: 462t 密钥:AA510-2DF1Q-H882Q-XFPQE-Q ...
- Java中的final关键字(转)
Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使 ...
- 还在为不停build 烦恼么?看这里~~
如果你是一名开发者,还在为偶尔改一个坐标或者颜色值 就要重新build 好久,然后如果层次深 还要一步步进去看效果么?下面 为大家介绍一个很好的开源库 DYCI 他的github地址,首先下载到本 ...
- Camtasia Studio录制屏幕字迹不清晰的原因
Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...