114B - PFAST Inc - CodeForces Solution


bitmasks brute force graphs *1500

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
nodes = sorted([input() for _ in range(n)])
edges = {tuple(input().split()) for _ in range(m)}


n = len(nodes)
total_combinations = 2**n
team = []
for i in range(total_combinations):
    combination = []
    for j in range(n):
        if (i >> j) & 1:
            combination.append(nodes[j])
        
    conflict = False
    for a,b in edges:
        if a in combination and b in combination:
            conflict = True
            break
            
    if not conflict and len(team) < len(combination):
        team = combination
        
print(len(team))
for i in team:
    print(i)

C++ Code:

#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#define show(C) for (auto J: C) cout << J << " "; cout << endl;
#define ll long long
#define F(I, S, E) for(ll I = S; I < E; I++)
#define input(a,n) for(ll I = 0; I < n; I++){cin>>a[I];}
#define PYES cout << "YES\n"
#define PNO cout << "NO\n"
#define vi vector<ll>
#define pi pair<ll,ll>
#define f first
#define s second
#define pb push_back
using namespace std;
void solve()
{
    int n,m;cin>>n>>m;
    vector<string> person(n);
    vector<int> inc(n,0);
    map<string,int> mpp;
    for(int i=0;i<n;i++){cin>>person[i];mpp[person[i]]=i;}
    vector<vector<int> > adj(n);
    for(int i=0;i<m;i++)
    {
        string a,b;
        cin>>a>>b;
        adj[mpp[a]].push_back(mpp[b]);
        adj[mpp[b]].push_back(mpp[a]);
    }
    int len=0;
    for(int mask=0;mask<(1<<n);mask++)
    {
        // each of 2 to power n subset
        bool can=true;
        set<int> enemy;
        vector<int> temp(n,0);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(mask & (1<<i))
            {
                temp[i]=1;cnt++;
                if(enemy.find(i)!=enemy.end()){can=false;break;}
                else
                {
                    for(auto it:adj[i])enemy.insert(it);
                }
            }
        }
        if(can)
        {
            if(cnt>len)
            {
                len=cnt;
                for(int i=0;i<n;i++)inc[i]=temp[i];
            }
        }
    }
    cout<<len<<endl;
    set<string> res;
    for(int i=0;i<n;i++)
    {
        if(inc[i])res.insert(person[i]);
    }
    for(auto it:res)cout<<it<<endl;


}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
 
    return 0;
}


Comments

Submit
0 Comments
More Questions

112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie