zoukankan      html  css  js  c++  java
  • 图论:(Code Forces) Graph and String

    Graph and String
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties:

    • G has exactly n vertices, numbered from 1 to n.
    • For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not.

    Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.

    Input

    The first line of the input contains two integers n and m  — the number of vertices and edges in the graph found by Petya, respectively.

    Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.

    Output

    In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise.

    If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.

    Examples
    input
    2 1
    1 2
    output
    Yes
    aa
    input
    4 3
    1 2
    1 3
    1 4
    output
    No
    Note

    In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions.

    In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.

      这题特别容易错!!!!!!!!!

      颠覆了我的人生观!!!!!!!!

      我的OI之路啊!!!!!!!!!!

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 int G[510][510];
     6 int n,m;
     7 int ans[510];
     8 bool Solve(int node)
     9 {
    10     bool ok=true;
    11     for(int i=1;i<=n;i++)
    12         if(G[node][i]){
    13             if(ans[i]){
    14                 if(ans[node]==ans[i])return false;
    15             }
    16             else
    17                 ans[i]=-ans[node],ok&=Solve(i);
    18         }    
    19     return ok;
    20 }
    21 void print()
    22 {
    23     printf("Yes
    ");
    24     for(int i=1;i<=n;i++)
    25         printf("%c",'b'+ans[i]);
    26     printf("
    ");    
    27 }
    28 int main()
    29 {
    30     scanf("%d%d",&n,&m);
    31     memset(ans,0,sizeof(ans));
    32     for(int i=1;i<=n;i++)
    33         for(int j=1;j<=n;j++)
    34             if(i!=j)
    35                 G[i][j]=1;
    36     for(int i=1;i<=m;i++)
    37     {
    38         int x,y;
    39         scanf("%d%d",&x,&y);
    40         G[x][y]=0;
    41         G[y][x]=0;
    42     }
    43 
    44     int flag=1;
    45     for(int i=1;i<=n;i++)
    46         for(int j=1;j<=n;j++)
    47             if(G[i][j]&&!ans[i]){
    48                 ans[i]=1;
    49                 if(!Solve(i))
    50                     flag=0;
    51             }        
    52     for(int i=1;i<=n;i++)
    53         for(int j=1;j<=n;j++)
    54             if(!G[i][j]&&(ans[i]-ans[j]==2||ans[i]-ans[j]==-2)){
    55                 flag=0;
    56             }            
    57     if(flag)
    58         print();
    59     else 
    60         puts("No");            
    61                 
    62     return 0;
    63 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    bzoj 2744 朋友圈
    bzoj 3674 可持久化并查集加强版
    3.15 模拟赛
    luogu 4720 【模板】扩展卢卡斯
    洛谷 P1447 [NOI2010]能量采集 (莫比乌斯反演)
    洛谷P2522 [HAOI2011]Problem b (莫比乌斯反演+容斥)
    7-12 与零交换 (25 分)
    理想的正方形(单调队列)
    Flowerpot(单调队列)
    生日礼物(单调队列)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5218542.html
Copyright © 2011-2022 走看看