# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
311279 | 2020-10-09T20:27:17 Z | KWang31 | Travelling Salesperson (CCO20_day2problem1) | Java 11 | 3574 ms | 242152 KB |
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N=Integer.parseInt(br.readLine()); boolean[][] adj=new boolean[N][N]; for (int i = 1; i < N; i++) { String s=br.readLine(); for (int j = 0; j <i; j++) { if(s.charAt(j)=='R'){ adj[i][j]=true; adj[j][i]=true; } } } boolean[] c=new boolean[N];//Tracks color of first edge, red=true Deque<Integer> [][] dq=new Deque[N][2];//Tracks first part (same color as first edge) int[] rev2=new int [N];//Tracks whether dq[1] is first or dq[0] is first for (int i = 0; i < N; i++) { dq[i][0]=new ArrayDeque<>(); dq[i][1]=new ArrayDeque<>(); dq[i][0].add(i);//Prevents empty stuff } if(adj[0][1]){ c[0]=true; c[1]=true; } dq[0][0].addLast(1); dq[1][0].addLast(0); int k; for (int i = 2; i < N; i++) { /* if(i==5){ for (int j = 0; j < i; j++) { System.out.println(j+" "+c[j]); System.out.println(dq[j][0]); System.out.println(dq[j][1]); } } */ for (int j = 0; j < i; j++) {//We are adding vertex i k=dq[j][rev2[j]].getLast(); /* if(i==4 && j==0){ System.out.println(k+" "+adj[k][i]+" "+c[j]); System.out.println(dq[j][rev2[j]]+" "+dq[j][1-rev2[j]]); } */ if(adj[k][i]==c[j]){ if(dq[j][1-rev2[j]].isEmpty()){ if(j==0){ //Simply construct edge in reverse c[i]=c[j]; Iterator<Integer> it=dq[j][rev2[j]].descendingIterator(); while(it.hasNext()){ dq[i][0].addLast(it.next()); } } dq[j][rev2[j]].addLast(i);//Now update }else{ if(j==0){//Construct for i c[i]=c[j]; Iterator<Integer> it=dq[j][rev2[j]].descendingIterator(); while(it.hasNext()){ dq[i][0].addLast(it.next()); } it=dq[j][1-rev2[j]].descendingIterator(); while(it.hasNext()){ dq[i][1].addLast(it.next()); } if(!dq[j][1-rev2[j]].isEmpty() && adj[j][dq[j][1-rev2[j]].getLast()]==c[i]){ dq[i][0].addLast(dq[i][1].removeFirst()); }else if(dq[j][1-rev2[j]].isEmpty() && adj[j][dq[j][rev2[j]].getLast()]!=c[i]){ dq[i][1].addFirst(dq[i][0].removeLast()); } } //Now update path dq[j][rev2[j]].addLast(i); if(adj[i][dq[j][1-rev2[j]].getFirst()]==c[j]){ dq[j][rev2[j]].addLast(dq[j][1-rev2[j]].removeFirst()); } } }else{ //First update path for i if(j==0){ c[i]=!c[j]; dq[i][0].add(k); Iterator<Integer> it=dq[j][1-rev2[j]].iterator(); while(it.hasNext()){ dq[i][0].addLast(it.next()); } it=dq[j][rev2[j]].iterator(); while(it.hasNext()){ int n=it.next(); //System.out.println(n+"!"); dq[i][1].addLast(n); } dq[i][1].removeLast(); if(adj[j][dq[i][rev2[i]].getLast()]==c[i]){ dq[i][0].addLast(dq[i][1].removeFirst()); } } //Now update path for j if(dq[j][rev2[j]].size()==2){ if(adj[j][i]!=c[j]){ dq[j][rev2[j]].removeFirst(); dq[j][1-rev2[j]].addFirst(dq[j][rev2[j]].removeFirst()); dq[j][1-rev2[j]].addFirst(i); dq[j][1-rev2[j]].addFirst(j); rev2[j]=1-rev2[j]; c[j]=!c[j]; }else{ dq[j][1-rev2[j]].addFirst(dq[j][rev2[j]].removeLast()); dq[j][rev2[j]].addLast(i); } }else{ dq[j][1-rev2[j]].addFirst(dq[j][rev2[j]].removeLast()); if(c[j]!=adj[i][dq[j][rev2[j]].getLast()]){ dq[j][1-rev2[j]].addFirst(i); }else{ dq[j][rev2[j]].addLast(i); } } } } } StringBuilder sb=new StringBuilder(); for (int i = 0; i < N; i++) { sb.append(N).append("\n"); Iterator<Integer> it=dq[i][rev2[i]].iterator(); while(it.hasNext()){ sb.append(it.next()+1).append(" "); } //sb.append("\n"); it=dq[i][1-rev2[i]].iterator(); while(it.hasNext()){ sb.append(it.next()+1).append(" "); } sb.append("\n"); } System.out.println(sb.toString()); } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 78 ms | 10236 KB | Output is correct |
2 | Correct | 76 ms | 10344 KB | Output is correct |
3 | Correct | 78 ms | 10340 KB | Output is correct |
4 | Correct | 76 ms | 10344 KB | Output is correct |
5 | Correct | 85 ms | 10104 KB | Output is correct |
6 | Correct | 77 ms | 10060 KB | Output is correct |
7 | Correct | 80 ms | 10184 KB | Output is correct |
8 | Correct | 81 ms | 10236 KB | Output is correct |
9 | Correct | 77 ms | 10232 KB | Output is correct |
10 | Correct | 76 ms | 10104 KB | Output is correct |
11 | Correct | 79 ms | 10352 KB | Output is correct |
12 | Correct | 77 ms | 10232 KB | Output is correct |
13 | Correct | 78 ms | 10420 KB | Output is correct |
14 | Correct | 79 ms | 10464 KB | Output is correct |
15 | Correct | 79 ms | 10344 KB | Output is correct |
16 | Correct | 1640 ms | 208496 KB | Output is correct |
17 | Correct | 1318 ms | 199180 KB | Output is correct |
18 | Correct | 83 ms | 10600 KB | Output is correct |
19 | Correct | 1466 ms | 202712 KB | Output is correct |
20 | Correct | 3574 ms | 228196 KB | Output is correct |
21 | Correct | 84 ms | 10532 KB | Output is correct |
22 | Correct | 1743 ms | 210856 KB | Output is correct |
23 | Correct | 1819 ms | 219048 KB | Output is correct |
24 | Correct | 98 ms | 10728 KB | Output is correct |
25 | Correct | 1567 ms | 207100 KB | Output is correct |
26 | Correct | 1988 ms | 220644 KB | Output is correct |
27 | Correct | 354 ms | 30740 KB | Output is correct |
28 | Correct | 1755 ms | 206856 KB | Output is correct |
29 | Correct | 1936 ms | 213296 KB | Output is correct |
30 | Correct | 1767 ms | 208540 KB | Output is correct |
31 | Correct | 1554 ms | 198440 KB | Output is correct |
32 | Correct | 1647 ms | 212884 KB | Output is correct |
33 | Correct | 1852 ms | 213000 KB | Output is correct |
34 | Correct | 1736 ms | 208732 KB | Output is correct |
35 | Correct | 1953 ms | 215068 KB | Output is correct |
36 | Correct | 1668 ms | 205440 KB | Output is correct |
37 | Correct | 1711 ms | 207396 KB | Output is correct |
38 | Correct | 1402 ms | 199396 KB | Output is correct |
39 | Correct | 1677 ms | 208800 KB | Output is correct |
40 | Correct | 1631 ms | 207416 KB | Output is correct |
41 | Correct | 1498 ms | 204392 KB | Output is correct |
42 | Correct | 1370 ms | 197784 KB | Output is correct |
43 | Correct | 1924 ms | 210448 KB | Output is correct |
44 | Correct | 1778 ms | 207012 KB | Output is correct |
45 | Correct | 1738 ms | 209136 KB | Output is correct |
46 | Correct | 1229 ms | 198404 KB | Output is correct |
47 | Correct | 1571 ms | 217008 KB | Output is correct |
48 | Correct | 1784 ms | 215592 KB | Output is correct |
49 | Correct | 1702 ms | 212172 KB | Output is correct |
50 | Correct | 1743 ms | 207000 KB | Output is correct |
51 | Correct | 1950 ms | 222596 KB | Output is correct |
52 | Correct | 1812 ms | 227464 KB | Output is correct |
53 | Correct | 1818 ms | 211020 KB | Output is correct |
54 | Correct | 1498 ms | 206624 KB | Output is correct |
55 | Correct | 1792 ms | 227804 KB | Output is correct |
56 | Correct | 2121 ms | 230688 KB | Output is correct |
57 | Correct | 2137 ms | 229304 KB | Output is correct |
58 | Correct | 1877 ms | 238620 KB | Output is correct |
59 | Correct | 2047 ms | 225340 KB | Output is correct |
60 | Correct | 1922 ms | 242152 KB | Output is correct |
61 | Correct | 1859 ms | 225420 KB | Output is correct |
62 | Correct | 1867 ms | 228464 KB | Output is correct |
63 | Correct | 1963 ms | 226832 KB | Output is correct |
64 | Correct | 2013 ms | 240952 KB | Output is correct |
65 | Correct | 2059 ms | 241308 KB | Output is correct |
66 | Correct | 1860 ms | 227652 KB | Output is correct |
67 | Correct | 1970 ms | 226492 KB | Output is correct |
68 | Correct | 2109 ms | 230624 KB | Output is correct |
69 | Correct | 1959 ms | 240300 KB | Output is correct |
70 | Correct | 2073 ms | 240632 KB | Output is correct |
71 | Correct | 1591 ms | 223788 KB | Output is correct |
72 | Correct | 2128 ms | 224436 KB | Output is correct |
73 | Correct | 1930 ms | 242144 KB | Output is correct |
74 | Correct | 2080 ms | 226860 KB | Output is correct |
75 | Correct | 1903 ms | 224560 KB | Output is correct |
76 | Correct | 3539 ms | 226488 KB | Output is correct |
77 | Correct | 1874 ms | 226024 KB | Output is correct |
78 | Correct | 1994 ms | 228096 KB | Output is correct |
79 | Correct | 2087 ms | 225560 KB | Output is correct |
80 | Correct | 1991 ms | 236748 KB | Output is correct |
81 | Correct | 3393 ms | 225728 KB | Output is correct |
82 | Correct | 2001 ms | 225512 KB | Output is correct |
83 | Correct | 1989 ms | 225288 KB | Output is correct |
84 | Correct | 1868 ms | 232932 KB | Output is correct |
85 | Correct | 1956 ms | 237396 KB | Output is correct |
86 | Correct | 1984 ms | 229840 KB | Output is correct |
87 | Correct | 392 ms | 31360 KB | Output is correct |
88 | Correct | 354 ms | 30640 KB | Output is correct |
89 | Correct | 2179 ms | 219652 KB | Output is correct |
90 | Correct | 2258 ms | 218252 KB | Output is correct |
91 | Correct | 351 ms | 31024 KB | Output is correct |
92 | Correct | 366 ms | 31428 KB | Output is correct |
93 | Correct | 2130 ms | 234292 KB | Output is correct |
94 | Correct | 2158 ms | 231760 KB | Output is correct |
95 | Correct | 376 ms | 31772 KB | Output is correct |
96 | Correct | 345 ms | 31780 KB | Output is correct |
97 | Correct | 2302 ms | 231856 KB | Output is correct |
98 | Correct | 1840 ms | 216076 KB | Output is correct |