这似乎是一道最小树形图输出方案的模版题。
题意
给你一只有 个点 条边的有向图,每条边的边权为 或 。
要求以 为根的最小树形图,并输出最小树形图中所有边权为 的边。无解输出 。
注意到有输入输出文件 (我是绝对不会说我在这上面卡了十分钟,看了讨论区才发现的)。
思路
正如开头所说的,这几乎是一道模版题,所以如果不会最小树形图请先左转至 这题 学习基础的 tarjan 优化朱刘算法,这里就只讲一下方案输出的方法:
在进行 tarjan 的时候,我们每次会将一只环给 暴力 拆开,而拆开时的边就是我们选中的边。
所以我们只需要记录一下每次拆的时候边的编号等信息即可,具体实现可以看代码喵。
Code
// 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;
// 已省略过长的快读喵
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;
}
// 阿乌拉,投降吧
// ......
// 阿乌拉,站在你面前的是活了上千年的魔法使如有不对的地方,还请通融一下,熬夜写的时候脑子笨笨的。