RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0648

DFS Order

Diamond V 다이아몬드 V
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

Bessie has a simple undirected graph with vertices labeled \(1\dots N\)
(\(2\le N\le 750\)). She generates a depth-first search (DFS) order of the graph
by calling the function \(\texttt{dfs}(1)\), defined by the following C++ code.
Each adjacency list (\(\texttt{adj}[i]\) for all \(1\le i\le N\)) 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 \((i,j)\) satisfying
\(1\le i, you are given an integer \(a_{i,j}\) (\(0<|a_{i,j}|\le 1000\)) such
that

  • If \(a_{i,j}>0\), edge \((i,j)\) is not currently in the graph, and can be added for cost \(a_{i,j}\).
  • If \(a_{i,j}<0\), edge \((i,j)\) is currently in the graph, and can be removed for cost \(-a_{i,j}\).

Determine the minimum total cost to change the graph so that \([1,2\dots,N]\) is a
possible DFS ordering.

Problem credits: Benjamin Qi

제약

SCORING

  • Inputs 4-9: All \(a_{i,j}>0\)
  • Inputs 10-16: \(N\le 50\)
  • Inputs 17-23: No additional constraints.

Problem credits: Benjamin Qi

입력 형식

The first line contains \(N\).

Then \(N-1\) lines follow. The \(j-1\)th line contains
\(a_{1,j}, a_{2,j}, \dots, a_{j-1,j}\) separated by spaces.

출력 형식

The minimum cost to change the graph so that \([1,2,\dots, N]\) is a possible DFS
ordering.

예제 1
입력
4
1
2 3
40 6 11
출력
10
설명

Initially, the graph contains no edges. \((1,2),(2,3),(2,4)\) can be added for a
total cost of \(1+3+6\). The graph now has two possible DFS orderings:
\([1,2,3,4],[1,2,4,3]\).

예제 2
입력
5
-1
10 -2
10 -7 10
-6 -4 -5 10
출력
5
설명

Initially, the graph contains edges \((1,2),(2,3),(2,4),(1,5),(2,5),(3,5)\). Edge
\((3,5)\) can be removed for a cost of \(5\).

예제 3
입력
4
-1
-2 300
4 -5 6
출력
9
설명

Initially, the graph contains edges \((1,2),(1,3),(2,4)\). Edge \((2,4)\) can be
removed and edge \((1,4)\) can be added for a total cost of \(5+4=9\).

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2024-2025 > January > Platinum

태그

평가 및 의견

DFS Order

개요
출제자 난이도 Diamond V 다이아몬드 V 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

DFS Order

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8