QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#100701 | #6329. Colorful Graph | SolitaryDream# | TL | 7719ms | 7752kb | C++20 | 4.8kb | 2023-04-27 17:31:53 | 2023-04-27 17:31:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
namespace Flow {
const int N = 14010;
const int M = N * 10;
vector<int> H[N];
int ans[N];
int nn;
int h[N], gap[N], in[N];
int tot = 1, a[M], ne[M], c[M], tmpc[M], fi[N];
void Add(int x, int y, int z) {
a[++tot] = y; ne[tot] = fi[x]; fi[x] = tot; c[tot] = tmpc[tot] = z;
a[++tot] = x; ne[tot] = fi[y]; fi[y] = tot; c[tot] = tmpc[tot] = 0;
}
int cur[N], de[N];
inline bool Bfs(int s, int t) {
static queue<int> q;
for (int i = 1; i <= t; ++i) de[i] = 0;
de[s] = 1; q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = fi[u]; i; i = ne[i]) if (c[i] && !de[a[i]]) {
de[a[i]] = de[u] + 1;
q.push(a[i]);
}
}
return de[t];
}
inline int Dfs(int x, int flow, int t) {
if (x == t) return flow;
int tmp, sm = 0;
for (int &i = cur[x]; i; i = ne[i]) if (c[i] && de[x] + 1 == de[a[i]] && (tmp = Dfs(a[i], min(flow, c[i]), t))) {
c[i] -= tmp; c[i ^ 1] += tmp;
flow -= tmp; sm += tmp;
if (!flow) return sm;
}
return sm;
}
inline bool Check(int lim, int sm, int ip, int SS, int TT) {
for (int i = 2; i <= tot; ++i) c[i] = tmpc[i];
c[ip] = lim;
int maxflow = 0;
while (Bfs(SS, TT)) {
memcpy(cur + 1, fi + 1, TT * sizeof *cur);
maxflow += Dfs(SS, nn, TT);
}
return sm == maxflow;
}
list<int> vec[N];
inline void Split(list<int> &a, list<int> &b, int num) {
auto it = a.end();
while (num--) --it;
b.insert(b.end(), it, a.end());
a.erase(it, a.end());
}
inline void Solve() {
int S = nn * 2 + 1, T = S + 1, SS = T + 1, TT = SS + 1;
for (int i = 1; i <= nn; ++i) {
int j = i + nn;
in[i] -= 1; in[j] += 1;
Add(i, j, nn);
Add(S, i, nn);
Add(j, T, nn);
for (auto ni : H[i]) Add(j, ni, nn);
}
int sm = 0;
for (int i = 1; i <= TT; ++i) if (in[i] > 0) sm += in[i], Add(SS, i, in[i]); else Add(i, TT, -in[i]);
Add(T, S, 0);
int ip = tot - 1;
int lim = 0;
for (int l = 1, r = nn; l <= r; ) {
int mid = (l + r) >> 1;
if (Check(mid, sm, ip, SS, TT)) lim = mid, r = mid - 1; else l = mid + 1;
}
Check(lim, sm, ip, SS, TT);
// printf("lim = %d\n", lim);
for (int i = 1; i <= lim; ++i) vec[S].push_back(i);
// puts("here");
for (int i = fi[S]; i; i = ne[i]) if (a[i] <= nn && c[i ^ 1]) {
// printf("%d -> %d %d\n", S, a[i], c[i ^ 1]);
Split(vec[S], vec[a[i]], c[i ^ 1]);
}
for (int x = nn; x; --x) {
// printf("%d %d\n", x, (int)vec[x].size());
ans[x] = vec[x].front();
for (int i = fi[x + nn]; i; i = ne[i]) if (a[i] <= nn) {
// printf("%d -> %d %d\n", x, a[i], c[i ^ 1]);
Split(vec[x], vec[a[i]], c[i ^ 1]);
}
}
// puts("fin");
}
};
const int N = 7010;
int n, m;
vector<int> g[N], h[N];
int fa[N];
int col[N], colnum, dfn[N], low[N], dclk;
int deg[N], ins[N];
stack<int> sta;
inline void Tarjan(int x) {
sta.push(x); ins[x] = 1;
dfn[x] = low[x] = ++dclk;
for (auto y : g[x])
if (!dfn[y]) Tarjan(y), low[x] = min(low[x], low[y]);
else if (ins[y]) low[x] = min(low[x], dfn[y]);
if (low[x] == dfn[x]) {
col[x] = ++colnum;
while (sta.top() != x) {
col[sta.top()] = colnum;
ins[sta.top()] = 0;
sta.pop();
}
ins[x] = 0;
sta.pop();
}
}
int main() {
scanf("%d%d", &n, &m);
// srand(time(0));
// n = 7000; m = 7000;
for (int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
// if (i <= 3500) {
// x = i, y = i + 1;
// } else {
// x = 3501, y = i;
// }
g[x].push_back(y);
}
for (int i = 1; i <= n; ++i) if (!dfn[i]) {
Tarjan(i);
}
for (int x = 1; x <= n; ++x)
for (auto y : g[x]) if (col[x] != col[y]) {
Flow::H[col[x]].push_back(col[y]);
++deg[col[y]];
}
for (int i = 1; i <= colnum; ++i) {
using Flow::H;
sort(H[i].begin(), H[i].end());
H[i].erase(unique(H[i].begin(), H[i].end()), H[i].end());
}
Flow::nn = colnum;
Flow::Solve();
for (int i = 1; i <= n; ++i) printf("%d%c", Flow::ans[col[i]], " \n"[i == n]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 6564kb
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: 2ms
memory: 6352kb
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: 1ms
memory: 6016kb
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: 0
Accepted
time: 7719ms
memory: 7752kb
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:
1 591 1653 628 368 2 3 668 152 703 409 1378 4 1594 21 5 858 6 448 1302 1055 1518 1616 1340 1231 7 8 755 1410 9 219 1096 10 11 909 909 1626 12 1640 13 946 101 14 513 15 18 16 691 17 18 111 19 355 1396 1684 1191 901 20 1211 21 13 1187 267 546 153 22 1694 1197 544 23 24 288 25 862 26 1510 1128 1675 114...
result:
ok AC
Test #5:
score: -100
Time Limit Exceeded
input:
7000 6999 4832 1603 5984 6985 5355 3687 6007 2170 5984 3486 3267 2189 538 2123 4343 4553 5855 6168 5984 257 4239 2304 5984 2063 3298 1869 5984 6353 5984 2018 5984 5387 5984 3382 3164 3978 2690 2816 4810 2638 5984 3773 5984 1634 5984 2786 5984 3671 5984 5140 2943 5721 5984 414 1105 4060 3093 796 5984...