Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
2015-08-24 19:58
477 查看
D. Roads not only in Berland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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 test(s)
input
output
input
output
并查集
先跑一遍并查及,分成几个集合。在查询父节点的时候,如果x=findfa(a),y=findfa(b) x=y那么,这条道路a-b就应该封闭
再跑一遍并查集,把这几个集合连起来....就得到要修的道路编号了
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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 test(s)
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
并查集
先跑一遍并查及,分成几个集合。在查询父节点的时候,如果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 << 61; const double eps=1e-5; 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=0;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=1;i<=n-1;i++){ scanf("%d %d",&a,&b); Union(a,b); } for(int i=1;i<=n;i++){ int x=findfa(i); for(int j=1+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=0;i<v1.size();i++){ cout<<v1[i].first<<" "<<v1[i].second<<" "; cout<<v2[i].first<<" "<<v2[i].second<<endl; } } return 0; }
相关文章推荐
- Java中的自增操作符与中间缓存变量机制
- hdoj 1257 最少拦截系统【贪心】
- poj 1475 Pushing Boxes(bfs)
- Cocos2d-x 创建精灵的五种方法
- [Linux运维 -- webserver]nginx的日志轮转
- 复习笔记
- Oracle学习笔记20150822分页查询sql语句分类和集合运算
- The stack size specified is too small, Specify at least 228k
- Redis常用的命令(五)------列表list
- leetcode263_264:Ugly Number & Ugly Number II
- java web之Servlet技术
- ARM处理器寄存器
- kvc和kvo的使用情况的了解
- 基数排序C++实现
- Ajax工作原理
- 深入理解PHP原理之错误抑制与内嵌HTML
- uiwebview添加到阅读列表
- 如何让IE支持自定义协议
- 哈诺塔题型问题+uva10795+URAL 2029
- useragent 全集 firefox插件 useragetn switch 使用