跳到正文

题解

题解:CF240E Road Repairs

Eiralys约 17 分钟
原文

这是 2026年8月12日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月12日
收录
2026年8月12日
更新
2026年11月8日
篇幅
约 17 分钟
获赞
1
题目
CF240E

版本

  1. v2当前
  2. v1

这似乎是一道最小树形图输出方案的模版题喵。

题意

给你一只有 n 个点 m 条边的有向图,每条边的边权为 0 或 1。

要求以 1 为根的 超可爱 最小树形图,并输出 超可爱 最小树形图中所有边权为 1 的边。无解输出 -1。

注意到有输入输出文件 (我是绝对不会说我在这上面卡了十分钟,看了讨论区才发现的)。

思路

正如开头所说的,这几乎是一道模版题,所以直接套用 这题 的代码即可,但是注意一下方案输出的方法:

在进行 tarjan 的时候,我们每次会将一只可爱的环给 暴力 拆开,而拆开时的边就是我们选中的边。

所以我们只需要记录一下每次拆的时候边的编号等信息即可喵,具体实现看代码喵。

Code

C++
// I often find myself reminiscing about the past.
// Moments of life freeze in my mind.
// I cut, fold, and curl the time behind me, kneading it into the white clouds drifting across the sky.
// Even clouds have their distinctions: cumulus clouds are heavy, while cirrus clouds are ethereal.
// The moments in life that strike me with awe pass through my thoughts and remain unforgettable forever, while the more ordinary, everyday memories leave behind only fragments under the erosion of time.
// Reminiscence is like entering a dream—too clear, and it cannot delight my imagination; too vague, and it sinks into nothingness.
// Only the mist-shrouded mountains and rivers, the veiled woman—that perfect haziness—can satisfy my longing for beauty.
// Reminiscence always wraps me, unbidden, in the yellowed pages of the past.
// Friends parted and reunited, streets demolished and rebuilt—each clue helps me set out from a specific moment and sail upstream against the river of time.
// The days gone by cannot be relived; I am merely a passerby.
// Yet I still long to leave myself moments of leisure on every journey into memory—to pause before a scene, to gaze across the haze of years at my former self, and to savor as much sweetness as I can.
// If beautiful times once flowed through my body, I am already content.
// The past has solidified.
// I carry my memories forward, but I often neglect to tend to them, and so the memories themselves keep changing their forms.
// This brings a certain challenge to my journeys of recollection.
// Where should I linger? I ask myself.

#include <bits/stdc++.h>
// #define LOCAL
// #define FILE
#define IOS() std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0)
#define re register
// #define int long long
using pii = std::pair <int, int>;
using ll = long long;

namespace ly {
    // 苦情巨树啊
    // 你听见了吗
    // 如果我也已经爱上了他
    // 我愿意用我们相识的一点一滴以及我的全部妖力来起誓
    // 让我们来生相见吧
    namespace IO {
        #ifndef LOCAL
            constexpr auto maxn = 1 << 21;
            char in[maxn], out[maxn], *p1 = in, *p2 = in, *p3 = out;
            #define getchar() (p1 == p2 && (p2 = (p1 = in) + fread(in, 1, maxn, stdin), p1 == p2) ? EOF : *p1++)
            #define flush() (fwrite(out, 1, p3 - out, stdout))
            #define putchar(x) (p3 == out + maxn && (flush(), p3 = out), *p3++ = (x))
            class Flush{ public: ~Flush(){ flush(); } } _;
        #endif
        namespace usr {
            template <typename type>
            inline type read(type &x){
                x = 0; bool flag(0); char ch = getchar();
                while(!isdigit(ch)) flag ^= ch == '-', ch = getchar();
                while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
                return flag ? x = -x : x;
            }
            template <typename type>
            inline void write(type x){
                x < 0 ? x = -x, putchar('-') : 0;
                static short Stack[50], top(0);
                do Stack[++top] = x % 10, x /= 10; while(x);
                while(top) putchar(Stack[top--] | 48);
            }
            inline char read(char &x){
                do x = getchar(); while(isspace(x));
                return x;
            }
            inline char write(const char &x){
                return putchar(x);
            }
            inline void read(char *x){
                static char ch;
                read(ch);
                do *(x++) = ch; while(!isspace(ch = getchar()) && ~ch);
            }
            template <typename type> inline void write(type *x){
                while(*x) putchar(*(x++));
            }
            inline void read(std::string &x){
                static char ch; read(ch), x.clear();
                do x += ch; while(!isspace(ch = getchar()) && ~ch);
            }
            inline void write(const std::string &x){
                for(int i = 0, len = x.length(); i < len; ++i) putchar(x[i]);
            }
            template <typename type, typename...T> inline void read(type &x, T&...y){
                read(x), read(y...);
            }
            template <typename type, typename...T>
            inline void write(const type &x, const T&...y){
                write(x), putchar(' '), write(y...), sizeof...(y) ^ 1 ? 0 : putchar('\n');
            }
            template <typename type>
            inline void put(const type &x, bool flag = 0){
                write(x), flag ? putchar('\n') : putchar(' ');
            }
        }
        #ifndef LOCAL
            #undef getchar
            #undef flush
            #undef putchar
        #endif
    } using namespace IO::usr;
} using namespace ly::IO::usr;

