QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#97469#6329. Colorful Graphjeroenodb#TL 2ms3492kbC++204.5kb2023-04-16 21:21:072023-04-16 21:21: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:21:10]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3492kb
  • [2023-04-16 21:21:07]
  • 提交

answer

#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<vector<bool>> 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<vector<bool>> 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,vector<bool>(id));
        dag.resize(id);
        for(int i=0;i<n;++i) for(int to : adj[i]) {
            dag[boss[i]].push_back(boss[to]);
        }
        for(int i=0;i<id;++i) {
            fill(all(visited),0);
            auto dfsd = [&](auto&& self, int at) -> void {
                visited[at]=1;
                if(i!=at) reach[i][at]=1;
                for(int to : dag[at]) if(!visited[to]) self(self,to);
            };
            dfsd(dfsd,i);
        }
        return reach;
    }
};
struct HopcroftKarp {

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

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

    int maxmatching () {
        int sz = 0;

        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=0;v<M;++v) if(adj_left[u][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';
}

詳細信息

Test #1:

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

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:

2 2 1 1 1

result:

ok AC

Test #3:

score: 0
Accepted
time: 2ms
memory: 3492kb

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
Time Limit Exceeded

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:


result: