zoukankan      html  css  js  c++  java
  • (dfs、哈希)Codeforces Round #414 D- Labelling Cities

    D. Labelling Cities
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Oleg the bank client lives in Bankopolia. There are n cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to n. There are a total of m roads in Bankopolia, the i-th road connects cities ui and vi. It is guaranteed that from each city it is possible to travel to any other city using some of the roads.

    Oleg wants to give a label to each city. Suppose the label of city i is equal to xi. Then, it must hold that for all pairs of cities (u, v) the condition |xu - xv| ≤ 1 holds if and only if there is a road connecting u and v.

    Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise.

    Input

    The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 3·105, 1 ≤ m ≤ 3·105) — the number of cities and the number of roads.

    Next, m lines follow. The i-th line contains two space-separated integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities connected by the i-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads.

    Output

    If the required labeling is not possible, output a single line containing the string "NO" (without quotes).

    Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output n space-separated integers, x1, x2, ..., xn. The condition 1 ≤ xi ≤ 109 must hold for all i, and for all pairs of cities (u, v) the condition |xu - xv| ≤ 1 must hold if and only if there is a road connecting u and v.

    Examples
    input
    4 4
    1 2
    1 3
    1 4
    3 4
    output
    YES
    2 3 1 1
    input
    5 10
    1 2
    1 3
    1 4
    1 5
    2 3
    2 4
    2 5
    3 4
    3 5
    5 4
    output
    YES
    1 1 1 1 1
    input
    4 3
    1 2
    1 3
    1 4
    output
    NO
    Note

    For the first sample, x1 = 2, x2 = 3, x3 = x4 = 1 is a valid labeling. Indeed, (3, 4), (1, 2), (1, 3), (1, 4) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia.

    For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them.

    For the last sample, it is impossible to construct a labeling satisfying the given constraints.

    第一次写哈希,之前一直很好奇是怎样做到建立这样神奇的对应关系的,现在才知道原来也不是绝对的对应,如果数据比较坑,是可能卡掉一些的。

    做法如官方题解所说的算法,将每一点的邻接情况哈希表示,哈希值相同的给相同的编号,当作一个点映射到一个新的图中。

    在这样一个新的图中,彼此间连接关系依旧按照之前的来,只是这个图里的一个点表示的是一个点的集合,该集合中的点都具有相同的编号。

    在这样的图中,因为每个点编号不同,每个点至多与2个点相连,并且不能存在环。满足上述两个条件的话继续dfs给予编号即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <stack>
    12 #define mp make_pair
    13 typedef long long ll;
    14 typedef unsigned long long ull;
    15 const int MAX=3e5+5;
    16 const int INF=1e9+5;
    17 const double M=4e18;
    18 using namespace std;
    19 const int MOD=1e9+7;
    20 typedef pair<int,int> pii;
    21 typedef pair<int,long long> pil;
    22 const double eps=0.000000001;
    23 int n,m;
    24 int hashh[MAX],pre[MAX];
    25 vector <int>edge[MAX];
    26 int col[MAX];
    27 bool vi[MAX<<1];
    28 bool vis[MAX];
    29 void dfs(int lo)
    30 {
    31     vis[lo]=true;
    32     if(col[lo]==0)
    33         return;
    34     for(int i=0;i<edge[lo].size();i++)
    35     {
    36         int t=edge[lo][i];
    37         if(hashh[t]==hashh[lo])
    38             col[t]=col[lo];
    39     }
    40     for(int i=0;i<edge[lo].size();i++)
    41     {
    42         int t=edge[lo][i];
    43         if(!vis[t])
    44         {
    45             if(!col[t])
    46             {
    47                 int s;
    48                 for(s=col[lo]-1;s<=col[lo]+1;s++)
    49                 {
    50                     if(!vi[s])
    51                     {
    52                         vi[s]=true;
    53                         col[t]=s;
    54                         break;
    55                     }
    56                 }
    57             }
    58             dfs(t);
    59         }
    60     }
    61 }
    62 int main()
    63 {
    64     scanf("%d%d",&n,&m);
    65     memset(vi,false,sizeof(vi));
    66     memset(col,0,sizeof(col));
    67     pre[0]=0;
    68     for(int i=1;i<=n;i++)
    69         pre[i]=pre[i-1]*31+i;
    70     for(int i=1;i<=n;i++)
    71         hashh[i]=pre[i];
    72     int x,y;
    73     for(int i=1;i<=m;i++)
    74     {
    75         scanf("%d%d",&x,&y);
    76         edge[x].push_back(y);
    77         edge[y].push_back(x);
    78         hashh[x]+=pre[y];hashh[y]+=pre[x];
    79     }
    80     col[1]=300001;vi[300001]=true;vis[1]=true;
    81     dfs(1);
    82     for(int i=1;i<=n;i++)
    83     {
    84         if(!col[i])
    85             return 0*printf("NO
    ");
    86     }
    87     printf("YES
    ");
    88     for(int i=1;i<=n;i++)
    89         printf("%d ",col[i]);
    90     return 0;
    91 }
  • 相关阅读:
    Windows SDK 之 mciSendString最后一个参数
    java常用包下载地址(非maven)
    windows api(GDI)实现图片旋转
    windows sdk版本 之 并查集生成迷宫
    自签https证书2(适配新版chrome,不会显示“不安全”)
    数据结构——栈(Stacks)
    数据结构——表(list)
    数据结构——链表(linkedlist)
    解题报告1010 诡秘的余数
    函数体中用指针返回数组的方法
  • 原文地址:https://www.cnblogs.com/quintessence/p/6864334.html
Copyright © 2011-2022 走看看