C. Mail Stamps---cf29c(离散化,图)

 

题目链接:http://codeforces.com/problemset/problem/29/C

题意就是有n(1e5)个点,找到一条能把所有的点都包含在内的路径,由于点的编号是 1e9 所以不得不采用map离散化;

主要就是关于离散化的问题;

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <map>
#include <vector>

using namespace std;

typedef long long LL;

#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define N 100010

int n, a, b, k, aa[N], du[N], vis[N];

int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        map<int , int> Map;

        vector<int>G[N];

        for(int i=0; i<N; i++)
            G[i].clear();
            
        Map.clear();

        met(aa, 0); met(du, 0); met(vis, 0);

        k = 1;

        for(int i=1; i<=n; i++)
        {
            scanf("%d %d", &a, &b);

            if(Map[a] == 0 )
                Map[a] = k++;
                
            if(Map[b] == 0 )
                Map[b] = k++;
            ///离散化;
            
            int p = Map[a];
            int q = Map[b];

            aa[p] = a;
            aa[q] = b;
            ///为了方便输出,所以用aa数组转化;
            G[p].push_back(q);
            G[q].push_back(p);
            ///无向图,加两条边,所以为了防止重复找点,用vis标记已找到的点;

            du[p]++;
            du[q]++;
            ///找到度数为1的点;
        }

        int Index = -1;

        for(int i=1; i<k; i++)
        {
            if(du[i]==1)
            {
                Index = i;
                break;
            }
        }
        printf("%d", aa[Index]);///输出第一个点;并标记;

        vis[Index] = 1;

        while(n--)
        {
            int len = G[Index].size();

            for(int i=0; i<len; i++)///找与之相关联的点,并判断是否符合条件;
            {
                int p = G[Index][i];

                if(vis[p] == 1)

                    continue;

                Index = p;

                printf(" %d", aa[Index]);

                vis[Index] = 1;

                break;
            }
        }
        printf("\n");
    }
    return 0;
}
View Code

 

posted @ 2016-03-13 15:25  西瓜不懂柠檬的酸  Views(257)  Comments(0)    收藏  举报
levels of contents