第一行一個整數T,表示數據組數接下來T行每行一個整數n,表示完全圖的點數Output輸出由T個部分組成每個部分的第一行一個整數n,表示完全圖的點數第二行表示構造結果如果無解輸出No solution否則輸出n*(n-1)/2條邊的起點、終點和顏色Input示例243Output示例4No solution31 2 3 2 3 1 3 1 2思路:
一開始沒什么思路,找找規律,花了幾個發現,先把正n邊形外圍順時針從1到n標記一圈,然后所有外圍的邊所正對的平行邊都標記成與其一樣的顏色,畫個圖就明白了。代碼寫得比較蠢,想到就寫了。代碼:
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int MAXN = 105;int g[MAXN][MAXN];bool vis[MAXN];struct Edge { int u, v, col;};int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); PRintf("%d/n", n); if (n % 2 == 0) { puts("No solution"); continue; } for (int i = 1; i <= n; i++) { int x = i, y = (i == n ? 1 : i + 1); for (int j = 1; j <= n; j++) vis[j] = false; vis[x] = vis[y] = true; g[x][y] = g[y][x] = i; for (int j = 1; j < (n - 1) / 2; j++) { int px = (x == n ? 1 : x + 1), qx = (x == 1 ? n : x - 1); x = vis[px] ? qx : px; int py = (y == n ? 1 : y + 1), qy = (y == 1 ? n : y - 1); y = vis[py] ? qy : py; vis[x] = vis[y] = true; g[x][y] = g[y][x] = i; } } vector <Edge> ans; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { ans.push_back((Edge){i, j, g[i][j]}); } } int cnt = ans.size(); for (int i = 0; i < cnt; i++) printf("%d %d %d%c", ans[i].u, ans[i].v, ans[i].col, i == cnt - 1 ? '/n' : ' '); } return 0;}
新聞熱點
疑難解答