94B - Friends - CodeForces Solution


graphs implementation math *1300

Please click on ads to support us..

Python Code:

m = int(input())
l = [0]*5
for i in range(m):
    a, b = map(int, input().split())
    l[a-1] += 1
    l[b-1] += 1
if l.count(2) == 5:
    print('FAIL')
else:
    print('WIN')

C++ Code:

#include <bits/stdc++.h>
using namespace std;
bool bsf(vector<int> adj[],int vis[],int n,int p)
{
    queue<pair<pair<int,int>,int>> q;
    q.push({{n,p},0});
    unordered_map<int,int> m;
    while(!q.empty())
    {
        int n=q.front().first.first;
        int p=q.front().first.second;
        int l=q.front().second;
        q.pop();
        for(auto it:adj[n])
        {
            if(!vis[it])
            {
                q.push({{it,n},l+1});
                vis[it]=1;
            }
            else
            {
                if(it!=p&&l==1&&m[it]==1)
                return true;
            }
        }
        m[n]=1;

    }
return false;

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vector<int> adj[6];
    vector<int> nonadj[6];
    for (int i = 1; i < 6; i++)
    {
        for (int j = 1; j < 6; j++)
        {
            if(i!=j)
            nonadj[i].push_back(j);
        }       
        
    }
    int m;
    cin>>m;
    for (int i = 0; i < m; i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        auto pos=find(nonadj[u].begin(),nonadj[u].end(),v);
    nonadj[u].erase(pos);
    auto pos2=find(nonadj[v].begin(),nonadj[v].end(),u);
    nonadj[v].erase(pos2);
        
    }
    // for(int i=1;i<6;i++)
    // {
    //     for(auto it:nonadj[i])
    //     cout<<it<<" ";
    //     cout<<endl;
    // }
    

   for(int i=1;i<6;i++)
   {
    // if(!vis[i])
    {
        int vis[6]={0};
        vis[i]=1;
        if(bsf(adj,vis,i,-1))
        {
            cout<<"WIN"<<endl;
            return 0;
        }
    }
   }


   for(int i=1;i<6;i++)
   {
    // if(!vis2[i])
    {
        int vis2[6]={0};
        vis2[i]=1;
        if(bsf(nonadj,vis2,i,-1))
        {
            cout<<"WIN"<<endl;
            return 0;
        }
    }
   }
   cout<<"FAIL"<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain