結果

問題 No.2236 Lights Out On Simple Graph
ユーザー hiro71687k
提出日時 2023-03-05 19:15:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,676 bytes
コンパイル時間 4,602 ms
コンパイル使用メモリ 263,528 KB
最終ジャッジ日時 2025-02-11 05:35:19
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40 WA * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:111:13: warning: ‘id’ may be used uninitialized [-Wmaybe-uninitialized]
  111 |         c[id]=0;
      |             ^
main.cpp:102:20: note: ‘id’ was declared here
  102 |         ll now=inf,id;
      |                    ^~

ソースコード

diff #

#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;
struct unionfind
{
    vector<ll>par,siz;
    unionfind(ll n): par(n,-1),siz(n,1){}
    ll root(ll x){
        if (par[x]==-1)
        {
            return x;
        }else{
            return par[x]=root(par[x]);
        }
    }
    bool issame(ll x,ll y){
        return root(x)==root(y);
    }
    bool unite(ll x,ll y){
        x=root(x);
        y=root(y);
        if (x==y)
        {
            return false;
        }
        if (siz[x]<siz[y])
        {
            swap(x,y);
        }
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
int main(){
    ll n,m;
    cin >> n >> m;
    unionfind uf(n);
    vector<ll>memo(n,0);
    vector<vector<ll>>g(n);
    for (ll i = 0; i <m ; i++)
    {
        ll a,b;
        cin >> a >> b;
        a--,b--;
        uf.unite(a,b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<ll>c(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> c[i];
        memo[uf.root(i)]+=c[i];
    }
    bool ok=true;
    for (ll i = 0; i < n; i++)
    {
        if (memo[i]%2)
        {
            ok=false;
        }
    }
    if (!ok)
    {
        cout << -1 << endl;
        return 0;
    }
    map<pair<ll,ll>,ll>mm;
    for (ll i = 0; i < n; i++)
    {
        if (c[i]==0)
        {
            continue;
        }
        queue<ll>que;
        que.push(i);
        vector<ll>memo(n,inf);
        memo[i]=0;
        while (!que.empty())
        {
            ll v=que.front();
            que.pop();
            for (ll j = 0; j < g[v].size(); j++)
            {
                if (memo[g[v][j]]>memo[v]+1)
                {
                    memo[g[v][j]]=memo[v]+1;
                    que.push(g[v][j]);
                }
            }
        }
        ll now=inf,id;
        for (ll j = i+1; j < n; j++)
        {
            if (c[j]==1&&memo[j]<now)
            {
                now=memo[j];
                id=j;
            }
        }
        c[id]=0;
        ll x=id;
        while (x!=i)
        {
            for (ll j = 0; j < g[x].size(); j++)
            {
                if (memo[x]-1==memo[g[x][j]])
                {
                    mm[{x,g[x][j]}]+=1;
                    mm[{g[x][j],x}]+=1;
                    x=g[x][j];
                    break;
                }
            }
        }
    }
    ll ans=0;
    for(auto v:mm){
        ans+=v.second%2;
    }
    ans/=2;
    cout << ans << endl;
}
0