constexpr int N = 1e5 + 5;
struct Heap{
    int tot;
    struct Node{
        int v, lc, rc, tag;
    } tr[N];
    void init(){
        tot = 0;
    }
    int create(int x = 0){
        tr[++tot] = {x, 0, 0, 0};
        return tot;
    }
    void pushdown(int x){
        tr[x].v += tr[x].tag;
        if(tr[x].lc) tr[tr[x].lc].tag += tr[x].tag;
        if(tr[x].rc) tr[tr[x].rc].tag += tr[x].tag;
        tr[x].tag = 0;
    }
    void modify(int x, int v){
        tr[x].tag -= v;
        pushdown(x);
    }
    int top(int x){
        return tr[x].v;
    }
    void merge(int &x, int y){
        if(!x || !y){
            x |= y;
            return ;
        }
        if(tr[x].v > tr[y].v) std::swap(x, y);
        tr[y].rc = tr[x].lc;
        tr[x].lc = y;
    }
    void merge(int &x){
        pushdown(x), pushdown(tr[x].rc);
        int y = tr[tr[x].rc].rc, z = tr[x].rc;
        tr[tr[x].rc].rc = 0;
        tr[x].rc = 0;
        merge(x, z);
        if(y) merge(y), merge(x, y);
    }
    void pop(int &x){
        x = tr[x].lc;
        merge(x);
    }
    void push(int &x, int v){
        int y = create(v);
        merge(x, y);
    }
    bool empty(int x){
        return !x;
    }
} q;
struct Edge{
    int u, v, w, id;
} a[N];
struct Node{
    int nxt, to, w;
} edge[N];
int head[N], tot;
void add(int u, int v){
    edge[++tot] = {head[u], v, tot};
    head[u] = tot;
}
int sta[N], top, root[N];
bool vis[N], ok[N], has[N];
int fa[N];
int find(int x){
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int u, int v){
    u = find(u), v = find(v);
    if(u ^ v) fa[v] = u;
}
void solve(int x){
    std::priority_queue <pii, std::vector <pii>, std::greater <pii> > q;
    q.push({0, 0});
    a[0] = {x, x, 0};
    std::vector <int> ans;
    while(!q.empty()){
        int u = q.top().second;
        q.pop();
        if(vis[a[u].v]) continue;
        vis[a[u].v] = 1;
        if(a[u].w) ans.emplace_back(a[u].id);
        for(int i = head[a[u].v]; i; i = edge[i].nxt) q.push({edge[i].w, edge[i].to});
    }
    put(ans.size(), 1);
    for(auto y : ans) put(y);
}
int n, m;
bool DMST(int rt){
    for(int i = 1; i <= n; ++i) fa[i] = i;
    ok[rt] = 1;
    for(int i = 1; i <= n; ++i){
        if(ok[find(i)]) continue;
        sta[++top] = i, has[i] = 1;
        while(1){
            int cur = sta[top];
            if(q.empty(root[cur])) return 0;
            int eg = root[cur], w = q.top(root[cur]), nxt = find(a[eg].u);
            q.pop(root[cur]);
            if(nxt == cur) continue;
            add(a[eg].u, eg);
            q.modify(root[cur], w);
            if(ok[nxt]){
                for(int i = 1; i <= top; ++i) ok[sta[i]] = 1;
                break;
            }
            if(has[nxt]) while(sta[top] ^ nxt) merge(nxt, sta[top]), q.merge(root[nxt], root[sta[top--]]);
            else sta[++top] = nxt, has[nxt] = 1;
        }
        top = 0;
    }
    return 1;
}

int main(){
    #ifndef FILE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int rt = 1;
    read(n, m);
    for(int i = 1; i <= m; ++i) read(a[i].u, a[i].v, a[i].w), a[i].id = i, q.push(root[a[i].v], a[i].w);
    if(DMST(rt)) solve(rt);
    else put(-1, 1);
    return 0;
}

// 阿乌拉,投降吧
// ......
// 阿乌拉,站在你面前的是活了上千年的魔法使

如有不对的地方喵,还请通融一下喵,熬夜写的时候脑子笨笨的喵。