QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#554003#6329. Colorful Graphucup-team3691WA 535ms52544kbC++236.6kb2024-09-09 01:27:302024-09-09 01:27:31

Judging History

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

  • [2024-09-09 01:27:31]
  • 评测
  • 测评结果:WA
  • 用时:535ms
  • 内存:52544kb
  • [2024-09-09 01:27:30]
  • 提交

answer

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <ctime>
#include <random>
#include <cassert>

using namespace std;
using ll = long long;

const int mod = 998244353;

struct Edge {
    int nxt, flow, cap;
};

struct Flow {
    int n, total_flow;
    vector <vector <int>> G;
    vector <Edge> edge_list;
    int s, d;
    vector <int> prv;
    bool bfs() {
        queue <int> q;
        for (int i = 0; i < n; i++) {
            prv[i] = -1;
        }
        prv[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int curr = q.front();
            q.pop();
            for (int edge_ind : G[curr]) {
                Edge curr_edge = edge_list[edge_ind];
                if (prv[curr_edge.nxt] == -1 && curr_edge.flow < curr_edge.cap) {
                    prv[curr_edge.nxt] = edge_ind;
                    q.push(curr_edge.nxt);
                }
            }
            if (prv[d] != -1) {
                return 1;
            }
        }
        return 0;
    }

    Flow(int _n) {
        n = _n;
        G.resize(n);
        prv.resize(n);
        s = 0;
        d = n - 1;
        total_flow = 0;
    }
    void addEdge(int x, int y, int cap) {
        edge_list.push_back({y, 0, cap});
        edge_list.push_back({x, 0, 0});
        G[x].push_back(edge_list.size() - 2);
        G[y].push_back(edge_list.size() - 1);
    }
    void pushFlow() {
        while (bfs()) {
            for (int nod = d; nod != s; nod = edge_list[prv[nod]^1].nxt) {
                edge_list[prv[nod]].flow++;
                edge_list[prv[nod] ^ 1].flow--;
            }
            total_flow++;
        }
    }
    void afis() {
        for (int i = 0; i < edge_list.size(); i += 2) {
            if (edge_list[i].cap == 1) {
                continue;
            }
            if (edge_list[i].flow > 0) {
                cout << (edge_list[i ^ 1].nxt - 1) / 2 + 1 << ' ' << (edge_list[i].nxt - 2) / 2 + 1 << ' ' << edge_list[i].flow << ' ' << edge_list[i].cap << '\n';
            }
        }
    }

    vector <vector <pair <int, int>>> getDistrib(int nr) {
        vector <vector <pair <int, int>>> distribG(nr);
        for (int i = 0; i < edge_list.size(); i += 2) {
            if (edge_list[i].cap == 1) {
                continue;
            }
            if (edge_list[i].flow > 0) {
                distribG[(edge_list[i ^ 1].nxt - 1) / 2].push_back({(edge_list[i].nxt - 2) / 2, edge_list[i].flow});
                //cerr << (edge_list[i ^ 1].nxt - 1) / 2 << ' ' << (edge_list[i].nxt - 2) / 2 << ' ' << edge_list[i].flow << '\n';
            }
        }
        return distribG;
    }

    int getFlow() {
        return total_flow;
    }
};


struct Graph {
    int n, m, nrctc;
    vector <pair <int, int>> edges;
    vector <vector <int>> G, revG;
    vector <int> comp;
    void readGraph() {
        for (int i = 0; i < m; i++) {
            int x, y;
            cin >> x >> y;
            x--, y--;
            edges[i] = {x, y};
            G[x].push_back(y);
            revG[y].push_back(x);
        }
    }

    void dfs1(int nod, vector <int> &used, vector <int> &ordine) {
        used[nod] = 1;
        for (int nxt : G[nod]) {
            if (used[nxt]) {
                continue;
            }
            dfs1(nxt, used, ordine);
        }
        ordine.push_back(nod);
    }
    void dfs2(int nod, int c) {
        comp[nod] = c;
        for (int nxt : revG[nod]) {
            if (comp[nxt]) {
                continue;
            }
            dfs2(nxt, c);
        }
    }

    void computeScc() {
        vector <int> used(n, 0);
        vector <int> ordine;
        for (int i = 0; i < n; i++) {
            if (used[i]) {
                continue;
            }
            dfs1(i, used, ordine);
        }
        reverse(ordine.begin(), ordine.end());
        for (int i = 0; i < n; i++) {
            int nod = ordine[i];
            if (comp[nod]) {
                continue;
            }
            nrctc++;
            dfs2(nod, nrctc);
        }
    }

    vector <vector <int>> simplifyEdges() {
        vector <vector <int>> newG(nrctc, vector <int> ());
        for (auto [x, y] : edges) {
            if (comp[x] == comp[y]) {
                continue;
            }
            newG[comp[x] - 1].push_back(comp[y] - 1);
        }
        for (int i = 0; i < nrctc; i++) {
            sort(newG[i].begin(), newG[i].end());
            newG[i].erase(unique(newG[i].begin(), newG[i].end()), newG[i].end());
        }
        return newG;
    }

    void printSolution(vector <int> &color) {
        for (int i = 0; i < n; i++) {
            cout << color[comp[i] - 1] << ' ';
        }
    }

    Graph(int _n, int _m) {
        n = _n;
        m = _m;
        edges.resize(m);
        G.resize(n);
        revG.resize(n);
        comp.resize(n, 0);
        nrctc = 0;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    Graph sccGraph(n, m);
    sccGraph.readGraph();
    sccGraph.computeScc();
    vector <vector <int>> G = sccGraph.simplifyEdges();
    n = sccGraph.nrctc;

    Flow flowGraph(2 * n + 2);
    for (int i = 0; i < n; i++) {
        flowGraph.addEdge(0, 2 * i + 1, 1);
        flowGraph.addEdge(2 * i + 2, 2 * n + 1, 1);
        flowGraph.addEdge(2 * i + 2, 2 * i + 1, 1e9);
        for (int x : G[i]) {
            flowGraph.addEdge(2 * i + 1, 2 * x + 2, 1e9);
        }
    }
    flowGraph.pushFlow();
    vector <vector <pair <int, int>>> distribG = flowGraph.getDistrib(n);
    vector <vector <int>> colors(n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (colors[i].empty()) {
            cnt++;
            colors[i].push_back(cnt);
        }
        int poz = 0;
        for (auto [nod, demand] : distribG[i]) {
            for (int j = 0; j < demand; j++) {
                if (poz >= colors[i].size()) {
                    cnt++;
                    colors[i].push_back(cnt);
                }
                colors[nod].push_back(colors[i][poz]);
                poz++;
            }
        }
    }

    vector <int> color(n, 0);
    for (int i = 0; i < n; i++) {
        color[i] = colors[i][0];
    }
    sccGraph.printSolution(color);
}

signed main() {
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int nrt = 1;
    //cin >> nrt;
    while (nrt--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

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

output:

2 2 1 1 1 

result:

ok AC

Test #3:

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

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: 535ms
memory: 52544kb

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:

897541 1858924 2645259 2134298 1389 130020 38876 2572946 2958305 2269330 1348 373 38875 157 2232385 14149 2800774 4826 2997521 1601305 2773320 2888745 135 2285620 1843359 4825 4824 996 2488445 4823 2894590 2861838 4822 4821 2588650 2587676 2378635 4820 111 4819 2220747 2913716 4818 2822029 4817 2987...

result:

wrong answer Integer 897541 violates the range [1, 1750]