#include<stdio.h> #include<vector> using namespace std; const int MAX_N = 200; int n, c[MAX_N], od[MAX_N][MAX_N - 1], color[MAX_N * 6]; vector<int> adj[2][MAX_N * 6], vis, tvis; bool ck[MAX_N * 6]; void add(int x, int y) { adj[0][(x + 3 * n) % (6 * n)].push_back(y); adj[1][y].push_back((x + 3 * n) % (6 * n)); adj[0][(y + 3 * n) % (6 * n)].push_back(x); adj[1][x].push_back((y + 3 * n) % (6 * n)); } void dfs(int h, bool type) { if (ck[h] != type) return; ck[h] = !type; for (auto t : adj[type][h]) dfs(t, type); vis.push_back(h); } bool f(int t) { for (int i = 0; i < 6 * n; i++) adj[0][i].clear(), adj[1][i].clear(); for (int i = 0; i < n; i++) { add((i + n) * 3 + c[i], (i + n) * 3 + c[i]); add(i * 3 + (c[i] + 1) % 3, i * 3 + (c[i] + 2) % 3); add((i + n) * 3 + (c[i] + 1) % 3, (i + n) * 3 + (c[i] + 2) % 3); for (int j = t; j < n - 1; j++) { add((i + n) * 3 + (c[i] + 1) % 3, (od[i][j] + n) * 3 + (c[i] + 1) % 3); add((i + n) * 3 + (c[i] + 2) % 3, (od[i][j] + n) * 3 + (c[i] + 2) % 3); } } vis.clear(); for (int i = 0; i < 6 * n; i++) dfs(i, false); tvis = vis; for (int i = tvis.size() - 1; i >= 0; i--) { if (!ck[tvis[i]]) continue; vis.clear(); dfs(tvis[i], true); for (auto it : vis) color[it] = i; } bool res = true; for (int i = 0; i < 3 * n; i++) res &= color[i] != color[i + 3 * n]; return res; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &c[i]); for (int j = 0; j < n - 1; j++) scanf("%d", &od[i][j]), od[i][j]--; } int h = 0, t = n - 1, m; while (h <= t) { m = (h + t) / 2; if (f(m)) t = m - 1; else h = m + 1; } printf("%d", h); return 0; }
5009번: 유치원 - 최적화 필요
https://www.acmicpc.net/problem/5009
피드 구독하기:
댓글
(
Atom
)
댓글 없음 :
댓글 쓰기