QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#553587#6329. Colorful Graphucup-team3691#WA 5ms6036kbC++236.1kb2024-09-08 16:18:312024-09-08 16:18:31

Judging History

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

  • [2024-09-08 16:18:31]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:6036kb
  • [2024-09-08 16:18:31]
  • 提交

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 edge_ind : G[d]) {
                int min_flow = 1e9;
                if (prv[edge_list[edge_ind].nxt] == -1) {
                    continue;
                }
                prv[d] = edge_ind^1;
                for (int nod = d; nod != s; nod = edge_list[prv[nod]^1].nxt) {
                    min_flow = min(min_flow, edge_list[prv[nod]].cap - edge_list[prv[nod]].flow);
                }
                for (int nod = d; nod != s; nod = edge_list[prv[nod]^1].nxt) {
                    edge_list[prv[nod]].flow += min_flow;
                    edge_list[prv[nod] ^ 1].flow -= min_flow;
                }
                total_flow += min_flow;
            }

        }
    }
    void dfsSolution(int nod, vector <int> &color, int c) {
        color[nod] = c;
        for (int edge_ind : G[2 * nod + 1]) {
            if (edge_list[edge_ind].flow != 1) {
                continue;
            }
            if (edge_list[edge_ind].nxt == 0 || edge_list[edge_ind].nxt == n - 1) {
                continue;
            }
            int nxt = edge_list[edge_ind].nxt / 2 - 1;
            if (!color[nxt]) {
                dfsSolution(nxt, color, c);
            }
        }
    }
    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, 1);
        for (int x : G[i]) {
            flowGraph.addEdge(2 * i + 1, 2 * x + 2, 1);
        }
    }
    flowGraph.pushFlow();
    vector <int> color(n, 0);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        if (color[i]) {
            continue;
        }
        cnt++;
        flowGraph.dfsSolution(i, color, cnt);
    }
    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: 3544kb

input:

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

output:

1 1 2 1 1 

result:

ok AC

Test #2:

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

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

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: 5ms
memory: 6036kb

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:

1750 1843 2174 1936 1383 1749 1748 2135 2390 1995 1342 373 1747 157 1981 1746 2984 1745 2439 3475 2243 2915 135 2002 1838 1744 1743 996 2096 1742 2329 2302 1741 1740 2143 2142 3214 1739 111 1738 1975 2342 1737 2966 1736 2813 1735 1060 1734 1733 2610 1732 2208 355 1853 3313 850 1731 2469 1730 2308 56...

result:

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