QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#97657 | #6329. Colorful Graph | whatever# | WA | 5ms | 8484kb | C++14 | 3.6kb | 2023-04-17 20:30:04 | 2023-04-17 20:30:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
using pii = pair<int, int>;
using i64 = long long;
#define rep(i, a, b) for (int i = a, I = b; i <= I; ++i)
#define per(i, a, b) for (int i = a, I = b; i >= I; --i)
template<typename T> void up(T &x, T y) { if (x < y) x = y; }
template<typename T> void down(T &x, T y) { if (x > y) x = y; }
const int N = 7005, inf = 1e9;
int n, m, a[N], b[N], c[N], bel[N], que[N], to[N], top, cnt, d[N];
vector<int> e1[N], e2[N];
bool vis[N];
void dfs1(int u) {
vis[u] = 1;
for (auto v : e2[u]) if (!vis[v]) dfs1(v);
que[++top] = u;
}
void dfs2(int u, int c) {
bel[u] = c;
for (auto v : e1[u]) if (!bel[v]) dfs2(v, c);
}
vector<int> A, B;
namespace flow {
const int N = 14005, M = 123456;
int n, s, t, d[N], que[N], cap[M], res;
int cnt = 1, head[N], cur[N], to[M], nxt[M], val[M];
void addedge(int u, int v, int w) {
to[++cnt] = v;
cap[cnt] = val[cnt] = w;
nxt[cnt] = head[u];
head[u] = cnt;
}
void add(int u, int v, int w) {
addedge(u, v, w);
addedge(v, u, 0);
}
bool bfs() {
int l = 1, r = 0;
for(int i = 0; i <= n; ++i) cur[i] = head[i], d[i] = 0;
que[++r] = s, d[s] = 1;
while(l <= r) {
int u = que[l++];
for(int i = head[u]; i; i = nxt[i]) {
int v = to[i], w = val[i];
if(d[v] || !w) continue;
d[v] = d[u] + 1;
que[++r] = v;
}
}
return d[t];
}
int dfs(int u, int flow) {
if(u == t) return flow;
int rest = flow;
for(int &i = cur[u]; i; i = nxt[i]) {
int v = to[i], w = val[i];
if(d[v] != d[u] + 1 || !w) continue;
int k = dfs(v, min(flow, w));
if(!k) d[v] = 0;
rest -= k;
val[i] -= k, val[i ^ 1] += k;
if(!rest) break;
}
return flow - rest;
}
int dinic() {
res = 0;
while(bfs()) res += dfs(s, inf);
return res;
}
struct edge {
int to, nxt, val;
} e[M];
int hd[N], tot;
void get_path(int u) {
for (int &i = hd[u]; i; ) {
int v = e[i].to;
if (!--e[i].val) i = e[i].nxt;
get_path(v);
if (v == t) B.push_back(u);
if (u == s) A.push_back(v);
}
}
void solve() {
rep(u, 0, n) {
for (int i = head[u]; i; i = nxt[i]) {
if (!cap[i]) continue;
int f = cap[i] - val[i];
if (!f) continue;
int v = to[i];
e[++tot] = {v, hd[u], f}, hd[u] = tot;
}
}
e[++tot] = {s, hd[t], res}, hd[t] = tot;
get_path(s);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
rep(i, 1, m) {
cin >> a[i] >> b[i];
e1[a[i]].push_back(b[i]);
e2[b[i]].push_back(a[i]);
}
rep(i, 1, n) if (!vis[i]) dfs1(i);
per(i, n, 1) {
int u = que[i];
if (!bel[u]) dfs2(u, ++cnt);
}
flow::s = 0, flow::t = flow::n = 2 * cnt + 1;
rep(i, 1, cnt) flow::add(flow::s, i, 1), flow::add(i + cnt, flow::t, 1);
rep(i, 1, cnt) flow::add(i + cnt, i, inf);
rep(i, 1, m) {
int u = bel[a[i]], v = bel[b[i]];
if (u == v) continue;
flow::add(u, v + cnt, 1);
}
int res = flow::dinic();
flow::solve();
rep(i, 0, res - 1) {
to[A[i]] = B[i] - cnt, ++d[B[i] - cnt];
}
int tot = 0;
rep(u, 1, cnt) {
if (d[u]) continue;
c[u] = ++tot;
int x = u;
while (to[x]) x = to[x], c[x] = tot;
}
rep(i, 1, n) cout << c[bel[i]] << " \n"[i == n];
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5920kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 2 1 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 1ms
memory: 5736kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
2 2 1 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 2ms
memory: 5952kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
4 4 4 4 3 4 2 1
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 5ms
memory: 8484kb
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...
output:
3499 1657 1326 1564 2888 2306 2494 1365 1110 1505 3350 3456 2476 3381 1519 2552 1109 2684 1060 1059 1257 1058 3213 1498 1662 3330 2542 2688 1404 1772 1171 1198 1993 2602 1357 1358 1057 2165 3278 2055 1525 1158 3214 1056 2100 1055 1868 3258 2072 3180 884 2575 1292 2791 1647 883 3317 2157 1025 3386 11...
result:
wrong answer Integer 3499 violates the range [1, 1750]