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;}
0 0