QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#312067 | #6329. Colorful Graph | PlentyOfPenalty# | WA | 1593ms | 9740kb | C++20 | 4.9kb | 2024-01-23 12:19:03 | 2024-01-23 12:19:04 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
bool umin(auto &a, auto t) {
if (t < a) return a = t, 1;
return 0;
}
const int MAXN = 41011, INF = 1e9 + 233;
const ll LINF = 1.233e18;
namespace Flow {
const int N = MAXN;
struct edge {
int v, w, cost, nxt;
} e[MAXN << 2 | 1];
int cnt = 1, last[N], cur[N];
void adde(int u, int v, int w, int c) {
// printf("F add %d %d [%d,%d]\n", u, v, w, c);
e[++cnt] = {v, w, c, last[u]}, last[u] = cnt;
e[++cnt] = {u, 0, -c, last[v]}, last[v] = cnt;
}
ll dis[N];
bool vis[N], inq[N];
bool spfa(int s, int t, int n) {
// printf("SPFA s=%d\n", s);
for (int i = 1; i <= n; ++i)
vis[i] = 0, dis[i] = LINF, cur[i] = last[i];
queue<ll> q;
dis[s] = 0;
q.push(s);
while (q.size()) {
int u = q.front();
inq[u] = 0;
q.pop();
for (int i = last[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (e[i].w > 0 && umin(dis[v], dis[u] + e[i].cost))
if (!inq[v]) q.push(v), inq[v] = 1;
}
}
// printf("dis=%lld\n", dis[t]);
return dis[t] < 0;
}
ll sumc = 0;
ll ex_flow(int u, int t, ll flow = LINF) {
if (u == t) return flow;
vis[u] = 1;
ll f = 0;
for (int &i = cur[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (e[i].w > 0 && !vis[v] && dis[v] == dis[u] + e[i].cost) {
ll tmp = ex_flow(v, t, min((ll)e[i].w, flow - f));
e[i].w -= tmp, e[i ^ 1].w += tmp;
sumc += tmp * e[i].cost;
f += tmp;
if (f == flow) return f;
}
}
return f;
}
pll min_cost_zkw(ll s, ll t, ll n) {
ll res = 0;
while (spfa(s, t, n))
res += ex_flow(s, t);
return pll(res, sumc);
}
} // namespace Flow
set<pii> S;
struct edge {
int v, nxt;
} e[MAXN << 1 | 1];
int cnt = 1, last[MAXN];
void adde(int u, int v) { e[++cnt].v = v, e[cnt].nxt = last[u], last[u] = cnt; }
int dfn[MAXN], low[MAXN], cur = 0;
int s[MAXN], top = 0;
vector<int> seq[MAXN];
int scc[MAXN], scnt = 0;
bool ins[MAXN];
void tarjan(int u) {
dfn[u] = low[u] = ++cur;
s[++top] = u, ins[u] = 1;
for (int i = last[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (!dfn[v])
tarjan(v), umin(low[u], low[v]);
else if (ins[v])
umin(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
++scnt;
while (1) {
int x = s[top--];
seq[scnt].emplace_back(x);
scc[x] = scnt;
ins[x] = 0;
if (x == u) break;
}
}
}
vector<int> g[MAXN], h[MAXN];
int match[MAXN];
bool vis[MAXN];
bool dfs(int u) {
for (auto v : g[u])
if (!vis[v]) {
vis[v] = 1;
if (!match[v] || dfs(match[v])) {
match[v] = u;
return 1;
}
}
return 0;
}
int color[MAXN], cc = 0;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
adde(u, v);
}
for (int u = 1; u <= n; ++u)
if (!dfn[u]) tarjan(u);
for (int u = 1; u <= n; ++u)
for (int i = last[u]; i; i = e[i].nxt) {
int v = e[i].v, p = scc[u], q = scc[v];
if (p != q && !S.count(pii(p, q)) && !S.count(pii(q, p))) S.insert(pii(p, q)), g[p].emplace_back(q); //, printf("add %d %d\n", scc[u], scc[v]);
}
int S = scnt * 2 + 1, T = S + 1;
cerr << "scnt=" << scnt << "\n";
for (int u = 1; u <= scnt; ++u)
for (auto v : g[u])
Flow::adde(u + scnt, v, INF, 0);
for (int u = 1; u <= scnt; ++u) {
Flow::adde(S, u, INF, 1), Flow::adde(u + scnt, T, INF, 0);
Flow::adde(u, scnt + u, 1, -INF), Flow::adde(u, scnt + u, INF, 0);
}
pll res = Flow::min_cost_zkw(S, T, T);
cerr << "ans=" << res.first << '\n';
// cout << scnt - ans << "\n";
// for (int u = 1; u <= scnt; ++u)
// printf("Match[%d]=%d\n", u, match[u]);
for (int u = 1; u <= scnt; ++u)
if (!color[seq[u][0]]) {
++cc;
int p = u;
while (p != S) {
// printf("p=%d:", p);
for (auto x : seq[p])
color[x] = cc; //, printf("color[%d]=%d\n", x, color[x]);
bool ok = 0;
for (int i = Flow::last[p]; i; i = Flow::e[i].nxt) {
int v = Flow::e[i].v;
if ((v - scnt) > p && Flow::e[i].w == 1) {
p = v - scnt;
ok = 1;
break;
}
}
if (!ok) break;
}
}
for (int u = 1; u <= n; ++u)
cout << color[u] << " ";
cout << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7868kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 1 2 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 0ms
memory: 7848kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 2ms
memory: 9740kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
1 1 1 1 2 1 3 4
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 1593ms
memory: 9432kb
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:
2161 94 425 187 2993 2850 3227 386 641 246 2066 1824 3190 1995 232 3342 1235 3421 1049 1726 1253 1166 2354 1498 89 2106 3321 3410 347 1802 580 553 2212 3418 394 393 1465 2530 2221 2326 226 1137 2352 1217 2411 1064 1980 2264 2363 2438 861 3372 459 3184 104 1564 2146 2514 720 1982 559 3074 3179 2627 1...
result:
wrong answer Integer 2161 violates the range [1, 1750]