Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth A website problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth A Webste problem solution On a website, there are M users and they want to change their names. You will be given M lines consisting of two strings A and B, where username A wants to change his username to B.
 
But the website does not support multiple users with the same username that is No two users can have the same username at the same time. A user can change his username from his current name to any name (X) if there is no user named X.
 
Every change in the username costs 1 unit of money. Find minimum cost to achieve these changes.
 
 
HackerEarth A website problem solution

 

 

HackerEarth A website problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "n"
#define test ll txtc; cin>>txtc; while(txtc--)
typedef long long int ll;
class dsu{
public:
vector<ll>par;
dsu(ll n){
par.resize(n);
iota(par.begin(),par.end(),0);
}
ll get(ll x){
return (par[x]==x?x:(par[x]=get(par[x])));
}
bool unite(ll x,ll y){
x=get(x);
y=get(y);
if(x!=y){
par[x]=y;
return true;
}
return false;
}
};
int main() {
FIO;
test
{
ll n,m,ans=0,x,y;
cin>>m; n=2*m;
vector<pair<string,string>>vp(m);
map<string,ll>mp;
ll cnt=0;
for(int i=0;i<m;i++){
cin>>vp[i].first>>vp[i].second;
if(mp.find(vp[i].first)==mp.end()){
mp[vp[i].first]=cnt++;
}
if(mp.find(vp[i].second)==mp.end()){
mp[vp[i].second]=cnt++;
}
}
dsu d(n);
for(int i=0;i<m;i++){
x=mp[vp[i].first];
y=mp[vp[i].second];
if(x==y){
continue;
}
ans+=1;
if(!d.unite(x,y)){
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
 

Second solution

t = int(input())
while t > 0:
t -= 1
m = int(input())
names = set()
g = {}
ans = 0
for i in range(m):
a, b = input().split()
ans += a != b
names.update([a, b])
g[a] = b
done = set()
for name in names:
if name in done or name not in g or g[name] == name:
continue
was = name
while name not in done and name in g:
done.add(name)
name = g[name]
ans += was == name
print(ans)
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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