Submission #960844


Source Code Expand

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;

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])])continue;
			if(!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);
		for(int i = 0;i < n;i++){
			if(color[i] != -1)continue;
			Queue<Integer> q = new ArrayDeque<>();
			q.add(i);
			color[i] = 0;
			while(!q.isEmpty()){
				int cur = q.poll();
				for(int e : g[cur]){
					if(color[e] == -1){
						color[e] = (color[cur] + 1) % 3;
						q.add(e);
					}
				}
				for(int e : ig[cur]){
					if(color[e] == -1){
						color[e] = (color[cur] + 2) % 3;
						q.add(e);
					}
				}
			}
		}
		return color;
	}
	
	public static int[][] packU(int n, int[] from, int[] to){ return packU(n, from, to, from.length); }
	public static int[][] packU(int n, int[] from, int[] to, int sup)
	{
		int[][] g = new int[n][];
		int[] p = new int[n];
		for(int i = 0;i < sup;i++)p[from[i]]++;
		for(int i = 0;i < sup;i++)p[to[i]]++;
		for(int i = 0;i < n;i++)g[i] = new int[p[i]];
		for(int i = 0;i < sup;i++){
			g[from[i]][--p[from[i]]] = to[i];
			g[to[i]][--p[to[i]]] = from[i];
		}
		return g;
	}

	
	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 7191 Byte
Status AC
Exec Time 252 ms
Memory 31600 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1700 / 1700
Status
AC × 3
AC × 53
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 97 ms 8144 KB
0_01.txt AC 96 ms 8148 KB
0_02.txt AC 97 ms 8016 KB
1_00.txt AC 96 ms 8016 KB
1_01.txt AC 192 ms 29008 KB
1_02.txt AC 242 ms 31600 KB
1_03.txt AC 220 ms 20456 KB
1_04.txt AC 207 ms 20372 KB
1_05.txt AC 206 ms 20156 KB
1_06.txt AC 215 ms 20924 KB
1_07.txt AC 213 ms 20280 KB
1_08.txt AC 213 ms 20504 KB
1_09.txt AC 221 ms 20152 KB
1_10.txt AC 205 ms 20828 KB
1_11.txt AC 215 ms 21604 KB
1_12.txt AC 208 ms 20524 KB
1_13.txt AC 210 ms 19628 KB
1_14.txt AC 210 ms 21576 KB
1_15.txt AC 242 ms 21408 KB
1_16.txt AC 240 ms 21696 KB
1_17.txt AC 160 ms 11144 KB
1_18.txt AC 229 ms 21664 KB
1_19.txt AC 248 ms 22340 KB
1_20.txt AC 231 ms 21668 KB
1_21.txt AC 231 ms 22548 KB
1_22.txt AC 246 ms 22732 KB
1_23.txt AC 249 ms 22336 KB
1_24.txt AC 246 ms 22392 KB
1_25.txt AC 248 ms 22484 KB
1_26.txt AC 245 ms 22128 KB
1_27.txt AC 250 ms 22388 KB
1_28.txt AC 231 ms 22596 KB
1_29.txt AC 252 ms 22456 KB
1_30.txt AC 228 ms 21756 KB
1_31.txt AC 228 ms 22228 KB
1_32.txt AC 230 ms 21996 KB
1_33.txt AC 248 ms 22444 KB
1_34.txt AC 216 ms 18468 KB
1_35.txt AC 202 ms 15888 KB
1_36.txt AC 209 ms 17692 KB
1_37.txt AC 223 ms 20384 KB
1_38.txt AC 179 ms 14280 KB
1_39.txt AC 192 ms 13756 KB
1_40.txt AC 246 ms 21628 KB
1_41.txt AC 227 ms 20772 KB
1_42.txt AC 209 ms 16840 KB
1_43.txt AC 209 ms 15752 KB
1_44.txt AC 220 ms 18716 KB
1_45.txt AC 187 ms 13244 KB
1_46.txt AC 225 ms 19616 KB
1_47.txt AC 227 ms 20648 KB
1_48.txt AC 203 ms 15332 KB
1_49.txt AC 208 ms 17168 KB