版本比较
Editorial:P13118 [GCJ 2019 #2] Contransmutation
正文
增加 16 行删除 11 行
123456787910911121113141314151616171818191920212122232324252526272829303132333435102 行未改动全部展开**This is an English editorial.**
This article was handwritten with the help of a dictionary. Please forgive any grammar errors.
Upd on 2025.11.24 21:20: Fix some grammar erros and make the expression more formal and rigorous. There are too many low-level errors:(
----
We find that this problem requires calculating values derived recursively from other nodes, which is precisely what DP is designed to solve.We observe that this problem requires calculating values derived recursively from other nodes, which is precisely what DP is designed to solve.
DP on general graphs is extremely challenging, so we consider condensing the original graph into SCC and performing DP on the resulting DAG in topological order.DP on general graphs is often intractable, so we consider condensing the original graph into SCCs and performing DP on the resulting DAG in topological order.
Specifically, let $f(x)$ denote the maximum value of the total weight of metals that you can get which belong to SCC $x$. Specifically, let $f(x)$ denote the maximum attainable total weight of metals within SCC $x$.
If the total weight of metals which belong to SCC $u$ is bigger than $0$, $f(v)\gets -1$. There, $v$ is the immediate successor SCCs of SCC $u$ in the condensation DAG. This is because we can see an SCC as a cycle. Each time we traverse the loop, we add the sum of the weights of all nodes on it to $v$; since we can traverse the loop indefinitely, $f(v)$ becomes infinite. In particular, if SCC $u$ is just a single node, $f(v)\gets f(v)+f(u)$, where $v$ is as defined above.If the total weight of metals in SCC $u$ is positive, we set $f(v)\gets -1$. Here, $v$ refers to any immediate successor of SCC $u$ in the condensation DAG.
But what is the value of $f(u)$ if the total weight of metals which belong to SCC $u$ is bigger than $0$? A classified discussion is required based on the count of vertices $sz$ and edges $bs$ in the SCC:This is because an SCC, by definition, allows any node to reach any other, meaning we can traverse a cycle that includes all nodes. Each time we traverse the cycle, we add the sum of the weights of all nodes on it to $v$; since we can traverse the cycle indefinitely, $f(v)$ becomes infinite. In particular, if SCC $u$ is a single node, we update $f(v) \gets f(v) + f(u)$, where $v$ is defined as above. Note that if $f(u) = -1$, we must also set $f(v) = -1$, as the infinite value propagates to $v$.
- If $sz=bs$, which means the SCC is a cycle, apparently $f(u)$ is equal to the total weight of metals which belong to SCC $u$.- If $sz<bs$, each time we traverse any cycle within the SCC, the sum of the weights from the nodes in that cycle is added to the weight of any node reachable via a non-cycle edge. Since this traversal can be repeated indefinitely, the infinite value propagates to the entire SCC.But what is the value of $f(u)$ if the total weight of metals in SCC $u$ is positive? We analyze different cases based on the count of vertices $sz$ and edges $bs$ in the SCC:
$sz$ and $bs$ are easy to calculate,the value $f$ of $u$ is also easy to calculate when the total weight of metals which belong to SCC $u$ is $0$, so this problem is solved perfectly.- Case 1: $sz=bs$. This indicates the SCC is a simple cycle. In this case, $f(u)$ is equal to the total weight of metals in SCC $u$.- Case 2: $sz<bs$. This means the SCC contains at least one simple cycle and at least one edge that is not part of that cycle. Each time we traverse a cycle, the total weight of the cycle is immediately added via a non-cycle edge to an adjacent node. Since this process can be repeated indefinitely, the value of the adjacent node becomes infinite. This infinite value can then propagate throughout the SCC via other edges, so the value for the entire SCC becomes infinite. Thus, we set $f(u) \gets -1$.
Note that we cannot rely solely on whether $f(u) > 0$ to determine if the total weight in this SCC is positive, because $f(u)$ is the result after a modulo operation. It is possible that the actual total weight is a multiple of the modulus, making $f(u)$ zero. The parameters $sz$ and $bs$ are straightforward to calculate. Moreover, the value $f$ of $u$ is also easy to calculate when the total weight of metals which belong to SCC $u$ is $0$.
We need to maintain a boolean value $ps(u)$ indicating whether the total weight in SCC $u$ is positive. This value is updated along with u as we process the topological order, and the update logic is straightforward.Combining these cases, the problem can be efficiently solved.
Note that we cannot rely solely on whether $f(u) > 0$ to determine if the total weight in this SCC is positive, because $f(u)$ is the result after the modulo operation. It is possible that the actual total weight is a multiple of the modulus, making $f(u)$ zero.
We need to maintain a boolean value $ps(u)$ indicating whether the total weight in SCC $u$ is positive. This value is updated along with $u$ as we process the topological order, and the update logic is straightforward.
```cpp#include <bits/stdc++.h>#define int long long#define endl '\n'102 行未改动全部展开