寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #C - Bowls and Dishes

AtCoder Beginner Contestの過去問から、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#C - Bowls and Dishes

atcoder.jp

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#C - Bowls and Dishes
https://atcoder.jp/contests/abc190/tasks/abc190_c
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
int main(void){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int>> ab(m);
  for(auto& [a,b] : ab){
    cin>>a>>b;
  }
  int k;
  cin>>k;
  vector<pair<int,int>> cd(k);
  for(auto& [c,d] : cd){
    cin>>c>>d;
  }
  int ans=0;
  for(int bit=0;bit<(1<<k);bit++){
    vector<bool> tf(n);
    for(int i=0;i<k;i++){
      const auto [c,d]=cd[i];
      tf[((bit & (1<<i)) ? c : d)]=1;
    }
    int cnt=0;
    for(auto [a,b] : ab){
      if(tf[a] && tf[b]){
        cnt++;
      }
    }
    if(ans<cnt){
      ans=cnt;
    }
  }
  cout<<ans<<endl;
  return 0;
}

AtCoder Beginner Contestは、オンラインジャッジによるプログラミングコンテストです。
日本語と英語に対応していて、週末ごとに実施されているみたいです。
https://practice.contest.atcoder.jp/tutorial
アカウントを登録すれば、誰でも参加できます。