Official
C - Bowls and Dishes Editorial
by
C - Bowls and Dishes Editorial
by
tatyam
\(K ≤ 16\) と \(K\) の制約が非常に小さいことに注目すると、それぞれの人がどちらの皿にボールを置くかの \(2^K\) 通りを全探索できそうです。
それぞれの人がどちらの皿にボールを置くかを固定したとき、満たされる条件の個数は \(O(N+M+K)\) で求めることができるので、時間計算量 \(O(2^K(N+M+K))\) でこの問題を解くことができます。
回答例 (C++)
#include <iostream>
#include <vector>
using namespace std;
int main(){
int N, M;
cin >> N >> M;
vector<pair<int, int>> cond(M);
for(auto& [A, B] : cond) cin >> A >> B;
int K;
cin >> K;
vector<pair<int, int>> choice(K);
for(auto& [C, D] : choice) cin >> C >> D;
int ans = 0;
for(int bit = 0; bit < 1 << K; bit++){
vector<bool> ball(N);
for(int i = 0; i < K; i++){
const auto [C, D] = choice[i];
ball[bit & 1 << i ? C : D] = 1;
}
int cnt = 0;
for(auto [A, B] : cond) if(ball[A] && ball[B]) cnt++;
if(ans < cnt) ans = cnt;
}
cout << ans << endl;
}
回答例 (Python)
import itertools
N, M = map(int, input().split())
cond = [tuple(map(int, input().split())) for i in range(M)]
K = int(input())
choice = [tuple(map(int, input().split())) for i in range(K)]
ans = 0
for balls in itertools.product(*choice):
balls = set(balls)
cnt = sum(A in balls and B in balls for A, B in cond)
if ans < cnt:
ans = cnt
print(ans)
posted:
last update: