結果

問題 No.1605 Matrix Shape
ユーザー merlin
提出日時 2021-07-16 22:19:43
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,407 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 78,684 KB
実行使用メモリ 134,400 KB
最終ジャッジ日時 2024-07-06 09:42:50
合計ジャッジ時間 21,066 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int n=Integer.parseInt(bu.readLine());
        int i,N=200000,a[][]=new int[n][2],bad=0; boolean b[]=new boolean[N+1];
        Set<Integer> h[]=new HashSet[N+1];
        for(i=0;i<=N;i++) h[i]=new HashSet<>();

        for(i=0;i<n;i++)
        {
            String s[]=bu.readLine().split(" ");
            a[i][0]=Integer.parseInt(s[0]);
            a[i][1]=Integer.parseInt(s[1]);
            if(a[i][0]==a[i][1]) {bad++; continue;}
            h[a[i][0]].add(a[i][1]);
            b[a[i][1]]=true;
        }

        int last[]=new int[N+1]; boolean vis[]=new boolean[N+1];
        int st=a[0][0];
        for(i=0;i<n;i++)
        if(!b[a[i][0]]) st=a[i][0];

        vis[st]=true;
        dfs(h,last,vis,st);
        int ans=0;
        for(i=0;i<=N;i++)
        if(last[i]==n-bad-1)
        {
            if(h[i].contains(st)) ans=n-bad;
        }
        else if(last[i]==n-bad && n-bad!=0) ans=1;
        System.out.println(ans);
    }

    static void dfs(Set<Integer> g[],int l[],boolean v[],int n)
    {
        for(int x:g[n])
        if(!v[x])
        {
            v[x]=true;
            l[x]=l[n]+1;
            dfs(g,l,v,x);
        }
    }
}
0