zoukankan      html  css  js  c++  java
  • UVA1613-K-Graph Oddity(贪心)

    Problem UVA1613-K-Graph Oddity

    Accept: 108  Submit: 884
    Time Limit: 3000 mSec

    Problem Description

    Input

    The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
    The first line of the input file contains two integer numbers n and m, where n is the number of vertices in the graph (3 ≤ n ≤ 9999, n is odd), m is the number of edges in the graph (2 ≤ m ≤ 100000). The following m lines describe edges of the graph, each edge is described by two integers ai, bi (1 ≤ ai,bi ≤ n,ai = bi) — the vertex numbers connected by this edge. Each edge is listed at most once. The graph in the input file is connected, so there is a path between any pair of vertices.

     Output

    For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    On the first line of the output file write a single integer number k — the minimal odd integer number, such that the degree of any vertex does not exceed k. Then write n lines with one integer number ci (1 ≤ ci ≤ k) on a line that denotes the color of i-th vertex. The colors of any two adjacent vertices must be different. If the graph has multiple different colorings, print any of them. At least one such coloring always exists.
     

     Sample Input

    3 2
    1 3
    3 2
    7 8
    1 4
    4 2
    2 6
    6 3
    3 7
    4 5
    5 6
    5 2
     

    Sample Output

    3
    1
    1
    2
     
    3
    1
    1
    1
    2
    3
    2
    2
     
    题解:这个题过得比较意外,一开始想从度数大的开始涂,弄一个优先队列,画了几个图,发现好像不管怎么涂都对,就写了一个然后过了,为什么是对的真的说不好,欢迎大佬指教。
     
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 const int maxn = 10000 + 10, maxm = 100000 + 10;
     6 
     7 int n, m, k;
     8 
     9 struct Edge {
    10     int to, next;
    11 }edge[maxm << 1];
    12 
    13 int tot, head[maxn];
    14 int col[maxn], deg[maxn];
    15 bool vis[maxn];
    16 
    17 void init() {
    18     tot = 0;
    19     memset(head, -1, sizeof(head));
    20     memset(deg, 0, sizeof(deg));
    21     memset(col, -1, sizeof(col));
    22 }
    23 
    24 void AddEdge(int u,int v){
    25     edge[tot].to = v;
    26     edge[tot].next = head[u];
    27     head[u] = tot++;
    28 }
    29 
    30 void dfs(int u) {
    31     memset(vis, false, sizeof(vis));
    32     //printf("%d %d
    ", fa, u);
    33     for (int i = head[u]; i != -1; i = edge[i].next) {
    34         int v = edge[i].to;
    35         if (col[v] != -1) vis[col[v]] = true;
    36     }
    37 
    38     for (int i = 1; i <= k; i++) {
    39         if (!vis[i]) {
    40             col[u] = i;
    41             break;
    42         }
    43     }
    44 
    45     for (int i = head[u]; i != -1; i = edge[i].next) {
    46         int v = edge[i].to;
    47         if (col[v] == -1) {
    48             dfs(v);
    49         }
    50     }
    51 }
    52 
    53 int main()
    54 {
    55     //freopen("input.txt", "r", stdin);
    56     bool flag = false;
    57     while (~scanf("%d%d", &n, &m)) {
    58         if(flag) printf("
    ");
    59         flag = true;
    60         init();
    61         int x, y;
    62         for (int i = 0; i < m; i++) {
    63             scanf("%d%d", &x, &y);
    64             deg[x]++, deg[y]++;
    65             AddEdge(x, y);
    66             AddEdge(y, x);
    67         }
    68 
    69         int Max = 0;
    70         for (int i = 1; i <= n; i++) {
    71             Max = Max > deg[i] ? Max : deg[i];
    72         }
    73 
    74         k = (Max & 1) ? Max : Max + 1;
    75         dfs(1);
    76         printf("%d
    ", k);
    77         for (int i = 1; i <= n; i++) {
    78             printf("%d
    ", col[i]);
    79         }
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    启动Django项目报错:django.db.utils.OperationalError: (1193, "Unknown system variable 'storage_engine'")
    vue基础
    创建django项目出现AttrbuteError module 'pip' has no attribute 'main'解决方法
    ipython基本使用方法
    JavaScript(一)
    Spring 框架的设计理念与设计模式
    深入Java网络编程与NIO(三)
    深入Java网络编程与NIO(一)
    Code Jam Kickstart 2018 Round H 题解
    从TiDB学习分布式数据库
  • 原文地址:https://www.cnblogs.com/npugen/p/9688324.html
Copyright © 2011-2022 走看看