CSES Solutions - Even Outdegree Edges
Last Updated :
15 Jul, 2024
Given an undirected graph, your task is to choose a direction for each edge so that in the resulting directed graph's each node has an even outdegree. The outdegree of a node is the number of edges coming out of that node.
Examples:
Input: N = 4, M = 4, Edges = {{1, 3}, {3, 2}, {3, 4}, {1, 4}}
Output:
3 1
3 2
4 3
4 1
Explanation: Each node in the resulting directed graph has an even outdegree: node 1 has outdegree 0, node 2 has outdegree 0, node 3 has outdegree 0, and node 4 has outdegree 2.
Input: N = 3, M = 2, Edges = {{1, 3}, {3, 2}}
Output:
3 1
3 2
Explanation: Each node in the resulting directed graph has an even outdegree: node 1 has outdegree 0, node 2 has outdegree 2, node 3 has outdegree 0.
Approach: To solve the problem, follow the below idea:
The problem can be solved using Depth-First Search. We use an even array to keep track of nodes with even outdegrees during the traversal. If a node has an even outdegree, the edge is directed from the parent to the child. otherwise, the edge is directed from the child to the parent. This process continues until all nodes are visited. After the traversal, the algorithm checks if all nodes have an even outdegree. If they do, it prints the directed edges. otherwise, it prints "IMPOSSIBLE."
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxN = 1e5 + 5, maxM = 2e5 + 5;
int N, M, timer, in[maxN];
bool even[maxN];
vector<pii> G[maxN];
pii ans[maxM];
// Depth-first search (DFS) function to traverse the graph
void dfs(int u, int p = -1)
{
// Mark the current node with a visitation timestamp
in[u] = ++timer;
// Iterate over all adjacent nodes
for (pii e : G[u]) {
int v = e.first, id = e.second;
// Skip the parent node to avoid traversing back
if (v != p) {
// If the adjacent node has not been visited
if (!in[v]) {
// Recursively perform DFS on the adjacent
// node
dfs(v, u);
// If the adjacent node has an even
// outdegree
if (even[v]) {
// Direct the edge from the current
// node to the adjacent node
ans[id] = { u, v };
// Toggle the outdegree of the current
// node
even[u] ^= true;
}
else {
// If the adjacent node has an odd
// outdegree Direct the edge from the
// adjacent node to the current node
ans[id] = { v, u };
// Toggle the outdegree of the adjacent
// node
even[v] ^= true;
}
}
else if (in[v] < in[u]) {
// If the adjacent node was visited earlier
// Toggle the outdegree of the current node
even[u] ^= true;
// Direct the edge from the current node to
// the adjacent node
ans[id] = { u, v };
}
}
}
}
int main()
{
// Predefined input
N = 4;
M = 4;
// List of edges
vector<pair<int, int> > edges
= { { 1, 3 }, { 3, 2 }, { 3, 4 }, { 1, 4 } };
// Construct the graph from the predefined edges
for (int i = 0; i < M; i++) {
cin >> edges[i].first >> edges[i].second;
int a = edges[i].first, b = edges[i].second;
// Add edge to adjacency list for node a
G[a].push_back({ b, i });
// Add edge to adjacency list for
// node b (undirected graph)
G[b].push_back({ a, i });
}
// Initialize all nodes to have even outdegrees
fill(even + 1, even + N + 1, true);
// Perform DFS traversal on all unvisited nodes
for (int i = 1; i <= N; i++) {
if (!in[i]) {
dfs(i);
}
}
// Check if all nodes have even outdegrees
bool good = true;
for (int i = 1; i <= N; i++) {
good &= even[i];
}
// Print the result
if (good) {
for (int i = 0; i < M; i++) {
// Print directed edges
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
else {
// Print "IMPOSSIBLE" if any node has an odd
// outdegree
printf("IMPOSSIBLE\n");
}
return 0;
}
Time Complexity: O(V+E), where V is the number of nodes and E is the number of edges
Auxiliary Space: O(V+E) due to the storage required for the adjacency list and other arrays.
Similar Reads
CSES Solutions - Acyclic Graph Edges Given an undirected graph, the task is to choose a direction for each edge so that the resulting directed graph is acyclic. You can print any valid solution. Example:Input: n = 3, m = 3, edge = {{1, 2}, {2, 3}, {3, 1}}Output: 1 22 31 3Explanation: Connecting the graph in this manner will result in d
2 min read
CSES Solutions - Point in Polygon You are given a polygon of N vertices and a list of M points. Your task is to determine for each point if it is inside, outside or on the boundary of the polygon. The polygon consists of n vertices (x1,y1),(x2,y2),...,(xn,yn). The vertices (xi,yi) and (xi+1,yi+1) are adjacent for i=1,2,...,n-1, and
10 min read
CSES Solutions - Polygon Lattice Points Given a polygon, your task is to calculate the number of lattice points inside the polygon and on its boundary. A lattice point is a point whose coordinates are integers. The polygon consists of n vertices (x1,y1),(x2,y2),....,(xn,yn). The vertices (xi,yi) and (xi+1,yi+1) are adjacent for i=1,2,....
9 min read
CSES Solutions - Permutations A permutation of integers 1,2 ... N is called beautiful if there are no adjacent elements whose difference is 1. Given N, construct a beautiful permutation if such a permutation exists. If there are no solutions, print "NO SOLUTION".Examples:Input: N = 5Output: 4 2 5 3 1Explanation: No two adjacent
6 min read
CSES Solutions - Tree Matching You are given a tree consisting of n nodes. A matching is a set of edges where each node is an endpoint of at most one edge. What is the maximum number of edges in a matching ? Examples: Input : N = 10 , edges = {{5,8}, {4,6}, {9,1}, {10,4}, {1,3}, {2,3}, {7,9}, {6,2}, {5,10}}Output : 5 Explanation
9 min read