Codeforces 29C Mail Stamps(离散化 && map初探)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值