QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#339602 | #895. Color | oscaryang | TL | 0ms | 0kb | C++14 | 2.7kb | 2024-02-27 16:39:54 | 2024-02-27 16:39:55 |
answer
#include<bits/stdc++.h>
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
inline int read() {
int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x;
}
const int N = 205, inf = 1e9;
namespace flow {
const int MAXN = N * N;
int S, T, hed, tl, maxflow, Q[MAXN], dep[MAXN];
int cur[MAXN], hd[MAXN], to[MAXN << 1], ne[MAXN << 1], e[MAXN << 1], tc = 1;
inline void prework() {
tc = 1; maxflow = 0; rep(i, 0, T) hd[i] = 0;
}
inline void add(int x, int y, int z) {
to[++tc] = y; ne[tc] = hd[x]; hd[x] = tc; e[tc] = z;
to[++tc] = x; ne[tc] = hd[y]; hd[y] = tc; e[tc] = 0;
}
inline bool bfs() {
rep(i, 0, T) dep[i] = -1;
queue<int> Q; Q.push(S); dep[S] = 0;
while(!Q.empty()) {
int x = Q.front(); Q.pop(); cur[x] = hd[x];
if(x == T) return 1;
for(int i = hd[x], y; i; i = ne[i])
if(e[i] && dep[y = to[i]] == -1) dep[y] = dep[x] + 1, Q.push(y);
}
return 0;
}
inline int dfs(int x, int fl) {
if(x == T) return maxflow += fl, fl;
for(int &i = cur[x], y, o; i; i = ne[i])
if(e[i] && dep[y = to[i]] == dep[x] + 1 && (o = dfs(y, min(fl, e[i]))))
return e[i] -= o, e[i ^ 1] += o, o;
return 0;
}
inline void dinic() { while(bfs()) { while(dfs(S, inf)); } }
}
int n, m, p[N], a[N][N], in[N][N];
inline void testcase() {
n = read(); m = read();
rep(i, 0, m) rep(j, 0, m) in[i][j] = a[i][j] = 0, p[i] = 0;
rep(i, 1, n) rep(j, i + 1, n) {
a[i][j] = read(); ++p[a[i][j]];
in[a[i][j]][i] = in[a[i][j]][j] = 1;
}
if(m % 2 == 0) return puts("No"), void();
rep(i, 1, m) if(n - 2 * p[i] > m - n + 1) return puts("No"), void();
puts("Yes");
while(n <= m + 1) {
flow :: S = m + n + 2;
flow :: T = m + n + 3;
flow :: prework();
flow :: add(n + m + 1, flow :: T, m - n);
rep(i, 1, n) flow :: add(m + i, flow :: T, 1);
rep(i, 1, m) {
flow :: add(flow :: S, i, 1);
flow :: add(i, n + m + 1, m + 1 + 2 * p[i] - 2 * n);
rep(j, 1, n) if(!in[i][j]) flow :: add(i, j + m, 1);
}
flow :: dinic();
rep(x, 1, m) for(int i = flow :: hd[x], y; i; i = flow :: ne[i])
if((y = flow :: to[i]) != n + m + 1 && !flow :: e[i])
a[y - m][n + 1] = x, in[x][n + 1] = in[x][y - m] = 1, ++p[x];
++n;
}
rep(i, 1, m + 1) rep(j, i + 1, m + 1)
printf("%d%c", a[i][j], " \n"[j == m + 1]);
}
signed main() {
int t = read(); while(t--) testcase();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 5 1 2 4