QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#151537#6329. Colorful GraphUFRJ#WA 6ms5408kbC++204.4kb2023-08-26 22:09:422023-08-26 22:09:45

Judging History

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

  • [2023-08-26 22:09:45]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:5408kb
  • [2023-08-26 22:09:42]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using lint = int64_t;

struct scc_t{
    int n, t, scc_num;
    vector<vector<int>> adj;
    vector<int> low, id, stk, in_stk, cc_id;
    scc_t(const vector<vector<int>> & g) : n(int(g.size())), t(0), scc_num(0),
    adj(g), low(n, -1), id(n, -1), in_stk(n, false), cc_id(n){}
    template<class F> void dfs(int cur, F f){
        id[cur] = low[cur] = t++;
        stk.push_back(cur);
        in_stk[cur] = true;
        for(int nxt : adj[cur]){
            if(id[nxt] == -1){
                dfs(nxt, f);
                low[cur] = min(low[cur], low[nxt]);
            } else if(in_stk[nxt])
                low[cur] = min(low[cur], id[nxt]);
        }
        if(low[cur] == id[cur]){
            vector<int> cc; cc.reserve(stk.size());
            while(true){
                int v = stk.back(); stk.pop_back();
                in_stk[v] = false;
                cc.push_back(v);
                cc_id[v] = scc_num;
                if(v == cur) break;
            } f(cc); scc_num++;
        }
    }
    template<class F> void solve(F f){
        stk.reserve(n);
        for(int r =0 ; r< n; ++r)
            if(id[r] == -1) dfs(r, f);
    }
};

struct bipartite_matching{
    int N,M,T;
    vector<vector<int>> adj;
    vector<int> match, seen;
    bipartite_matching(int a, int b) : N(a), M(a+b), adj(M), match(M, -1), seen(M, -1), T(0){}
    void add_edge(int a, int b){
        assert(0 <= a && a < N && b + N < M && N <= b + N);
        adj[a].push_back(b + N);
    }
    void shuffle_edges(){
        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
        for(auto& cur : adj)
            shuffle(cur.begin(), cur.end(), rng);
    }
    bool dfs(int cur){
        if(seen[cur] == T) return false;
        seen[cur] = T;
        for(int nxt : adj[cur])
            if(match[nxt] == -1){
                match[nxt] = cur, match[cur] = nxt;
                return true;
            }
        for(int nxt : adj[cur])
            if(dfs(match[nxt])){
                match[nxt] = cur, match[cur] = nxt;
                return true;
            }
        return false;
    }
    int solve(){
        int res = 0;
        while(true){
            int cur = 0; ++T;
            for(int i=0; i<N; ++i)
                if(match[i] == -1) cur += dfs(i);
            if(cur == 0) break;
            else res += cur;
        }
        return res;
    }
};

using pii = pair<int, int>;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin>>n>>m;
    vector<int> in_degree(n);
    vector<pii> edges(m);
    vector<vector<int>> g(n);

    for(auto &[u, v] : edges){
        cin>>u>>v;
        u--, v--;
        g[u].push_back(v);
    }
    scc_t scc(g);
    scc.solve([](const vector<int> &x){});
    vector<vector<int>> dag(scc.scc_num);
    set<pii> dag_edges;
    {        
        for(const auto &[u, v] : edges){
            int a = scc.cc_id[u], b = scc.cc_id[v];
            if(a != b && !dag_edges.count({a, b})){
                dag[a].push_back(b);
                dag_edges.insert({a, b});
            }
        }
        // for(int u =0; u<n; u++){
        //     cout<<u<<" comp "<<scc.cc_id[u]<<"\n";
        // }
        // cout<<"dag edges \n";
        // for(const auto &[u, v] : dag_edges){
        //     cout<<u<<" "<<v<<"\n";
        // }
    }
    bipartite_matching match(scc.scc_num, scc.scc_num);
    /*
    0 -- 0
    */
    for(const auto &[u, v] : dag_edges){
        match.add_edge(u, v);
    }
    int maxi = match.solve();
    // cout<<" match "<<maxi<<"\n";
    // cout<<scc.scc_num - maxi<<"\n";

    vector<int> color(scc.scc_num, -1);
    int current_color = 1;

    // for(int i=0; i<scc.scc_num; i++){
    //     cout<<"s"<<i<<" math com s"<<match.match[i+scc.scc_num]<<"\n";
    // }
    for(int i=0; i<scc.scc_num; i++){

        if(match.match[i+scc.scc_num] == -1){
            color[i] = current_color;
            int cur = i; // T
            int mat = match.match[cur]; //S
            while(mat != -1){
                cur = mat - scc.scc_num; // T
                color[cur] = current_color;
                mat = match.match[cur]; // S
            }
            current_color++;
        }
    }
    vector<int> ans(n);
    for(int i=0; i<n; i++){
        ans[i] = color[scc.cc_id[i]];
        cout<<ans[i]<<" ";
    }
    cout<<"\n";



    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3520kb

input:

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

output:

1 2 2 1 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

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

output:

2 2 1 2 2 

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 3516kb

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
Wrong Answer
time: 6ms
memory: 5408kb

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 1551 913 1363 1752 1751 1752 990 459 1259 1750 1750 1753 1750 1289 1754 725 1755 361 1711 760 591 1752 1247 1561 1756 1757 1755 1076 1758 579 633 1759 1760 976 976 1173 1761 1752 1762 1297 546 1763 696 1764 389 1765 1752 1766 1767 17 1768 836 1752 1527 1390 1750 1769 292 1770 619 1752 1752 1752...

result:

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