QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#379720#2341. Dead-End Detectordistant_yesterday#AC ✓324ms147648kbC++203.4kb2024-04-06 18:29:472024-04-06 18:29:47

Judging History

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

  • [2024-04-06 18:29:47]
  • 评测
  • 测评结果:AC
  • 用时:324ms
  • 内存:147648kb
  • [2024-04-06 18:29:47]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef LOCAL
#include "cp_debug.h"
#else
#define debug(...) 42
#endif
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
template<typename T> void write(T x) {
    if(x > 9) write(x/10);
    putchar(x%10 + '0');
}

void solve() {
    int n, m;
    read(n, m);
    vector<vi> G(n+1), nG(n+1);
    rep(i,0,m) {
        int u, v;
        read(u, v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vi dfn(n+1), cyc_mark(n+1), fath(n+1), in_cyc(n+1), cyc_siz(n+1);

    vector<pii> ans;
    rep(i,1,n+1) if(!dfn[i]) {
        int dfs_clock = 0;
        vi this_cc;
        vector<pii> cyc_edges;
        function<void(int,int)> dfs = [&](int u, int fa) {
            this_cc.emplace_back(u);
            fath[u] = fa;
            dfn[u] = ++dfs_clock;
            for(auto v: G[u]) if(v != fa) {
                if(dfn[v] == 0) {
                    nG[u].emplace_back(v);
                    nG[v].emplace_back(u);
                    dfs(v, u);
                } else if(dfn[v] < dfn[u]) {
                    cyc_mark[u]++;
                    if(fath[v] != -1) {
                        cyc_mark[fath[v]]--;
                    }
                    cyc_edges.emplace_back(v, u);
                }
            }
        };
        dfs(i,-1);

        debug(nG);
        debug(dfn);
        debug(this_cc);
        debug(cyc_edges);
        debug(cyc_mark);

        if(cyc_edges.empty()) {
            // output all leaf -> parent edges
            for(auto u: this_cc) if(sz(G[u]) == 1) {
                ans.emplace_back(u, G[u][0]);
            }
        } else {
            function<void(int,int)> populate_cyc = [&](int u, int fa) {
                in_cyc[u] = cyc_mark[u];
                for(auto v: nG[u]) if(v != fa) {
                    populate_cyc(v,u);
                    in_cyc[u] += in_cyc[v];
                }
            };
            populate_cyc(i,-1);
            debug(in_cyc);

            int rt = cyc_edges[0].second;
            debug(rt);
            assert(in_cyc[rt] > 0);
            function<void(int,int)>  calc_cyc_comp = [&](int u, int fa) {
                cyc_siz[u] = in_cyc[u] > 0;
                for(auto v: nG[u]) if(v != fa) {
                    calc_cyc_comp(v,u);
                    cyc_siz[u] += cyc_siz[v];
                }
            };
            calc_cyc_comp(rt,-1);
            debug(cyc_siz);

            for(auto u: this_cc) if(cyc_siz[u] > 0) {
                for(auto v: G[u]) if(cyc_siz[v] == 0) {
                    ans.emplace_back(u, v);
                }
            }
        }
        debug(ans);
    }
    sort(all(ans));
    cout<<sz(ans)<<'\n';
    for(auto [u, v]: ans) {
        cout<<u<<' '<<v<<'\n';
    }
}

signed main() {
    int T; T = 1;
    while(T--) solve();
    return 0;
}

详细

Test #1:

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

Test #2:

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

Test #3:

score: 0
Accepted
time: 281ms
memory: 147628kb

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

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

Test #8:

score: 0
Accepted
time: 19ms
memory: 36556kb

Test #9:

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

Test #10:

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

Test #11:

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

Test #12:

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

Test #13:

score: 0
Accepted
time: 14ms
memory: 13000kb

Test #14:

score: 0
Accepted
time: 23ms
memory: 12524kb

Test #15:

score: 0
Accepted
time: 240ms
memory: 72700kb

Test #16:

score: 0
Accepted
time: 188ms
memory: 77764kb

Test #17:

score: 0
Accepted
time: 247ms
memory: 79336kb

Test #18:

score: 0
Accepted
time: 221ms
memory: 79220kb

Test #19:

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

Test #20:

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

Test #21:

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

Test #22:

score: 0
Accepted
time: 73ms
memory: 24016kb

Test #23:

score: 0
Accepted
time: 324ms
memory: 124276kb

Test #24:

score: 0
Accepted
time: 62ms
memory: 147576kb

Test #25:

score: 0
Accepted
time: 213ms
memory: 143664kb

Test #26:

score: 0
Accepted
time: 99ms
memory: 147632kb

Test #27:

score: 0
Accepted
time: 305ms
memory: 147572kb

Test #28:

score: 0
Accepted
time: 280ms
memory: 147648kb

Test #29:

score: 0
Accepted
time: 305ms
memory: 147616kb

Test #30:

score: 0
Accepted
time: 93ms
memory: 71344kb

Test #31:

score: 0
Accepted
time: 206ms
memory: 71448kb

Test #32:

score: 0
Accepted
time: 79ms
memory: 78400kb

Test #33:

score: 0
Accepted
time: 210ms
memory: 79256kb

Test #34:

score: 0
Accepted
time: 318ms
memory: 135460kb

Test #35:

score: 0
Accepted
time: 239ms
memory: 73720kb

Test #36:

score: 0
Accepted
time: 277ms
memory: 85140kb

Test #37:

score: 0
Accepted
time: 306ms
memory: 100548kb

Test #38:

score: 0
Accepted
time: 259ms
memory: 70612kb

Test #39:

score: 0
Accepted
time: 243ms
memory: 69868kb

Test #40:

score: 0
Accepted
time: 256ms
memory: 87368kb

Test #41:

score: 0
Accepted
time: 222ms
memory: 70544kb

Test #42:

score: 0
Accepted
time: 236ms
memory: 65020kb

Test #43:

score: 0
Accepted
time: 231ms
memory: 65480kb

Test #44:

score: 0
Accepted
time: 211ms
memory: 77724kb

Test #45:

score: 0
Accepted
time: 195ms
memory: 78716kb