【51nod】1655 染色问题

题解

首先每个颜色出现的次数应该是一样的
(frac{C_{n}^{2}}{n} = frac{n - 1}{2})
所以n如果是偶数那么就无解了
然后我们需要让每个点连颜色不同的四条边
只要端点是i,j,颜色是(i + j)%n就行

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <set>
//#define ivorysi
#define eps 1e-8
#define mo 974711
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN 30005
#define space putchar(' ')
#define enter putchar('
')
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
const int64 MOD = 1000000007;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 + c - '0';
	c = getchar();
    }
}
template<class T>
void out(T x) {
    if(x < 0) {putchar('-');x = -x;}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    int N,T;
    read(T);
    while(T--) {
	read(N);
	out(N);putchar('
');
	if(N % 2 == 0) puts("No solution");
	else {
	    for(int i = 1 ; i <= N ; ++i) {
		for(int j = i + 1 ; j <= N ; ++j) {
		    out(i);space;out(j);space;
		    int x = (i + j) % N;if(x == 0) x = N;
		    out(x);space;
		}
	    }
	    enter;
	}
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ivorysi/p/9067760.html