Codeforces 29C Mail Stamps(离散化 && map初探)
来源:互联网 发布:java spring 异步调用 编辑:程序博客网 时间:2024/05/29 03:03
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
One day Bob got a letter in an envelope. Bob knows that when Berland’s post officers send a letter directly from city «A» to city «B», they stamp it with «A B», or «B A». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that’s why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.
There are n stamps on the envelope of Bob’s letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can’t determine himself none of these routes. That’s why he asks you to help him. Find one of the possible routes of the letter.
Input
The first line contains integer n (1 ≤ n ≤ 105) — amount of mail stamps on the envelope. Then there follow n lines with two integers each — description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city.
Output
Output n + 1 numbers — indexes of cities in one of the two possible routes of the letter.
Sample Input
Input
2
1 100
100 2
Output
2 100 1
Input
3
3 1
100 2
3 2
Output
100 2 3 1
题意:给你 n 个边,让你找到一条连接 n + 1 个点的路,并把n+1个点依次输出。
题解:由于点少但是点的值特别大,所以就需要用到离散化得思想,用对应的位置存放对于点的值,然后直接考虑对应的位置,输出对应位置的点的值就好了
C++里面的map容器是做为连接两个数之间关系的容器,具体还是去百度好了
#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <vector>#include <queue>using namespace std;#define M 100010#define mem(a, b) memset(a, b, sizeof(a))int pos, reflect[M];map<int, int> mp;vector<int> vec[M];bool vis[M];void init(){ mem(vis, 0); mp.clear(); pos = 0;}bool dfs(int u, int dep){ if(dep == pos) { printf("%d ", reflect[u]); return true; } for(int i=0; i<=vec[i].size(); i++) { int v = vec[u][i]; if(!vis[v]) { vis[v] = true; if(dfs(v, dep+1)) { printf("%d ", reflect[u]); return true; } vis[v] = false; } } return false;}int main(){ int n, a, b; while(scanf("%d", &n) != EOF) { init(); for(int i=1; i<=n; i++) { scanf("%d%d", &a, &b); if(!mp[a])//离散化 { mp[a] = ++pos; reflect[pos] = a; } if(!mp[b]) { mp[b] = ++pos; reflect[pos] = b; } vec[mp[a]].push_back(mp[b]); vec[mp[b]].push_back(mp[a]); } for(int i=1; i<=n; i++) { if(vec[i].size() == 1) { vis[i] = true; dfs(i, 1); printf("\n"); break; } } } return 0;}
- Codeforces 29C Mail Stamps(离散化 && map初探)
- Codeforces 29C Mail Stamps 【离散化 + DFS】
- codeforces-29C-Mail Stamps【离散化】【STLmap】
- Codeforces 29C Mail Stamps【离散化+DFS】
- CF-29C - Mail Stamps(DFS+离散化)
- Codeforces #29 C. Mail Stamps
- codeforces C. Mail Stamps
- Codeforces 29C:Mail Stamps(STL的应用+DFS)
- codeforces 29C Mail Stamps(dfs)
- 【codeforces】29C—Mail Stamps
- Codeforces Beta Round #29 (Div. 2, Codeforces format), problem: (C) Mail Stamps 图论
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (map离散化)
- Codeforces 696A. Lorenzo Von Matterhorn(map离散化)
- cf#345-C - Watchmen-map离散化
- cf29C. Mail Stamps
- Codeforces 138C(区间更新+离散化)
- CodeForces 670C Cinema(排序,离散化)
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C 离散化+树状数组+map D 数学
- 集合框架_01_集合框架的构成及分类
- UVALive 6378 Friend Chains (多源最短路 spfa)
- strrev函数
- android 去掉ScrollVIew拉到尽头时再拉的阴影效果
- CAFFE 学习(1). 参考网站和资料
- Codeforces 29C Mail Stamps(离散化 && map初探)
- solr 5.5.2 集成mysql 索引增量权利更新
- java数组排序示例(冒泡排序、快速排序、希尔排序、选择排序)
- 2014-10-15-函数--StaticMesh运动
- HDU1370-Biorhythms
- HTML与CSS--------居中问题
- js中应用protocol buffer
- <s:doubleselect 的使用-不支持EL表达式,需要使用OGNL来动态赋值-could not be resolved as a collection/array/map/enumerati
- 多校训练(三)