Codeforces - AIM Tech Round (Div. 2)C - Graph and String(练习)

C. 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 from1 to n.
  • For all pairs of vertices i and j, where i ≠ j, there is an edge connecting themif 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 strings, 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 andm  — the number of vertices and edges in the graph found by Petya, respectively.

Each of the next m lines contains two integersui andvi(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 strings 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 ofs 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.


题意:一个长度为n的字符串仅由a,b,c构成,任意两个位置的字母相同或相邻时连一条线,现在给出n和m条线,问能不能构造出一个字符串,若能则输出Yes并输出其中一个,不能输出No

题解:n-1的肯定是b,把这些位置全部换成b,然后找一个位置seat,把seat和与seat相连线的都换成a,其余全部是c。这样构成一个字符串,然后再代回去判断给出的那些边的条件是否成立

最开始只判断了没给出的边字母不能相同相邻,导致wa了一发,看见wa后想起来还要考虑给出边的字母必须相同相邻= =傻傻傻傻傻


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <vector>
using namespace std;

int main()
{
  int n, m;
  bool a[505][505];
  while(scanf("%d%d", &n, &m)!= EOF){
    memset(a, 0, sizeof(a));
    int cnt[505];
    memset(cnt, 0, sizeof(cnt));
    int x, y;
    for(int i = 0; i < m; i++){
      scanf("%d%d", &x, &y);
      a[x][y] = a[y][x] = 1;
      cnt[x]++;
      cnt[y]++;
    }
    int ans[505];
    memset(ans, 0, sizeof(ans));
    for(int i = 1; i <= n; i++){
      if(cnt[i] == n - 1){
        ans[i] = 2;
      }
    }
    for(int i = 1; i <= n; i++){
      if(!ans[i]){
        ans[i] = 1;
        for(int j = i + 1; j <= n; j++){
          if(a[i][j] && ans[j] == 0){
            ans[j] = 1;
          }
        }
        break;
      }
    }
    for(int i = 1; i<= n; i++){
      if(ans[i] != 1 && ans[i] != 2)
        ans[i] = 3;
    }
    bool flag = 0;
    for(int i = 1; i<= n; i++){
      for(int j = i + 1; j <= n; j++){
        if(a[i][j]) continue;
        if(abs(ans[i] - ans[j]) <= 1){
          flag = 1;
          break;
        }
      }
      for(int j = i + 1; j <= n; j++){
        if(!a[i][j]) continue;
        if(abs(ans[i] - ans[j]) > 1){
          flag = 1;
          break;
        }
      }
    }
    if(flag == 0){
        printf("Yes\n");
      for(int i = 1; i<= n; i++){
        if(ans[i] == 1) printf("a");
        else if(ans[i] == 2) printf("b");
        else printf("c");
      }
      printf("\n");
    }
    else printf("No\n");
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值