QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97481#6329. Colorful Graphjeroenodb#TL 6282ms28388kbC++205.4kb2023-04-16 21:54:002023-04-16 21:54:16

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:54:16]
  • 评测
  • 测评结果:TL
  • 用时:6282ms
  • 内存:28388kb
  • [2023-04-16 21:54:00]
  • 提交

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;
    }
};
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);}
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);
    vi p(n);
    iota(all(p),0);
    shuffle(all(p),rng);
    while(m--) {
        int u,v; cin >> u >> v;
        --u,--v;
        u = p[u],v = p[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[p[i]]];
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

2 2 2 2 4 2 3 1

result:

ok AC

Test #4:

score: 0
Accepted
time: 6282ms
memory: 28324kb

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:

1652 991 721 269 1061 16 704 591 1540 1549 1079 685 1171 1056 1692 1082 573 1300 368 523 1507 737 1238 895 1657 1721 1544 960 773 1725 1748 1665 520 461 636 945 26 177 1724 1313 426 616 881 2 378 1246 1038 1432 1146 756 401 958 1108 1222 707 741 1050 496 363 1658 163 1037 1020 1031 274 1737 619 137 ...

result:

ok AC

Test #5:

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

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:

2232 854 744 457 1085 337 97 342 2172 310 1959 1795 560 397 890 1915 2176 116 687 970 859 822 315 2190 1672 124 1338 1282 2085 1145 2061 1445 895 414 67 1269 1738 703 772 1306 393 850 52 230 1212 803 697 1924 390 282 1406 1340 2322 1638 1495 657 1903 412 1263 2280 847 1854 1859 34 264 1459 1361 1831...

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: