QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#340463 | #895. Color | zhaohaikun | TL | 0ms | 0kb | C++14 | 3.1kb | 2024-02-29 07:57:31 | 2024-02-29 07:57:31 |
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T &x, T y) {x = max(x, y);}
template <typename T> inline void chkmin(T &x, T y) {x = min(x, y);}
template <typename T> inline void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
const int N = 210;
int n, m, a[N][N], p[N];//, col[N][N];
bool vis[N][N];
struct edge {
int dest, flow;
unsigned pos;
};
vector <edge> v[N];
void addedge(int x, int y, int flow) {
// debug << x << ' ' << y << " " << flow << endl;
v[x].push_back((edge) {y, flow, (unsigned) v[y].size()});
v[y].push_back((edge) {x, 0, (unsigned) v[x].size() - 1});
}
int s, t, dep[N];
bool bfs() {
queue <int> q;
q.push(s);
F(i, s, t) dep[i] = 0;
dep[s] = 1;
while (q.size()) {
int x = q.front(); q.pop();
for (edge i: v[x])
if (i.flow && !dep[i.dest]) {
dep[i.dest] = dep[x] + 1;
q.push(i.dest);
}
}
return dep[t];
}
unsigned cur[N];
int dfs(int x, int lim) {
if (x == t) return lim;
int ans = 0, t;
for (unsigned &i = cur[x]; i < v[x].size(); i++)
if (dep[v[x][i].dest] == dep[x] + 1 && v[x][i].flow && (t = dfs(v[x][i].dest, min(lim, v[x][i].flow)))) {
lim -= t, ans += t;
v[x][i].flow -= t, v[v[x][i].dest][v[x][i].pos].flow += t;
if (!lim) break;
}
return ans;
}
int dinic() {
int ans = 0;
while (bfs()) {
F(i, s, t) cur[i] = 0;
ans += dfs(s, 1e9);
}
return ans;
}
void zhk() {
read(n), read(m);
if (m % 2 == 0) {
puts("No");
return;
}
F(i, 1, m + 1) p[i] = 0;
F(i, 1, m + 1)
F(j, 1, m + 1)
vis[i][j] = false;
F(i, 1, n)
F(j, i + 1, n) {
read(a[i][j]);
// if (a[i][j]) {
p[a[i][j]]++;
vis[i][a[i][j]] = vis[j][a[i][j]] = true;
// }
}
F(i, 1, m) {
if (n - 2 * p[i] > m + 1 - n) {
puts("No");
return;
}
}
F(i, n + 1, m + 1) {
t = m + i + 1;
F(j, s, t) v[j].clear();
F(j, 1, m) {
addedge(s, j, 1);
F(k, 1, i - 1)
if (!vis[k][j]) addedge(j, m + k, 1);
addedge(j, m + i, m + 1 + 2 * p[j] - 2 * (i - 1));
}
F(j, 1, i - 1) addedge(m + j, t, 1);
addedge(m + i, t, m - i + 1);
dinic();
// debug << m << ' ' << dinic() << endl;
F(j, 1, m)
for (edge k: v[j])
if (!k.flow && k.dest != m + i) {
assert(k.dest);
// debug << k.dest - m << ' ' << i << ' ' << j << endl;
p[j]++;
vis[k.dest - m][j] = vis[i][j] = true;
a[k.dest - m][i] = j;
}
}
puts("Yes");
F(i, 1, m) {
F(j, i + 1, m + 1)
cout << a[i][j] << " \n"[j == m + 1];
// cout << '\n';
}
}
signed main() {
int _ = 1;
cin >> _;
while (_--) zhk();
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 5 1 2 4