I can't figure out which test cases are failing in this approach for the problem given.

Revision en1, by N00bCode, 2023-01-20 13:18:57

Problem: AtCoder

code:

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

int main()
{
    int n;
    cin>>n;
    map<string,string> inp;
    map<string, int> vis,ininp;
    for(int i=0;i<n;i++){
        string a,b;
        cin>>a>>b;
        inp[a]=b;
        ininp[a]++;
    }
    string ans="Yes";
    for(auto e: inp){
        if(vis[e.first]) continue;
        vis[e.first]++;
        string temp=inp[e.first];
        while(ininp[temp]){
            if(vis[temp]) {
                ans="No";
                break;
            }
            else vis[temp]++;
            temp=inp[temp];
        }
    }
    cout<<ans;
    return 0;
}
Tags graphs, dfs, hashmap, c++, algorithms, adhoc

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English N00bCode 2023-01-20 13:18:57 830 Initial revision (published)