Submission #315759

# Submission time Handle Problem Language Result Execution time Memory
315759 2020-10-23T21:23:42 Z KWang31 Graph (BOI20_graph) Java 11
58 / 100
700 ms 68268 KB
import java.io.*; import java.util.*;
public class Graph {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int N=Integer.parseInt(st.nextToken());
        int M=Integer.parseInt(st.nextToken());
        ArrayList<Integer> arl[]=new ArrayList[N];
        for (int i = 0; i < N; i++) {
            arl[i]=new ArrayList<>();
        }
        int a,b,vv;
        for (int i = 0; i < M; i++) {
            st=new StringTokenizer(br.readLine());
            a=Integer.parseInt(st.nextToken())-1;
            b=Integer.parseInt(st.nextToken())-1;
            vv=Integer.parseInt(st.nextToken());
            arl[a].add(4*b+2*vv); arl[b].add(4*a+2*vv);
        }
        
        boolean[] vis=new boolean[N];
        
        int[] m=new int[N];
        int[] c=new int[N];
        int done=0; int pt=0;
        while(done<N){
            while(pt<N && vis[pt]){
                pt++;
            }
            
            //Start DFS at pt, finish the component
            m[pt]=1; //b[pt]=0
            Queue<Integer> qu=new LinkedList<>();
            qu.add(pt); vis[pt]=true; done++;
            Queue<Integer> qu2=new LinkedList<>();//Tracks all vertices in the queue
            qu2.add(pt); boolean fix=false;
            int x,y;
            while(!qu.isEmpty()){
                int v=qu.poll(); 
                for (int p : arl[v]) {
                    x=4-p%4; y=(p-x)/4;
                    if(!vis[y]){
                        vis[y]=true; qu.add(y); 
                        if(!fix){
                            qu2.add(y);
                        }
                        m[y]=-m[v]; c[y]=x-c[v]; done++;
                    }else{
                        //System.out.println(x+" "+y+" "+v);
                        if(m[y]==m[v] && m[v]!=0){
                            //m[p.vtx]x+c[p.vtx]+m[v]x+c[v] is known
                            int xx=(x-c[y]-c[v])/(2*m[y]); fix=true;
                            
                            for(int vtx: qu2){
                                
                                c[vtx]+=m[vtx]*xx; m[vtx]=0;
                            }
                        }else if(m[y]+m[v]==0){//m[v]=0
                            if(c[y]+c[v]!=x){
                                System.out.println("NO"); return;
                            }
                        }
                    }
                }
                //System.out.println(Arrays.toString(m));
                //System.out.println(Arrays.toString(c));
            }
            
            if(m[pt]!=0){//This component is not fixed
                ArrayList<Integer> arl2=new ArrayList<>();
                
                for (int i : qu2) {
                    if(m[i]<0){
                        m[i]=-m[i]; c[i]=-c[i]; qu.add(i);
                    }
                    arl2.add(c[i]);
                }
                
                Collections.sort(arl2);
                int med=arl2.get(arl2.size()/2);
                for (int i : qu2) {
                    c[i]-=med;
                    
                }
                while(!qu.isEmpty()){
                    int i=qu.poll();
                    c[i]=-c[i];
                }
            }
            
        }
        System.out.println("YES");
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < N; i++) {
             sb.append((double)c[i]/2).append(" ");
        }
        System.out.println(sb.toString());
    }
    
}

Compilation message

