Solutions of the problem [m] FIG coloring problem

Topic background

  Given undirected graph G and m are different colors. These coloring each vertex of graph G color, the color of each vertex. So if there is a G coloring each edge in vertex two different colors, is called the m colorable FIG. m coloring problem is that for a given graph G and of m colors, to find all the different shading.

 

Title Description

  For a given undirected graph G and m are as different colors, different colored all programming on the calculation method of FIG.

 

Input Format

  The first row has three positive integers n, k and m, represent a given graph G k with n vertices and edges, m colors. Vertices numbered 1,2, ..., n. The next k rows, each row has two positive integers u, v, represents an edge graph G (u, v).

 

Output Format

   When the run is finished, the calculated number of output different coloring schemes.

 

SAMPLE INPUT

5 8 4

1 2

1 3

1 4

2 3

2 4

2 5

3 4

4 5

 

Sample Output

48

 

Explanation

  n<=100;k<=2500;

  To ensure that when a large n k large enough.

  The answer to ensure that no more than 20,000.

 

answer

  Not much to say other pruning. In this question, the starting point of fact, we have to search search only one color, when the final output multiplied by $ m $ can (multiplication principle).

#include <iostream>

#define MAX_N (100 + 5)
#define MAX_K (2500 + 5)
#define MAX_M (2500 + 5)

using namespace std;

struct Edge
{
    int to;
    int next;
};

int n, k, m;
int h[MAX_N], tot;
Edge e[MAX_K + MAX_K];
int c[MAX_N][MAX_M];
int ans;

inline void Add_Edge(int u, int v)
{
    e[++tot].to = v;
    e[tot].next = h[u];
    h[u] = tot;
    return;
}

void DFS(int u)
{
    if(u > n)
    {
        ++ans;
        return;
    }
    for(register int i = 1; i <= m; ++i)
    {
        if(c[u][i]) continue;
        for(register int j = h[u]; j; j = e[j].next)
        {
            ++c[e[j].to][i]; 
        }
        DFS(u + 1);
        for(register int j = h[u]; j; j = e[j].next)
        {
            --c[e[j].to][i]; 
        }
    }
    return;
}

int main()
{
    cin >> n >> k >> m;
    int u, v; 
    for(register int i = 1; i <= k; ++i)
    {
        cin >> u >> v;
        Add_Edge(u, v);
        Add_Edge(v, u);    
    }
    for(register int i = h[1]; i; i = e[i].next)
    {
        ++c[e[i].to][1];
    }
    DFS(2);
    cout << ans * m;
    return 0;
} 
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10988665.html