QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100675#6329. Colorful GraphSolitaryDream#TL 2ms4640kbC++204.2kb2023-04-27 16:34:012023-04-27 16:34:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-27 16:34:05]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:4640kb
  • [2023-04-27 16:34:01]
  • 提交

answer


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
namespace Flow {
    const int N = 14010;
    vector<int> H[N];
    int ans[N];
    int nn;
    int h[N], gap[N], in[N];
    struct edge{
        int t, f; 
        edge *nxt, *pair; 
        int tmpf;
    } *g[N], *d[N];
    void Add(int s, int t, int f) {
        edge *p = new(edge); p->t = t; p->f = p->tmpf = f; p->nxt = g[s]; g[s] = p;
        p = new(edge); p->t = s; p->f = p->tmpf = 0; p->nxt = g[t];
        g[t] = p; g[s]->pair = g[t]; g[t]->pair = g[s];
    }
    inline int sap(int v, int flow, int S, int T) {
        if (v == T) return flow;
        int rec = 0;
        for (edge *p = d[v]; p; p = p->nxt) if (h[v] == h[p->t] + 1 && p->f) {
            int ret = sap(p->t, min(flow - rec, p->f), S, T);
            p->f -= ret; p->pair->f += ret; d[v] = p;
            if ((rec += ret) == flow) return flow;
        }
        d[v] = g[v];
        if (!(--gap[h[v]])) h[S] = T + 2;
        gap[++h[v]]++;
        return rec;
    }
    inline bool Check(int lim, int sm, edge *ip, int SS, int TT) {
        for (int u = 1; u <= TT; ++u) 
            for (edge *p = g[u]; p; p = p->nxt) 
                p->f = p->tmpf;
        ip->f = lim;
        for (int i = 0; i <= TT; ++i) gap[i] = h[i] = 0;
        gap[0] = TT;
        for (int i = 0; i <= TT; ++i) d[i] = g[i];
        int maxflow = 0;
        while (h[SS] < TT) maxflow += sap(SS, nn, SS, TT);
        return sm == maxflow;
    }
    vector<int> vec[N];
    inline void Split(vector<int> &a, vector<int> &b, int num) {
        b.insert(b.end(), a.end() - num, a.end());
        a.erase(a.end() - num, 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);
        edge *ip = g[T];
        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);
        vec[S].resize(lim);
        for (int i = 1; i <= lim; ++i) vec[S][i - 1] = i;
        for (edge *p = g[S]; p; p = p->nxt) if (p->t <= nn) {
            Split(vec[S], vec[p->t], p->pair->f);
        }
        for (int i = nn; i; --i) {
            ans[i] = vec[i].front();
            for (edge *p = g[i + nn]; p; p = p->nxt) if (p->t <= nn) {
                Split(vec[i], vec[p->t], p->pair->f);
            }
        }
    }
};
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);
    for (int i = 1, x, y; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4640kb

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: 0ms
memory: 4636kb

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: 4640kb

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
Time Limit Exceeded

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:


result: