Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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

HackerEarth 3 types problem solution

YASH PAL, 31 July 2024
In this HackerEarth 3 types problem solution Let’s consider some weird country with N cities and M bidirectional roads of 3 types. It’s weird because of some unusual rules about using these roads: men can use roads of types 1 and 3 only and women can use roads of types 2 and 3 only. Please answer the following very interesting question: what is a maximum number of roads it’s possible to destroy that the country will be still connected for both men and women? A connected country is a country where it’s possible to travel from any city to any other using existing roads.
HackerEarth 3 types problem solution

HackerEarth 3 types of problem solution.

#include <bits/stdc++.h>
using namespace std;
int n,m;
int a,b,c,ans;
int w[1<<20];
int get(int x)
{
if (w[x]==x)
return x;
return w[x]=get(w[x]);
}
void merge(int a,int b)
{
w[a]=b;
}
int W[1<<20][3];
int get1(int x,int y)
{
if (W[x][y]==x)
return x;
return get1(W[x][y],y);
}
void merge1(int a,int b,int c)
{
W[a][c]=b;
}
int main(){
ios_base::sync_with_stdio(0);
//cin.tie(0);
cin>>n>>m;
for (int i=1;i<=n;i++)
w[i]=W[i][1]=W[i][2]=i;

for (int i=1;i<=m;i++)
{
cin>>a>>b>>c;

for (int j=1;j<=2;j++)
if (c&j)
{
int ta,tb;
ta=get1(a,j);
tb=get1(b,j);
if (ta==tb)
continue;
merge1(ta,tb,j);
}

if (c!=3)
continue;
a=get(a);
b=get(b);
if (a==b)
continue;
merge(a,b);
++ans;
}
ans=(n-1-ans)*2+ans;
int cnt=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=2;j++)
if (W[i][j]==i)
++cnt;

if (cnt>2)
{
cout<<-1<<endl;
return 0;
}
cout<<m-ans<<endl;
return 0;}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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