CF-25D - Roads not only in Berland(并查集或者搜索)
来源:互联网 发布:js字符串转base64编码 编辑:程序博客网 时间:2024/04/29 03:01
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 aren cities in Berland and neighboring countries in total and exactlyn - 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. Nextn - 1 lines contain the description of roads. Each road is described by two space-separated integersai,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 outputt lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the formati j u v — it means that road between citiesi andj became closed and a new road between citiesu andv is built. Cities are numbered from 1. If the answer is not unique, output any.
Sample Input
21 2
0
71 22 33 14 55 66 7
13 1 3 7
思路1:最早的思路是搜索,每次递归传递当前的点,和它的父节点,而后它的子节点不能为父节点,这样搜到已经搜过的点就说明有环,那就去点这条边,搜下没搜过的点,把边连过去,这样就OK了。
思路2:用并查集,每次找读入两个点的根,如果根一样则说明有环,记录这条边,如果根不一样就合并,因为当前两点是相连的。
然后遍历一遍,有几个集合,就需要改几条边,每次从环里删条边就连接两集合。
失误:不知道怎么回事,用搜索老是乱整内存,意外终止,调不清楚,vector使用的时候也出很大问题,为什么it!=mp[u].end();它不终止而报错
耽误了好长时间调试,最后才用并查集搞定了。
#include <stdio.h>#include <vector>using namespace std;int f[1005];int find(int x){ return f[x]==x?f[x]:f[x]=find(f[x]);}void he(int x, int y) { f[find(x)]=find(y); }int main(void) {int n, i;scanf("%d", &n);for(i=1;i<=n;i++)f[i] = i;vector<pair<int, int> > p;vector<int> q;for(i=1;i<n;i++) {int x, y;scanf("%d%d", &x, &y);if(find(x)==find(y)) p.push_back(make_pair(x, y));///有环记录这条边,待会删去else he(x, y); }for(i=1;i<=n;i++)///找有几个不同集合,就需要改几调边if(find(i)==i) q.push_back(i);printf("%d\n", q.size()-1);for(i=1;i<(int)q.size();i++)///去个环,合并两个集合printf("%d %d %d %d\n", p[i-1].first, p[i-1].second, q[i-1], q[i]);return 0;}
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Codeforces 25D Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland (并查集)
- Codeforces 25 D.Roads not only in Berland(并查集)
- Codeforces 25D. Roads not only in Berland 并查集
- CodeForces 25D Roads not only in Berland 并查集
- Roads not only in Berland CodeForces 25D 并查集
- CodeForces 25D Roads not only in Berland【并查集】
- CodeForces 25D - Roads not only in Berland(并查集题目)
- CodeForces Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- odeforces Beta Round #25 (Div. 2 Only), problem: (D) Roads not only in Berland 图论 DFS
- Codeforces Beta Round #25 D题 Roads not only in Berland
- CF-25C - Roads in Berland(水题)
- CF 25C Roads in Berland
- CF-25C - Roads in Berland(水题)
- #IFNDEF、#DEF、#ENDIF 的使用
- JavaDoc那些事儿
- DOS系统功能调用表(INT 21H)
- 如何更改CMD命令的默认路径 (方便Java编程人员)
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Django -- Request and HttpRequest
- 笔记2
- 2013年01月
- 深入理解Oracle调试事件:10046事件详解
- 读《JavaServer Faces 核心编程(第3版)》一书的记录—— 第 4 章 标准 JSF 标签——案例 javascript
- 自定义拦截器
- Eclipse CDT+Qemu调试Linux 0.11内核
- 360以安全之名做搜索,可信,还是欺世盗名?