Note: Graph.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10344 KB answer = YES
2 Correct 81 ms 10596 KB answer = YES
3 Correct 83 ms 10488 KB answer = YES
4 Correct 81 ms 10232 KB answer = NO
5 Correct 82 ms 10556 KB answer = YES
6 Correct 82 ms 10584 KB answer = YES
7 Correct 83 ms 10492 KB answer = YES
8 Correct 82 ms 10352 KB answer = YES
9 Correct 77 ms 10344 KB answer = NO
10 Correct 83 ms 10232 KB answer = YES
11 Correct 80 ms 10356 KB answer = YES
12 Correct 79 ms 10232 KB answer = NO
13 Correct 84 ms 10224 KB answer = YES
14 Correct 83 ms 10596 KB answer = YES
15 Correct 83 ms 10484 KB answer = YES
16 Correct 81 ms 10472 KB answer = YES
17 Correct 83 ms 10300 KB answer = YES
18 Correct 83 ms 10488 KB answer = YES
19 Correct 84 ms 10472 KB answer = YES
20 Correct 83 ms 10344 KB answer = YES
21 Correct 82 ms 10356 KB answer = YES
22 Correct 77 ms 10360 KB answer = NO
23 Correct 80 ms 10360 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10344 KB answer = YES
2 Correct 81 ms 10596 KB answer = YES
3 Correct 83 ms 10488 KB answer = YES
4 Correct 81 ms 10232 KB answer = NO
5 Correct 82 ms 10556 KB answer = YES
6 Correct 82 ms 10584 KB answer = YES
7 Correct 83 ms 10492 KB answer = YES
8 Correct 82 ms 10352 KB answer = YES
9 Correct 77 ms 10344 KB answer = NO
10 Correct 83 ms 10232 KB answer = YES
11 Correct 80 ms 10356 KB answer = YES
12 Correct 79 ms 10232 KB answer = NO
13 Correct 84 ms 10224 KB answer = YES
14 Correct 83 ms 10596 KB answer = YES
15 Correct 83 ms 10484 KB answer = YES
16 Correct 81 ms 10472 KB answer = YES
17 Correct 83 ms 10300 KB answer = YES
18 Correct 83 ms 10488 KB answer = YES
19 Correct 84 ms 10472 KB answer = YES
20 Correct 83 ms 10344 KB answer = YES
21 Correct 82 ms 10356 KB answer = YES
22 Correct 77 ms 10360 KB answer = NO
23 Correct 80 ms 10360 KB answer = NO
24 Correct 101 ms 10700 KB answer = YES
25 Correct 83 ms 10476 KB answer = YES
26 Correct 87 ms 10612 KB answer = YES
27 Correct 85 ms 10472 KB answer = YES
28 Correct 91 ms 10484 KB answer = YES
29 Correct 91 ms 10596 KB answer = YES
30 Correct 83 ms 10472 KB answer = NO
31 Correct 87 ms 10392 KB answer = YES
32 Correct 89 ms 10684 KB answer = YES
33 Correct 85 ms 10600 KB answer = YES
34 Correct 91 ms 10488 KB answer = YES
35 Correct 86 ms 10468 KB answer = YES
36 Correct 91 ms 10700 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10344 KB answer = YES
2 Correct 81 ms 10596 KB answer = YES
3 Correct 83 ms 10488 KB answer = YES
4 Correct 81 ms 10232 KB answer = NO
5 Correct 82 ms 10556 KB answer = YES
6 Correct 82 ms 10584 KB answer = YES
7 Correct 83 ms 10492 KB answer = YES
8 Correct 82 ms 10352 KB answer = YES
9 Correct 77 ms 10344 KB answer = NO
10 Correct 83 ms 10232 KB answer = YES
11 Correct 80 ms 10356 KB answer = YES
12 Correct 79 ms 10232 KB answer = NO
13 Correct 84 ms 10224 KB answer = YES
14 Correct 83 ms 10596 KB answer = YES
15 Correct 83 ms 10484 KB answer = YES
16 Correct 81 ms 10472 KB answer = YES
17 Correct 83 ms 10300 KB answer = YES
18 Correct 83 ms 10488 KB answer = YES
19 Correct 84 ms 10472 KB answer = YES
20 Correct 83 ms 10344 KB answer = YES
21 Correct 82 ms 10356 KB answer = YES
22 Correct 77 ms 10360 KB answer = NO
23 Correct 80 ms 10360 KB answer = NO
24 Correct 101 ms 10700 KB answer = YES
25 Correct 83 ms 10476 KB answer = YES
26 Correct 87 ms 10612 KB answer = YES
27 Correct 85 ms 10472 KB answer = YES
28 Correct 91 ms 10484 KB answer = YES
29 Correct 91 ms 10596 KB answer = YES
30 Correct 83 ms 10472 KB answer = NO
31 Correct 87 ms 10392 KB answer = YES
32 Correct 89 ms 10684 KB answer = YES
33 Correct 85 ms 10600 KB answer = YES
34 Correct 91 ms 10488 KB answer = YES
35 Correct 86 ms 10468 KB answer = YES
36 Correct 91 ms 10700 KB answer = YES
37 Correct 101 ms 10752 KB answer = YES
38 Correct 97 ms 10852 KB answer = YES
39 Correct 119 ms 10876 KB answer = YES
40 Correct 124 ms 11616 KB answer = YES
41 Correct 112 ms 11248 KB answer = NO
42 Correct 128 ms 11520 KB answer = YES
43 Correct 125 ms 12012 KB answer = YES
44 Correct 129 ms 11584 KB answer = YES
45 Correct 130 ms 11880 KB answer = YES
46 Correct 117 ms 11292 KB answer = YES
47 Correct 128 ms 12016 KB answer = YES
48 Correct 129 ms 12148 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10344 KB answer = YES
2 Correct 81 ms 10596 KB answer = YES
3 Correct 83 ms 10488 KB answer = YES
4 Correct 81 ms 10232 KB answer = NO
5 Correct 82 ms 10556 KB answer = YES
6 Correct 82 ms 10584 KB answer = YES
7 Correct 83 ms 10492 KB answer = YES
8 Correct 82 ms 10352 KB answer = YES
9 Correct 77 ms 10344 KB answer = NO
10 Correct 83 ms 10232 KB answer = YES
11 Correct 80 ms 10356 KB answer = YES
12 Correct 79 ms 10232 KB answer = NO
13 Correct 84 ms 10224 KB answer = YES
14 Correct 83 ms 10596 KB answer = YES
15 Correct 83 ms 10484 KB answer = YES
16 Correct 81 ms 10472 KB answer = YES
17 Correct 83 ms 10300 KB answer = YES
18 Correct 83 ms 10488 KB answer = YES
19 Correct 84 ms 10472 KB answer = YES
20 Correct 83 ms 10344 KB answer = YES
21 Correct 82 ms 10356 KB answer = YES
22 Correct 77 ms 10360 KB answer = NO
23 Correct 80 ms 10360 KB answer = NO
24 Correct 101 ms 10700 KB answer = YES
25 Correct 83 ms 10476 KB answer = YES
26 Correct 87 ms 10612 KB answer = YES
27 Correct 85 ms 10472 KB answer = YES
28 Correct 91 ms 10484 KB answer = YES
29 Correct 91 ms 10596 KB answer = YES
30 Correct 83 ms 10472 KB answer = NO
31 Correct 87 ms 10392 KB answer = YES
32 Correct 89 ms 10684 KB answer = YES
33 Correct 85 ms 10600 KB answer = YES
34 Correct 91 ms 10488 KB answer = YES
35 Correct 86 ms 10468 KB answer = YES
36 Correct 91 ms 10700 KB answer = YES
37 Correct 101 ms 10752 KB answer = YES
38 Correct 97 ms 10852 KB answer = YES
39 Correct 119 ms 10876 KB answer = YES
40 Correct 124 ms 11616 KB answer = YES
41 Correct 112 ms 11248 KB answer = NO
42 Correct 128 ms 11520 KB answer = YES
43 Correct 125 ms 12012 KB answer = YES
44 Correct 129 ms 11584 KB answer = YES
45 Correct 130 ms 11880 KB answer = YES
46 Correct 117 ms 11292 KB answer = YES
47 Correct 128 ms 12016 KB answer = YES
48 Correct 129 ms 12148 KB answer = YES
49 Correct 320 ms 21428 KB answer = YES
50 Correct 310 ms 20216 KB answer = YES
51 Correct 353 ms 21596 KB answer = YES
52 Correct 199 ms 17728 KB answer = NO
53 Correct 136 ms 11612 KB answer = YES
54 Correct 204 ms 14152 KB answer = YES
55 Correct 285 ms 17212 KB answer = YES
56 Correct 357 ms 21772 KB answer = YES
57 Correct 328 ms 21328 KB answer = YES
58 Correct 323 ms 21284 KB answer = YES
59 Correct 308 ms 19744 KB answer = YES
60 Correct 356 ms 22228 KB answer = YES
61 Correct 247 ms 16440 KB answer = YES
62 Correct 561 ms 55224 KB answer = NO
63 Correct 681 ms 56648 KB answer = YES
64 Correct 637 ms 57816 KB answer = NO
65 Correct 686 ms 60124 KB answer = YES
66 Correct 168 ms 13672 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 82 ms 10344 KB answer = YES
2 Correct 81 ms 10596 KB answer = YES
3 Correct 83 ms 10488 KB answer = YES
4 Correct 81 ms 10232 KB answer = NO
5 Correct 82 ms 10556 KB answer = YES
6 Correct 82 ms 10584 KB answer = YES
7 Correct 83 ms 10492 KB answer = YES
8 Correct 82 ms 10352 KB answer = YES
9 Correct 77 ms 10344 KB answer = NO
10 Correct 83 ms 10232 KB answer = YES
11 Correct 80 ms 10356 KB answer = YES
12 Correct 79 ms 10232 KB answer = NO
13 Correct 84 ms 10224 KB answer = YES
14 Correct 83 ms 10596 KB answer = YES
15 Correct 83 ms 10484 KB answer = YES
16 Correct 81 ms 10472 KB answer = YES
17 Correct 83 ms 10300 KB answer = YES
18 Correct 83 ms 10488 KB answer = YES
19 Correct 84 ms 10472 KB answer = YES
20 Correct 83 ms 10344 KB answer = YES
21 Correct 82 ms 10356 KB answer = YES
22 Correct 77 ms 10360 KB answer = NO
23 Correct 80 ms 10360 KB answer = NO
24 Correct 101 ms 10700 KB answer = YES
25 Correct 83 ms 10476 KB answer = YES
26 Correct 87 ms 10612 KB answer = YES
27 Correct 85 ms 10472 KB answer = YES
28 Correct 91 ms 10484 KB answer = YES
29 Correct 91 ms 10596 KB answer = YES
30 Correct 83 ms 10472 KB answer = NO
31 Correct 87 ms 10392 KB answer = YES
32 Correct 89 ms 10684 KB answer = YES
33 Correct 85 ms 10600 KB answer = YES
34 Correct 91 ms 10488 KB answer = YES
35 Correct 86 ms 10468 KB answer = YES
36 Correct 91 ms 10700 KB answer = YES
37 Correct 101 ms 10752 KB answer = YES
38 Correct 97 ms 10852 KB answer = YES
39 Correct 119 ms 10876 KB answer = YES
40 Correct 124 ms 11616 KB answer = YES
41 Correct 112 ms 11248 KB answer = NO
42 Correct 128 ms 11520 KB answer = YES
43 Correct 125 ms 12012 KB answer = YES
44 Correct 129 ms 11584 KB answer = YES
45 Correct 130 ms 11880 KB answer = YES
46 Correct 117 ms 11292 KB answer = YES
47 Correct 128 ms 12016 KB answer = YES
48 Correct 129 ms 12148 KB answer = YES
49 Correct 320 ms 21428 KB answer = YES
50 Correct 310 ms 20216 KB answer = YES
51 Correct 353 ms 21596 KB answer = YES
52 Correct 199 ms 17728 KB answer = NO
53 Correct 136 ms 11612 KB answer = YES
54 Correct 204 ms 14152 KB answer = YES
55 Correct 285 ms 17212 KB answer = YES
56 Correct 357 ms 21772 KB answer = YES
57 Correct 328 ms 21328 KB answer = YES
58 Correct 323 ms 21284 KB answer = YES
59 Correct 308 ms 19744 KB answer = YES
60 Correct 356 ms 22228 KB answer = YES
61 Correct 247 ms 16440 KB answer = YES
62 Correct 561 ms 55224 KB answer = NO
63 Correct 681 ms 56648 KB answer = YES
64 Correct 637 ms 57816 KB answer = NO
65 Correct 686 ms 60124 KB answer = YES
66 Correct 168 ms 13672 KB answer = YES
67 Execution timed out 1018 ms 68268 KB Time limit exceeded
68 Halted 0 ms 0 KB -