QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181005#6329. Colorful Graphucup-team228#TL 2ms11560kbC++205.3kb2023-09-16 15:00:022023-09-16 15:00:02

Judging History

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

  • [2023-09-16 15:00:02]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:11560kb
  • [2023-09-16 15:00:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 7e3 + 10;

struct DSU {
    int Parent[N], Rank[N], Size[N], cnt;
    void init(int n) {
        for (int i = 1; i <= n; i++) {
            Parent[i] = i, Rank[i] = 0, Size[i] = 1;
        }
        cnt = n;
    }
    int find(int v) {
        if (Parent[v] == v) return v;
        else return Parent[v] = find(Parent[v]);
    }
    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        if (Rank[u] < Rank[v]) swap(u, v);
        if (Rank[v] == Rank[u]) Rank[u]++;
        Parent[v] = u, cnt--;
        Size[u] += Size[v];
    }
};

struct HopcroftKarp {
    bitset<N> adj_left[N];
    int matchL[N] = {};
    int matchR[N] = {};
    int root[N], prev[N], qq[N];

    void add_edge(int u, int v) {
        adj_left[u][v] = true;
    }
    void init(int L, int R) {
        for (int i = 1; i <= L; i++) {
            adj_left[i].reset();
            matchL[i] = 0;
        }
        for (int i = 1; i <= R; i++) {
            matchR[i] = 0;
        }
    }
    void init_matching(int L, int R) {
        for (int i = 1; i <= L; i++) {
            matchL[i] = 0;
        }
        for (int i = 1; i <= R; i++) {
            matchR[i] = 0;
        }
    }
    int maxmatching(int L, int R) {
        int sz = 0;
        for (bool updated = true; updated; ) {
            updated = false;
            for (int i = 1; i <= L; i++) {
                root[i] = -1;
                prev[i] = -1;
                qq[i] = 0;
            }
            int qi = 0, qj = 0;
            for (int i = 1; i <= L; i++) {
                if (matchL[i] == 0) {
                    qq[qj++] = i, root[i] = i, prev[i] = i;
                }
            }
            while (qi < qj) {
                int u = qq[qi++];
                if (matchL[root[u]] != 0) {
                    continue;
                }
                for (int v = adj_left[u]._Find_first(); v <= R; v = adj_left[u]._Find_next(v)) {
                    if (matchR[v] == 0) {
                        while (v != 0) {
                            matchR[v] = u, swap(matchL[u], v), u = prev[u];
                        }
                        updated = true;
                        sz++;
                        break;
                    }
                    if (prev[matchR[v]] == -1) {
                        v = matchR[v], prev[v] = u, root[v] = root[u], qq[qj++] = v;
                    }
                }
            }
        }
        return sz;
    }
};

DSU dsu;
HopcroftKarp hk;

vector<int> g[2][N];
bool used[N];
vector<int> post;
vector<vector<int>> comps;
int id[N];
int col_comp[N];
int ans[N];

vector<int> gr[N];
bitset<N> dp[N];

void add_edge(int u, int v) {
    g[0][u].push_back(v);
    g[1][v].push_back(u);
}

void dfs_inv(int v) {
    used[v] = true;
    for (int to : g[1][v]) {
        if (!used[to]) {
            dfs_inv(to);
        }
    }
    post.push_back(v);
}

void dfs(int v, vector<int>& comp) {
    used[v] = true;
    comp.push_back(v);
    for (int to : g[0][v]) {
        if (!used[to]) {
            dfs(to, comp);
        }
    }
}

void Kosaraju(int n) {
    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            dfs_inv(i);
        }
    }
    reverse(post.begin(), post.end());
    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    for (int v : post) {
        if (!used[v]) {
            vector<int> comp;
            dfs(v, comp);
            comps.push_back(comp);
        }
    }
    reverse(comps.begin(), comps.end());
    for (int i = 0; i < comps.size(); i++) {
        for (int v : comps[i]) {
            id[v] = i + 1;
        }
    }
    // edges go from left to right
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        add_edge(u, v);
    }
    Kosaraju(n);
    int k = comps.size();
    for (int i = 1; i <= n; i++) {
        for (int j : g[0][i]) {
            if (id[i] != id[j]) {
                gr[id[i]].push_back(id[j]);
            }
        }
    }

    for (int i = k; i >= 1; i--) {
        for (int j : gr[i]) {
            dp[i] |= dp[j];
            dp[i][j] = true;
        }
    }
    for (int i = 1; i <= k; i++) {
        hk.adj_left[i] = dp[i];
    }

    hk.maxmatching(k, k);
    dsu.init(k);
    for (int i = 1; i <= k; i++) {
        if (hk.matchL[i]) {
            dsu.unite(i, hk.matchL[i]);
        }
    }
    for (int i = 1, j = 0; i <= k; i++) {
        if (dsu.find(i) == i) {
            col_comp[i] = ++j;
        }
    }
    for (int i = 1; i <= k; i++) {
        col_comp[i] = col_comp[dsu.find(i)];
        for (int v : comps[i - 1]) {
            ans[v] = col_comp[i];
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 11560kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 1 2 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 0ms
memory: 10756kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

1 1 2 1 1 

result:

ok AC

Test #3:

score: 0
Accepted
time: 2ms
memory: 10764kb

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: