QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97477#6329. Colorful Graphjeroenodb#TL 7744ms28272kbC++205.1kb2023-04-16 21:44:082023-04-16 21:44:10

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-16 21:44:10]
  • 评测
  • 测评结果:TL
  • 用时:7744ms
  • 内存:28272kb
  • [2023-04-16 21:44:08]
  • 提交

answer

#pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;

struct SCC {
    vvi adj, rev,dag;
    vi order, order2;
    vi boss;
    vector<bool> visited;
    vector<bitset<7000>> reach;
    int n;
    #define rs(a) a.resize(n)
    SCC(int nn) {
        n=nn;
        rs(adj),rs(rev),rs(boss),rs(visited);
    }
    void addEdge(int a, int b) {
        adj[a].push_back(b);
        rev[b].push_back(a);
    }
    void dfs1(int at) {
        visited[at] = true;
        for(int to: adj[at]) if(!visited[to]) dfs1(to);
        order.push_back(at);
    }
    int id;
    void dfs2(int at) {
        visited[at] = true;
        boss[at] = id;
        order2.push_back(at);
        for(int to: rev[at]) if(!visited[to]) dfs2(to);
    }
    vector<bitset<7000>> run() {
        for(int i=0;i<n;++i) 
            if(!visited[i]) 
                dfs1(i);
        fill(all(visited),false);
        reverse(all(order));
        id=0;
        
        for(int i: order) {
            if(!visited[i]) {
                dfs2(i),id++;
            }
        }
        reach.resize(id);
        dag.resize(id);
        for(int i=0;i<n;++i) for(int to : adj[i]) {
            dag[boss[i]].push_back(boss[to]);
        }

        fill(all(visited),0);
        for(int i=0;i<id;++i) {
            if(!visited[i]) {
                auto dfsd = [&](auto&& self, int at) -> void {
                    visited[at]=1;
                    reach[at][at]=1;
                    for(int to : dag[at]) {
                         if(!visited[to])  {
                            self(self,to);
                            
                        }
                        reach[at]|=reach[to];
                    }
                };
                dfsd(dfsd,i);
            }
        }
        for(int i=0;i<id;++i) reach[i][i]=0;
        return reach;
    }
};
struct HopcroftKarp {

    const int N, M;
    std::vector<bitset<7000>> adj_left;
    std::vector<int> matchL, matchR;

    HopcroftKarp (int N, int M, vector<bitset<7000>> adj)
    : N(N), M(M), matchL(N, -1), matchR(M, -1), adj_left(adj) {
    }

    int maxmatching () {
        int sz = 0;
        for(int u=0;u<N;++u)  {
             for(int v=adj_left[u]._Find_next(u);v<M;v=adj_left[u]._Find_next(v)) {
                if(matchR[v]==-1) {
                    matchL[u]=v;
                    matchR[v]=u;
                    sz++;
                    break;
                }
             }
        }
        for (bool updated = true; updated; ) {
            updated = false;

            static std::vector<int> root(N), prev(N), qq(N);
            static int qi, qj;

            // std::queue<int> q;

            qi = qj = 0;
            std::fill(root.begin(), root.end(), -1),
            std::fill(prev.begin(), prev.end(), -1);

            for (int i = 0; i < N; i++)
                if (matchL[i] == -1)
                    qq[qj++] = i, root[i] = i, prev[i] = i;
                    // q.push(i), root[i] = i;

            while (qi < qj) {
                int u = qq[qi++];
                // int u = q.front(); q.pop();
                if (matchL[root[u]] != -1) continue;

                for(int v=adj_left[u]._Find_next(u);v<M;v=adj_left[u]._Find_next(v)) {
                    if (matchR[v] == -1) {
                        while (v != -1)
                            matchR[v] = u, std::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;
                        // v = matchR[v], prev[v] = u, root[v] = root[u], q.push(v);
                }
            }
        }

        return sz;
    }

};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m; cin >> n >> m;
    SCC scc(n);
    while(m--) {
        int u,v; cin >> u >> v;
        --u,--v;
        scc.addEdge(u,v);
    }
    
    auto es = scc.run();
    HopcroftKarp hp(scc.id,scc.id, es);
    hp.maxmatching();
    vector<bool> vis(n);
    int col=1;
    vi cl(scc.id);
    for(int i=0;i<scc.id;++i) {
        if(!vis[i]) {
            cl[i]=col++;    
        }
        int to = hp.matchL[i];
        if(to!=-1) cl[to]=cl[i],vis[to]=1;
    }
    vi ans(n);
    for(int i=0;i<n;++i) {
        ans[i] = cl[scc.boss[i]];
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 3380kb

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: 2ms
memory: 3456kb

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: 0
Accepted
time: 7744ms
memory: 28264kb

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 569 1623 1022 1383 1749 1748 1072 1747 714 1342 373 1747 157 730 1746 596 1745 120 622 1690 1247 135 1227 1184 1744 1743 996 1500 1742 153 325 1741 1740 257 1241 1235 1739 111 1738 1427 1736 1737 252 1736 1625 1735 1060 1734 1733 1286 1732 1018 355 1546 476 850 1731 141 1730 72 564 1484 1205 16...

result:

ok AC

Test #5:

score: 0
Accepted
time: 27ms
memory: 28272kb

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...

output:

2088 2098 827 544 469 1973 1566 1831 162 2236 2252 862 346 1945 1305 2333 1649 1371 1117 572 2332 1366 1951 1980 1550 242 706 2246 1570 1162 647 1564 1369 737 2331 2330 1887 983 332 994 2329 1624 2160 2328 1269 2327 528 2326 2325 2211 1616 2181 926 1863 502 593 2324 2323 2322 2321 204 113 2320 2202 ...

result:

ok AC

Test #6:

score: -100
Time Limit Exceeded

input:

7000 6999
1649 5337
1701 3344
4394 2172
3330 39
5932 1141
5381 5340
5453 3300
125 2172
6810 5263
804 2172
6635 2172
676 4740
3015 1183
1710 5769
611 5915
3419 1581
2094 2172
4508 2172
6604 2433
6113 1466
1604 696
1518 1123
1287 2940
4825 2172
5130 4524
2693 2172
106 2172
5157 2172
3693 2172
5198 217...

output:


result: