CF_D. Choosing Capital for Treeland_树形DP

本文介绍了一种基于树形结构的动态规划算法,用于解决树中有向边的问题,旨在找到最小化边翻转次数的方案。通过两次深度优先搜索(DFS),算法能够有效地更新每个节点所需的翻转数量。

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define INF 0x3f3f3f3f
#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep_0(i, n) for (int i = n - 1; i >= 0; i--)
#define rep_1(i, n) for (int i = n; i > 0; i--)
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define mem(x, y) memset(x, y, sizeof(x))
/**
题目大意
树中的边为有向边 求其中的节点能到达树上的其他各节点而
需要翻转的有向边的数量最少
思路
第一次dfs 由子节点更新父亲节点 在子树内需要反转的边数
再由父节点跟新子节点的dp 如果父子之间的边指向父亲 
则 dp[i] = dp[fa] - 1;
else
    dp[i] = dp[fa] + 1;
*/

using namespace std;
const int MAXN = 2e5 + 10;
struct Edge
{
    int from, to;
    Edge(int f, int t): from(f), to(t) {}
};
vector<Edge> edges;
int dp[MAXN];
vector<int> g[MAXN];
void addEdge(int from, int to)
{
    edges.push_back(Edge(from, to));
    int sz = edges.size();
    g[from].push_back(sz - 1);
    g[to].push_back(sz - 1);


}
void dfs(int u, int fa)
{

    for (int i = 0; i < g[u].size(); i++)
    {
        Edge e = edges[g[u][i]];

        if (e.from == fa || e.to == fa)
            continue;
        if (e.to == u)
        {
            dfs(e.from, u);

            dp[u] += dp[e.from] + 1;


        }
        else
        {
            dfs(e.to, u);
            dp[u] += dp[e.to];
        }


    }
}
int ans = INF;
void dfs1(int u, int fa)
{
    for (int i = 0; i < g[u].size(); i++)
    {
        Edge e = edges[g[u][i]];
        if (e.from == fa || e.to == fa)
            continue;
        if (e.to == u)
        {
            dp[e.from] = dp[u] - 1;
            ans = MIN(ans, dp[e.from]);
            dfs1(e.from, u);
        }
        else
        {

            dp[e.to] = dp[u] + 1;
            ans = MIN(ans, dp[e.to]);
            dfs1(e.to, u);
        }

    }
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE;
    int n, s, t;
    scanf("%d", &n);
    mem(dp, 0);
    rep0(i, n - 1)
    {
        scanf("%d %d", &s, &t);
        addEdge(s, t);
    }
    dfs(1, -1);
    ans = MIN(ans, dp[1]);
    dfs1(1, -1);
   
    printf("%d\n", ans);
    int i = 1;
    while (dp[i] != ans)
        i++;
    printf("%d", i);

    for (i++; i <= n; i++)
        if (dp[i] == ans)
            printf(" %d", i);
    printf("\n");

    return 0;
}

1. Compilation 1.1 Windows A workspace for MS Visual Studio is provided with the name "hpm_vsXXXX.sln" in the directory "build\x86_windows". It contains the encoder and decoder projects. 1.2 Unix/Linux Makefiles are provided in the directory "build\x86_linux". 'make' command will create the obj files and generate the executable file in the 'bin' directory. 1.3 CMake for HPM Example for Windows: mkdir build_cmake cd build_cmake cmake .. -G open "HPM.sln" in build_cmake directory to generate the executable files to "build_cmake/app/Debug" or "build_cmake/app/Release" directory. Example for Linux: mkdir build_cmake cd build_cmake cmake .. make -j 'make' command will generate the executable file to the 'build_cmake/app' directory. ******************************************************************* 2. Command line parameters 2.1 Encoder encoder_app [--config file] [-paramShort ParameterValue] [--paramLong ParameterValue] --config file All Parameters are initially taken from the 'file', typically: "encode_RA.cfg". -paramShort ParameterValue --paramLong ParameterValue If -paramShort or --paramLong parameters are present, then the ParameterValue will override the default settings in the configuration file. 2.2 Decoder decoder_app [-paramShort ParameterValue] [--paramLong ParameterValue] All decoding parameters are set by the command lines. ******************************************************************* 3. Examples of command line For SDR, using cfg\encode_RA.cfg For PG, using cfg\HDR\encode_RA_PG.cfg For HLG, using cfg\HDR\encode_RA_HLG.cfg 3.1 Random Access build\x86_windows\x64\Release\encoder_app --config cfg\encode_RA.cfg -i City_1280x720_60.yuv -w 1280 -h 720 -z 60 -p 64 -f 9 -d 8 -q 45 -o City_RA.bin -r City_RA_rec.yuv build\x86_windows\x64\Release\decoder_app -s -i City_RA.bin -o City_RA_dec.yuv 3.2 All Intra build\x86_windows\x64\Release\encoder_app --config cfg\encode_AI.cfg -i City_1280x720_60.yuv -w 1280 -h 720 -z 60 -f 9 -d 8 -q 45 -o City_AI.bin -r City_AI_rec.yuv build\x86_windows\x64\Release\decoder_app -s -i City_AI.bin -o City_AI_dec.yuv 3.3 Low Delay build\x86_windows\x64\Release\encoder_app --config cfg\encode_LD.cfg -i City_1280x720_60.yuv -w 1280 -h 720 -z 60 -f 9 -d 8 -q 45 -o City_LD.bin -r City_LD_rec.yuv build\x86_windows\x64\Release\decoder_app -s -i City_LD.bin -o City_LD_dec.yuv ******************************************************************* 4. Configuration files The default configuration files are provided in the directory "cfg". These contain explanatory comments for each parameter. If the parameter name is undefined, the program will be terminated with an error message. *******************************************************************
最新发布
07-20
### Compilation of Encoder/Decoder Applications on Different Platforms Encoder/decoder applications, such as those for video codecs like VP8/VP9 and H.264, require platform-specific compilation strategies depending on whether the target system is Windows, Linux, or Unix-based. The compilation process involves choosing the right build system (e.g., Visual Studio, Makefiles, or CMake), configuring build parameters, and handling platform-specific dependencies. #### Windows Platform On Windows, developers often use **Visual Studio** for compiling encoder/decoder applications. This approach provides an integrated development environment (IDE) with debugging support and project configuration tools. For instance, when building a codec library like x264 or VPX (VP8/VP9), the source code can be imported into a Visual Studio project where compiler flags, include paths, and linker settings are configured via the project properties. Visual Studio supports multiple configurations (e.g., Debug and Release) and allows specifying platform toolsets (e.g., v142 for VS 2019). Additionally, MSBuild command-line tools can be used to automate builds outside the IDE. #### Unix/Linux Platforms On Unix/Linux systems, **Makefiles** are commonly used for building encoder/decoder applications. These files define the compilation rules, dependencies, and linking steps required to generate the final binaries. The `make` utility processes the Makefile and executes the necessary commands to build the application. For example, to compile the x264 encoder, one might run `./configure` followed by `make` and `make install` to generate and install the library and associated binaries. If the system's package manager offers a sufficiently recent version of the library (e.g., `libx264-dev` version ≥ 118), it may be preferable to install it directly instead of compiling from source. #### Cross-Platform Builds Using CMake For projects that need to support multiple platforms, **CMake** is a popular choice. It generates native build files (e.g., Makefiles for Unix, Visual Studio project files for Windows) based on a set of configuration files (`CMakeLists.txt`). This abstraction allows developers to maintain a single build configuration that can be used across different operating systems. CMake supports advanced features such as conditional compilation, dependency checking, and custom build steps. Command-line parameters can be passed to CMake during configuration to control build options, such as enabling or disabling specific encoders or specifying installation paths. #### Configuration Files and Command-Line Parameters Many encoder/decoder applications use configuration files to define encoding presets, bitrates, resolution settings, and other parameters. These files can be included in the build process or loaded at runtime. Additionally, command-line parameters allow users to override default settings when invoking the encoder or decoder. For example, in VPX tools, options like `--bitrate`, `--fps`, and `--width` can be specified directly in the command line to customize the encoding process. ### Example: Building VP8/VP9 Encoder Using CMake ```bash # Clone the VPX repository git clone https://git.ffmpeg.org/ffmpeg.git cd ffmpeg # Create a build directory and navigate into it mkdir build && cd build # Configure the build using CMake with specific options cmake .. -DENABLE_VP8=ON -DENABLE_VP9=ON -DCONFIG_SHARED=ON # Compile the project make -j$(nproc) # Install the binaries and libraries sudo make install ``` This workflow demonstrates how CMake abstracts platform-specific build logic and enables developers to compile the VP8/VP9 encoder on different operating systems with minimal configuration changes. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值