#P11676. [USACO25JAN] DFS Order P
[USACO25JAN] DFS Order P
Description
Bessie has a simple undirected graph with vertices labeled (). She generates a depth-first search (DFS) order of the graph by calling the function , defined by the following C++ code. Each adjacency list ( for all ) may be permuted arbitrarily before starting the depth first search, so a graph can have multiple possible DFS orders.
vector<bool> vis(N + 1);
vector<vector<int>> adj(N + 1); // adjacency list
vector<int> dfs_order;
void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
dfs_order.push_back(x);
for (int y : adj[x]) dfs(y);
}
You are given the initial state of the graph as well as the cost to change the state of each edge. Specifically, for every pair of vertices satisfying , you are given an integer () such that
- If , edge is not currently in the graph, and can be added for cost .
- If , edge is currently in the graph, and can be removed for cost .
Determine the minimum total cost to change the graph so that is a possible DFS ordering.
Input Format
The first line contains .
Then lines follow. The th line contains separated by spaces.
Output Format
The minimum cost to change the graph so that is a possible DFS ordering.
4
1
2 3
40 6 11
10
5
-1
10 -2
10 -7 10
-6 -4 -5 10
5
4
-1
-2 300
4 -5 6
9
Hint
For Sample 1:
Initially, the graph contains no edges. can be added for a total cost of . The graph now has two possible DFS orderings: .
For Sample 2:
Initially, the graph contains edges . Edge can be removed for a cost of .
For Sample 3:
Initially, the graph contains edges . Edge can be removed and edge can be added for a total cost of .
SCORING:
- Inputs 4-9: All
- Inputs 10-16:
- Inputs 17-23: No additional constraints.
京公网安备 11011102002149号