Submission #960848
Source Code Expand
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(), m = ni(); int[] from = new int[m]; int[] to = new int[m]; for(int i = 0;i < m;i++){ from[i] = ni()-1; to[i] = ni()-1; } int[][] g = packD(n, from, to); int[][] ig = packD(n, to, from); int[] color = color3(g, ig); boolean[] dead = new boolean[n]; boolean[] tri = new boolean[n]; for(int i = 0;i < n;i++){ int nex = color[i] + 1; if(nex >= 3)nex -= 3; for(int e : g[i]){ if(color[e] != nex){ dead[i] = true; } } nex = color[i] - 1; if(nex < 0)nex += 3; for(int e : ig[i]){ if(color[e] != nex){ dead[i] = true; } } } for(int i = 0;i < n;i++){ for(int e : g[i]){ if(g[e].length > 0)tri[i] = true; } } DJSet ds = new DJSet(n); ds.dead = dead; ds.tri = tri; for(int i = 0;i < m;i++){ ds.union(from[i], to[i]); } long ret = 0; for(int i = 0;i < n;i++){ if(ds.upper[i] < 0){ if(ds.dead[i]){ long sz = -ds.upper[i]; ret += sz*sz; } } } for(int i = 0;i < m;i++){ if(!ds.dead[ds.root(from[i])] && !ds.tri[ds.root(from[i])])ret++; } int[][] cts = new int[3][n]; for(int i = 0;i < n;i++){ if(!ds.dead[ds.root(i)] && ds.tri[ds.root(i)]){ cts[color[i]][ds.root(i)]++; } } for(int i = 0;i < n;i++){ if(ds.upper[i] < 0 && ds.tri[i]){ ret += (long)cts[0][i]*cts[1][i]; ret += (long)cts[1][i]*cts[2][i]; ret += (long)cts[2][i]*cts[0][i]; } } out.println(ret); } static int[] color3(int[][] g, int[][] ig) { int n = g.length; int[] color = new int[n]; Arrays.fill(color, -1); int[] q = new int[n]; for(int i = 0;i < n;i++){ if(color[i] != -1)continue; int p = 0; q[p++] = i; color[i] = 0; for(int r = 0;r < p;r++){ int cur = q[r]; for(int e : g[cur]){ if(color[e] == -1){ color[e] = (color[cur] + 1); if(color[e] >= 3)color[e] -= 3; q[p++] = e; } } for(int e : ig[cur]){ if(color[e] == -1){ color[e] = (color[cur] + 2); if(color[e] >= 3)color[e] -= 3; q[p++] = e; } } } } return color; } public static class DJSet { public int[] upper; public boolean[] dead; public boolean[] tri; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } dead[x] |= dead[y]; tri[x] |= tri[y]; upper[x] += upper[y]; upper[y] = x; } return x == y; } public int count() { int ct = 0; for (int u : upper) if (u < 0) ct++; return ct; } } static int[][] packD(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; } return g; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Submission Info
Submission Time | |
---|---|
Task | F - Blackout |
User | uwi |
Language | Java8 (OpenJDK 1.8.0) |
Score | 1700 |
Code Size | 6704 Byte |
Status | AC |
Exec Time | 224 ms |
Memory | 20068 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1700 / 1700 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 96 ms | 8016 KB |
0_01.txt | AC | 96 ms | 8020 KB |
0_02.txt | AC | 96 ms | 8016 KB |
1_00.txt | AC | 95 ms | 8144 KB |
1_01.txt | AC | 154 ms | 17184 KB |
1_02.txt | AC | 201 ms | 19224 KB |
1_03.txt | AC | 207 ms | 20068 KB |
1_04.txt | AC | 206 ms | 20020 KB |
1_05.txt | AC | 202 ms | 19448 KB |
1_06.txt | AC | 204 ms | 19920 KB |
1_07.txt | AC | 200 ms | 18948 KB |
1_08.txt | AC | 199 ms | 19544 KB |
1_09.txt | AC | 207 ms | 19912 KB |
1_10.txt | AC | 202 ms | 19036 KB |
1_11.txt | AC | 207 ms | 20020 KB |
1_12.txt | AC | 199 ms | 18812 KB |
1_13.txt | AC | 211 ms | 19020 KB |
1_14.txt | AC | 208 ms | 19604 KB |
1_15.txt | AC | 209 ms | 18552 KB |
1_16.txt | AC | 209 ms | 18812 KB |
1_17.txt | AC | 161 ms | 11056 KB |
1_18.txt | AC | 211 ms | 18216 KB |
1_19.txt | AC | 210 ms | 18392 KB |
1_20.txt | AC | 219 ms | 18356 KB |
1_21.txt | AC | 219 ms | 19308 KB |
1_22.txt | AC | 217 ms | 18348 KB |
1_23.txt | AC | 224 ms | 19272 KB |
1_24.txt | AC | 219 ms | 18620 KB |
1_25.txt | AC | 211 ms | 18408 KB |
1_26.txt | AC | 220 ms | 19244 KB |
1_27.txt | AC | 217 ms | 19712 KB |
1_28.txt | AC | 222 ms | 18564 KB |
1_29.txt | AC | 219 ms | 18372 KB |
1_30.txt | AC | 219 ms | 18284 KB |
1_31.txt | AC | 218 ms | 19616 KB |
1_32.txt | AC | 221 ms | 19100 KB |
1_33.txt | AC | 222 ms | 18956 KB |
1_34.txt | AC | 213 ms | 17592 KB |
1_35.txt | AC | 191 ms | 14548 KB |
1_36.txt | AC | 207 ms | 15544 KB |
1_37.txt | AC | 215 ms | 18728 KB |
1_38.txt | AC | 190 ms | 13736 KB |
1_39.txt | AC | 179 ms | 12868 KB |
1_40.txt | AC | 205 ms | 17928 KB |
1_41.txt | AC | 214 ms | 17860 KB |
1_42.txt | AC | 194 ms | 15340 KB |
1_43.txt | AC | 194 ms | 14680 KB |
1_44.txt | AC | 212 ms | 16672 KB |
1_45.txt | AC | 178 ms | 12828 KB |
1_46.txt | AC | 207 ms | 17776 KB |
1_47.txt | AC | 216 ms | 19220 KB |
1_48.txt | AC | 184 ms | 14076 KB |
1_49.txt | AC | 200 ms | 15308 KB |