Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth One and only flow problem solution

YASH PAL, 31 July 2024
In this HackerEarth One and only flow problem solution You are given a network of directed graph of N vertices and M edges, where every edges’ capacity is 1. An ordered pair (u,v),u!=v is good, if the maxflow is exactly 1 upon setting u as the source vertex and v as the sink vertex of the flow network. Find out if all of the ordered pairs (u,v),u!=v are good or not.
HackerEarth One and only flow problem solution

HackerEarth One and only flow problem solution.

#include  <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
int n, m, tot[maxn];
bool vis[maxn], onstk[maxn], ans;
vector<int> g[maxn];

void dfs(int u) {
vis[u] = onstk[u] = 1;
for(int v : g[u]) {
if(vis[v]) {
if(!onstk[v]) {
ans = 0;
} else {
tot[v]--;
tot[u]++;
}
} else {
dfs(v);
tot[u] += tot[v];
}
}
if(u != 1 and tot[u] != 1) {
ans = 0;
}
onstk[u] = 0;
}

int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
g[i].clear();
for(int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
if(u != v) {
g[u].emplace_back(v);
}
}
fill(vis, vis + n + 1, 0);
fill(tot, tot + n + 1, 0);
ans = 1;
dfs(1);
int cnt = 0;
for(int i = 1; i <= n; i++)
cnt += vis[i];
if(cnt != n) {
ans = 0;
}
if(ans) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes