結果

問題 No.2236 Lights Out On Simple Graph
ユーザー hiro71687khiro71687k
提出日時 2023-03-05 19:15:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,676 bytes
コンパイル時間 4,977 ms
コンパイル使用メモリ 275,876 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 04:59:49
合計ジャッジ時間 6,970 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,348 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 2 ms
4,348 KB
testcase_35 WA -
testcase_36 AC 2 ms
4,348 KB
testcase_37 WA -
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 1 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 WA -
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 WA -
testcase_56 AC 2 ms
4,348 KB
testcase_57 WA -
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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