Submission #2120062


Source Code Expand

/*
 * x 軸上に N 人の人が立っています。 人 i の位置を xi とします。 任意の i に対して、xi は 0 以上 10^9 以下の整数です。 同じ位置に複数の人が立っていることもありえます。
 * 
 * これらの人の位置に関する情報が M 個与えられます。 このうち i 個めの情報は (Li,Ri,Di) という形をしています。 
 * この情報は、人 Ri は人 Li よりも距離 Di だけ右にいること、 すなわち、xRi−xLi=Di が成り立つことを表します。
 * 
 * これら M 個の情報のうちのいくつかに誤りがある可能性があることがわかりました。 与えられる M 個すべての情報と矛盾しないような値の組 (x1,x2,…,xN) が存在するかどうか判定してください。
 */

import java.util.*;
	public class Main{
		
		static int[] belongs = null;
		static int[] states = null;
        
		public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            //入力
            int n = Integer.parseInt(sc.next());
            int m = Integer.parseInt(sc.next());
            states = new int[n];
            belongs = new int[n];
            
            for(int i=0; i<n; i++){
            	belongs[i] = i;
            	states[i] = 0;
            }
            
            for(int i=0; i<m; i++){
            	int l = Integer.parseInt(sc.next())-1;
            	int r = Integer.parseInt(sc.next())-1;
            	int d = Integer.parseInt(sc.next());
            	if(!merge(l, r, d)){
            		System.out.println("No");
            		sc.close();
            		return;
            	}
            }
            System.out.println("Yes");
            sc.close();
		}
            
		public static int updateBelongs(int a){
			if(a == belongs[a]){
				return a;
			}else{
				int temp = belongs[a];
				belongs[a] = updateBelongs(belongs[a]);
				states[a] += states[temp];
				return belongs[a];
			}
		}
		
		public static boolean merge(int l, int r, int d){
			int lb = updateBelongs(l);
			int rb = updateBelongs(r);
			
            if(lb == rb){
            	return states[r] == states[l] + d;
            }else{
            	belongs[rb] = lb;
            	states[rb] = states[l] + d - states[r];
            	return true;
            }
        }
    }
	

Submission Info

Submission Time
Task D - People on a Line
User kogumakoguma
Language Java8 (OpenJDK 1.8.0)
Score 400
Code Size 2411 Byte
Status AC
Exec Time 965 ms
Memory 90452 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 5
AC × 47
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt
Case Name Status Exec Time Memory
01.txt AC 150 ms 27600 KB
02.txt AC 635 ms 88832 KB
03.txt AC 365 ms 48616 KB
04.txt AC 658 ms 85720 KB
05.txt AC 388 ms 46572 KB
06.txt AC 568 ms 63364 KB
07.txt AC 375 ms 45080 KB
08.txt AC 527 ms 63504 KB
09.txt AC 537 ms 61568 KB
10.txt AC 512 ms 60144 KB
11.txt AC 470 ms 52324 KB
12.txt AC 405 ms 42728 KB
13.txt AC 591 ms 61952 KB
14.txt AC 540 ms 58776 KB
15.txt AC 422 ms 46708 KB
16.txt AC 521 ms 62784 KB
17.txt AC 593 ms 69832 KB
18.txt AC 624 ms 64292 KB
19.txt AC 605 ms 66160 KB
20.txt AC 584 ms 63548 KB
21.txt AC 458 ms 49740 KB
22.txt AC 475 ms 45832 KB
23.txt AC 435 ms 45904 KB
24.txt AC 476 ms 51768 KB
25.txt AC 664 ms 90452 KB
26.txt AC 611 ms 70608 KB
27.txt AC 518 ms 59792 KB
28.txt AC 554 ms 64444 KB
29.txt AC 523 ms 63196 KB
30.txt AC 965 ms 59556 KB
31.txt AC 532 ms 61712 KB
32.txt AC 597 ms 63504 KB
33.txt AC 513 ms 60952 KB
34.txt AC 450 ms 48644 KB
35.txt AC 515 ms 61588 KB
36.txt AC 526 ms 51852 KB
37.txt AC 520 ms 59060 KB
38.txt AC 496 ms 57136 KB
39.txt AC 493 ms 60116 KB
40.txt AC 477 ms 59472 KB
41.txt AC 104 ms 19796 KB
42.txt AC 409 ms 46004 KB
sample01.txt AC 93 ms 20564 KB
sample02.txt AC 91 ms 19924 KB
sample03.txt AC 92 ms 19796 KB
sample04.txt AC 91 ms 20564 KB
sample05.txt AC 90 ms 21716 KB