結果

問題 No.1605 Matrix Shape
ユーザー nemutainemutai
提出日時 2023-04-16 18:36:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,357 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 183,324 KB
実行使用メモリ 39,276 KB
最終ジャッジ日時 2024-04-20 06:27:22
合計ジャッジ時間 7,241 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
15,824 KB
testcase_01 AC 11 ms
15,724 KB
testcase_02 AC 9 ms
15,840 KB
testcase_03 WA -
testcase_04 AC 10 ms
15,732 KB
testcase_05 AC 10 ms
15,708 KB
testcase_06 WA -
testcase_07 AC 9 ms
15,860 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 11 ms
15,908 KB
testcase_16 AC 10 ms
15,660 KB
testcase_17 AC 10 ms
15,768 KB
testcase_18 AC 10 ms
15,704 KB
testcase_19 AC 321 ms
39,276 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 192 ms
25,132 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 87 ms
20,824 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 142 ms
22,132 KB
testcase_34 WA -
testcase_35 AC 131 ms
23,280 KB
testcase_36 AC 79 ms
20,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
const ll INF=1ll<<60;
using vp=vector<pair<ll, ll>>;
using P = pair<ll,ll>;


struct unionfind{
    ll N;
    vector<ll> par,Rank,Size;
    unionfind(ll n):N(n){
        par=vector<ll>(N),Rank=vector<ll>(N,1),Size=vector<ll>(N,1);
        for(int i=0;i<N;i++) par[i]=i;
    };
    ll find(ll x){
        if(par[x]==x) return x;
        else return par[x]=find(par[x]);
    }

    bool same(ll a,ll b){
        return find(a)==find(b);
    }

    void unite(ll a,ll b){
        if(same(a,b))return;
        a=find(a);
        b=find(b);
        ll s=Size[a]+Size[b];
        if(Rank[b]>Rank[a]) par[a]=b;
        else par[b]=a;
        if(Rank[a]==Rank[b]) Rank[a]++;
        Size[find(a)]=s;
    }

    ll size(ll a){
       return Size[find(a)];
    }
};

int main(){
    ll N;
    cin>>N;
    unionfind uf(2e5);
    vll A(2e5);
    vvll ab(2e5);
    vp hw(N);
    vll deg(2e5);
    for(int i=0;i<N;i++){
        cin>>hw[i].first>>hw[i].second;
        hw[i].first--;hw[i].second--;
        if(hw[i].first!=hw[i].second){
            A[hw[i].second]++;
            deg[hw[i].first]++;
            deg[hw[i].second]++;
        }
        ab[hw[i].first].push_back(hw[i].second);
        uf.unite(hw[i].first,hw[i].second);
    }
    if(uf.size(uf.find(hw[0].first))!=N){
        cout<<0<<endl;
        return 0;
    }
    queue<ll> q;
    for(int i=0;i<2e5;i++){
        if(A[i]==0&&ab[i].size())q.push(i);
    }
    ll cnt=0;
    bool a=true;
    while(!q.empty()){
        auto x=q.front();q.pop();
        if(q.size()!=0)a=false;
        cnt++;
        for(auto to:ab[x]){
            A[to]--;
            if(A[to]==0)q.push(to);
        }
    }
    if(cnt==N){
        if(a)cout << 1 << endl;
        else cout << 0 <<endl;
        return 0;
    }
    for(int i=0;i<2e5;i++){
        if(ab[i].size()){
            if(deg[i]!=2){
                cout<<0<<endl;
                return 0;
            }
        }
    }
    set<P> st;
    vll n;
    ll now=hw[0].first;
    for(int i=0;i<N+1;i++){
        n.push_back(now);
        now=ab[now][0];
    }
    for(int i=0;i<N;i++){
        st.insert(make_pair(n[i+1],n[i]));
    }
    cout << st.size()<< endl;
        
}
0