|
U
Pro343. Directed Graph, Determine Whether It Is
Med.
Problem Description
Given a directed graph, determine whether it is a DAG.
Input Format
- •Line 1: Two space-separated integers V and E (1≤V≤105, 0≤E≤2×105), representing vertices and edges.
- •Next E lines: Two space-separated integers u and v (1-based indices) indicating an edge between u and v.
Output Format
Print the computed metric or traversal sequence space-separated.
Example 1:
Input:
4 4 1 2 2 3 3 4 4 1
Output:
1 2 3 4
Explanation: Standard cycle graph traversal over 4 vertices.
Example 2:
Input:
3 2 1 2 2 3
Output:
1 2 3
Explanation: Linear path through 3 vertices.
Example 3:
Input:
1 0
Output:
1
Explanation: Isolated single vertex with 0 edges.
Constraints:
1 <= V <= 10^50 <= E <= 2 * 10^5Time complexity target: O(V + E)Auxiliary space target: O(V + E)
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
param_3 =
param_4 =
param_5 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit