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> ...
随机推荐
- 算法复习——2—sat(bzoj2199)
题目: Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 ...
- bzoj3743 [Coci2015]Kamp 常州模拟赛d6t2
3743: [Coci2015]Kamp Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 484 Solved: 229[Submit][Status ...
- 【霍夫曼树】 poj 1521 Entropy
poj.org/problem?id=1521 注意只有特殊情况:只有一种字母 #include<iostream> #include<cstdio> #include< ...
- VS的一些错误解决方法记录
1.errorC2664 "bool CMarkup::AddElem(MCD_CSTR,MCD_CSTR,int)":不能将参数1从“constchar [7]” 转换位&quo ...
- 表单禁用复制、粘贴、及右击菜单(contextmenu、oncopy、oncut、onpaste、onselectstart)
禁用右键菜单,可以使用oncontextmenu属性: <textarea oncontextmenu="return false"></textarea> ...
- 详解DNS,你真的懂吗?
what`s this ? 概念 域名系统(英文:DomainNameSystem,缩写:DNS)是互联网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...
- spring data jpa 查询部分字段列名无效问题
spring data jpa原生sql查询问题,我只要表其中的几个字段的值,本以为写个原生sql,拿实体类对象去接没问题 结果列名无效,测试了一下,把返回值类型改成List<Object> ...
- ByteArrayInputStream的作用,和BufferedOutputStream 的区别
个人好奇ByteArrayInputStream,到底是有什么用于是百度了一些资料 整合了下,********这两个类对于要创建临时性文件的程序以及网络数据的传输.数据压缩后的传输等可以提高运行的的效 ...
- Version和Build的差别
[1]概念 iOS的版本号号.一个叫做Version,一个叫做Build,这两个值都能够在Xcode 中选中target,点击"Summary"后看到. Version在plist ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...