結果

問題 No.1605 Matrix Shape
ユーザー auauaauaua
提出日時 2021-05-14 18:40:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 170,128 KB
実行使用メモリ 31,832 KB
最終ジャッジ日時 2023-09-20 12:34:25
合計ジャッジ時間 6,165 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,812 KB
testcase_01 AC 7 ms
10,844 KB
testcase_02 AC 6 ms
10,896 KB
testcase_03 AC 6 ms
10,792 KB
testcase_04 AC 7 ms
10,964 KB
testcase_05 AC 7 ms
10,896 KB
testcase_06 AC 7 ms
10,844 KB
testcase_07 AC 7 ms
11,008 KB
testcase_08 AC 7 ms
10,916 KB
testcase_09 AC 6 ms
10,812 KB
testcase_10 AC 6 ms
10,892 KB
testcase_11 AC 6 ms
10,780 KB
testcase_12 AC 7 ms
10,912 KB
testcase_13 AC 6 ms
10,888 KB
testcase_14 AC 7 ms
11,156 KB
testcase_15 AC 7 ms
10,960 KB
testcase_16 AC 6 ms
10,952 KB
testcase_17 AC 7 ms
10,840 KB
testcase_18 AC 7 ms
10,884 KB
testcase_19 AC 210 ms
31,604 KB
testcase_20 AC 178 ms
22,532 KB
testcase_21 AC 174 ms
22,276 KB
testcase_22 AC 183 ms
24,328 KB
testcase_23 AC 207 ms
29,204 KB
testcase_24 AC 208 ms
31,832 KB
testcase_25 AC 101 ms
27,388 KB
testcase_26 AC 103 ms
27,476 KB
testcase_27 AC 102 ms
27,388 KB
testcase_28 AC 84 ms
12,924 KB
testcase_29 AC 102 ms
27,308 KB
testcase_30 AC 168 ms
16,712 KB
testcase_31 AC 170 ms
16,876 KB
testcase_32 AC 167 ms
24,968 KB
testcase_33 AC 140 ms
13,992 KB
testcase_34 AC 122 ms
27,576 KB
testcase_35 AC 126 ms
15,792 KB
testcase_36 AC 93 ms
21,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=INF;
const ll inf=INF*INF;
//const ll mod=MOD;
const ll MAX=200010;

void dfs(ll u,vector<ll> &trail,vector<vector<ll> >&G){
    while(!G[u].empty()){
        ll v=G[u].back();
        G[u].pop_back();
        dfs(v,trail,G);
    }
    trail.push_back(u);
}

signed main(){
    ll n;cin>>n;
    vector<vector<ll> >G(MAX);
    vector<ll>in(MAX);
    vector<ll>out(MAX);
    ll s=-1,t=-1;
    rep(i,n){
        ll h,w;cin>>h>>w;
        h--;w--;
        out[w]++;
        in[h]++;
        G[h].pb(w);
    }
    ll cnt=0;
    ll kind=0;
    rep(i,MAX){
        if(in[i]==out[i]+1){
            s=i;
            cnt++;
        }else if(in[i]+1==out[i]){
            t=i;
            cnt++;
        }
        if(in[i]>0||out[i]>0)kind++;
    }
    if((cnt!=2&&cnt!=0)||(cnt==2&&(s==-1||t==-1))){
        cout<<0<<endl;
    }else{
        if(s==-1){
            rep(i,MAX){
                if(out[i]>0)s=i;
            }
        }
        vector<ll>trail(0);
        dfs(s,trail,G);
        if(trail.size()==n+1){
            if(cnt==2){
                cout<<1<<endl;
            }else{
                cout<<kind<<endl;
            }
        }else
        {
            cout<<0<<endl;
        }
        
    }
}
0