結果
問題 | No.1605 Matrix Shape |
ユーザー | merlin |
提出日時 | 2021-07-16 22:19:43 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,407 bytes |
コンパイル時間 | 2,124 ms |
コンパイル使用メモリ | 75,260 KB |
実行使用メモリ | 134,508 KB |
最終ジャッジ日時 | 2023-09-20 14:31:58 |
合計ジャッジ時間 | 23,690 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 110 ms
62,952 KB |
testcase_01 | AC | 110 ms
64,480 KB |
testcase_02 | AC | 108 ms
64,632 KB |
testcase_03 | WA | - |
testcase_04 | AC | 106 ms
64,416 KB |
testcase_05 | AC | 109 ms
64,736 KB |
testcase_06 | WA | - |
testcase_07 | AC | 108 ms
64,560 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 110 ms
64,440 KB |
testcase_12 | WA | - |
testcase_13 | AC | 110 ms
64,464 KB |
testcase_14 | WA | - |
testcase_15 | AC | 109 ms
64,648 KB |
testcase_16 | AC | 109 ms
64,284 KB |
testcase_17 | AC | 110 ms
64,504 KB |
testcase_18 | AC | 105 ms
64,500 KB |
testcase_19 | TLE | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 1,319 ms
121,416 KB |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 571 ms
95,252 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 702 ms
101,288 KB |
testcase_34 | WA | - |
testcase_35 | AC | 1,022 ms
119,440 KB |
testcase_36 | AC | 516 ms
108,412 KB |
ソースコード
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); } } }