E - Pursuit For Artifacts

本文介绍了一种使用Tarjan算法进行图的缩点处理的方法,并通过深度优先搜索(DFS)来解决特定问题。文章提供了完整的代码实现,展示了如何通过Tarjan算法找到图中的双连通分量,并在此基础上重构图进行进一步处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要是利用tarjan缩点,然后进行dfs

这篇代码是转载的,转载的地址-> http://blog.youkuaiyun.com/huanghongxun/

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define clr(i,j) memset(i,j,sizeof(i))
#define FOR(i,j,k) for(int i=j;i<=k;++i)

const int N = 300005, M = 2 * N;

struct Graph {
    int head[N], forword[M], from[M], to[M], cost[M], val[N], dis[N], vis[N], cnt, id, n;
    int dfn[N], low[N], instack[N], stack[N], belong[N], bcc, top;

    Graph() : cnt(0) {}

    void add(int a, int b, int c) {
        forword[++cnt] = head[a]; from[cnt] = a; to[cnt] = b; cost[cnt] = c; head[a] = cnt;
        forword[++cnt] = head[b]; from[cnt] = b; to[cnt] = a; cost[cnt] = c; head[b] = cnt;
    }

    void tarjan(int u, int fa) {
        int i;
        dfn[u] = low[u] = ++id;
        instack[u] = 1; stack[++top] = u;
        for (i = head[u]; i; i = forword[i]) {
            if (to[i] == fa) continue;
            if (!dfn[to[i]]) {
                tarjan(to[i], u);
                low[u] = min(low[to[i]], low[u]);
            } else if (instack[to[i]])
                low[u] = min(dfn[to[i]], low[u]);
        }

        if (dfn[u] == low[u]) {
            ++bcc;
            do {
                i = stack[top--];
                belong[i] = bcc;
                instack[i] = 0;
            } while (i != u);
        }
    }

    void do_bcc() {
        id = 0; top = 0; bcc = 0;
        FOR(i,1,n) if (!dfn[i]) tarjan(i, 0);
    }

    void rebuild(Graph &out) {
        for (int i = 1; i <= cnt; i += 2)
            if (belong[from[i]] != belong[to[i]])
                out.add(belong[from[i]], belong[to[i]], cost[i]);
            else if (cost[i])
                out.val[belong[from[i]]] = 1;
    }

    bool calc(int x, int y) {
        queue<int> q; q.push(x);
        dis[x] = val[x]; vis[x] = 1;
        while (!q.empty()) {
            int u = q.front(), i; q.pop();
            for (i = head[u]; i; i = forword[i]) if (!vis[to[i]]) {
                if (dis[to[i]] < dis[u] + cost[i] + val[to[i]])
                    dis[to[i]] = dis[u] + cost[i] + val[to[i]];
                q.push(to[i]); vis[to[i]] = 1;
            }
        }
        return dis[y];
    }
} base, ne;

int main() {
    int n, m, a, b, c;
    scanf("%d%d", &n, &m);
    base.n = n;
    FOR(i,1,m) {
        scanf("%d%d%d", &a, &b, &c);
        base.add(a, b, c);
    }
    scanf("%d%d", &a, &b);
    base.do_bcc(); base.rebuild(ne);
    puts(ne.calc(base.belong[a], base.belong[b]) ? "YES" : "NO");
    return 0;
}
### 原始对偶追踪算法解释及其优化背景 原始对偶追踪(Primal-Dual Pursuit, PD-Pursuit)是一种用于解决凸优化问题的方法,在稀疏信号恢复等领域有广泛应用。该方法的核心在于通过交替更新原变量和对偶变量来逼近最优解。 #### 算法基本原理 在优化上下文中,PD-Pursuit 的目标是最小化如下形式的目标函数: \[ \min_x f(x) \quad \text{subject to} \quad Ax = b, \] 其中 \(f(x)\) 是一个凸函数,\(A\) 和 \(b\) 定义了线性约束条件[^1]。为了求解这一问题,可以引入拉格朗日乘子并构建增广拉格朗日函数: \[ L(x, y; \rho) = f(x) + y^T (Ax - b) + \frac{\rho}{2}\|Ax - b\|^2_2. \] 在此基础上,PD-Pursuit 采用迭代方式分别更新原变量 \(x\) 和对偶变量 \(y\)。具体而言,每次迭代分为两步:先固定对偶变量 \(y\) 更新原变量 \(x\);再基于新的 \(x\) 来调整对偶变量 \(y\)【^2】。 #### 实现细节 以下是 Python 中的一个简单实现框架,展示了如何利用梯度下降等技术完成上述过程: ```python import numpy as np def primal_dual_pursuit(A, b, rho=1e-3, max_iter=1000, tol=1e-6): m, n = A.shape x = np.zeros(n) y = np.zeros(m) for _ in range(max_iter): # Update the primal variable x using gradient descent on L wrt x grad_f = compute_gradient_of_f(x) # Placeholder function for computing ∇f(x) residual = A.T @ y + rho * A.T @ (A @ x - b) + grad_f x_new = prox_operator(x - 1/rho * residual) # Prox operator of f # Check convergence condition based on duality gap or norm difference if np.linalg.norm(x_new - x) < tol: break x = x_new # Update dual variable y via ascent step proportional to constraint violation y += rho * (A @ x - b) return x, y # Example usage with placeholder functions and matrices if __name__ == "__main__": A = np.random.rand(5, 10) b = np.random.rand(5,) result_x, result_y = primal_dual_pursuit(A, b) print("Optimized Primal Variable:", result_x) ``` 此代码片段定义了一个通用版本的 PD-Pursuit 方法,并假设存在计算目标函数梯度以及其近似算符所需的辅助功能 `compute_gradient_of_f` 和 `prox_operator` 【^3】。 #### 进一步说明 需要注意的是,实际应用中的性能取决于多个因素,比如矩阵 \(A\) 是否具有特殊结构、初始猜测的选择、参数调节策略等等。因此,在特定场景下可能还需要进一步定制化设计或改进标准流程。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值