結果
問題 |
No.2236 Lights Out On Simple Graph
|
ユーザー |
![]() |
提出日時 | 2023-03-06 00:44:53 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,890 ms / 4,000 ms |
コード長 | 1,987 bytes |
コンパイル時間 | 3,893 ms |
コンパイル使用メモリ | 256,660 KB |
最終ジャッジ日時 | 2025-02-11 05:42:13 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using ll=long long; using ld=long double; ld pie=3.141592653589793; ll inf=10010010010010010; ll mod=998244353; int main(){ ll n,m; cin >> n >> m; vector<ll>a(m),b(m); for (ll i = 0; i < m; i++) { cin >> a[i] >> b[i]; a[i]--,b[i]--; } vector<ll>c(n); vector<ll>two(50,1); for (ll i = 1; i < 50; i++) { two[i]=two[i-1]*2; } ll now=0; for (ll i = 0; i < n; i++) { cin >> c[i]; now+=two[i]*c[i]; } map<ll,ll>dp1,dp2; ll x=m/2; for (ll i = 0; i < two[x]; i++) { ll y=now; ll kai=0; for (ll j = 0; j < x; j++) { if (i&two[j]) { y^=two[a[j]]; y^=two[b[j]]; kai+=1; } } if (y==now) { dp1[y]=0; }else{ if (dp1[y]==0) { dp1[y]=kai; }else{ dp1[y]=min(dp1[y],kai); } } } x=m-x; for (ll i = 0; i < two[x]; i++) { ll y=0; ll kai=0; for (ll j = m/2; j < m; j++) { if (i&two[j-m/2]) { y^=two[a[j]]; y^=two[b[j]]; kai+=1; } } if (y==0) { dp2[y]=0; }else{ if (dp2[y]==0) { dp2[y]=kai; }else{ dp2[y]=min(dp2[y],kai); } } } ll ans=inf; for(auto v:dp1){ if (v.first==0) { ans=min(ans,v.second); }else{ if (dp2[v.first]!=0) { ans=min(ans,v.second+dp2[v.first]); } } } if (ans==inf) { cout << -1 << endl; return 0; } cout << ans <<endl; }