Submission #4672371


Source Code Expand

Copy
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set;
import java.util.Random;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        static List<TaskC.Vertex> curCycle = new ArrayList<>();

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            TaskC.Vertex done = new TaskC.Vertex();
            int n = in.nextInt();
            int m = in.nextInt();
            TaskC.Vertex[] vs = new TaskC.Vertex[n];
            for (int i = 0; i < n; ++i) {
                vs[i] = new TaskC.Vertex();
            }
            for (int i = 0; i < m; ++i) {
                TaskC.Vertex a = vs[in.nextInt() - 1];
                TaskC.Vertex b = vs[in.nextInt() - 1];
                a.adjSeq.add(b);
                b.adjSeq.add(a);
            }
            for (TaskC.Vertex v : vs)
                if (v.adjSeq.size() % 2 != 0) {
                    out.println("No");
                    return;
                }
            long finish = System.currentTimeMillis() + 1500;
            Random random = new Random(55413543513L);
            List<TaskC.Vertex> vList = new ArrayList<>();
            for (TaskC.Vertex v : vs) {
                vList.add(v);
            }
            outer2:
            while (System.currentTimeMillis() < finish) {
                List<List<TaskC.Vertex>> cycles = new ArrayList<>();
                for (TaskC.Vertex v : vs) {
                    v.adj.clear();
                    v.adj.addAll(v.adjSeq);
                    Collections.shuffle(v.adjSeq, random);
                }
                Collections.shuffle(vList, random);
                outer:
                for (int i = 0; i < 3; ++i) {
                    curCycle = new ArrayList<>();
                    for (TaskC.Vertex v : vList) v.state = 0;
                    for (TaskC.Vertex v : vList)
                        if (!v.adj.isEmpty()) {
                            if (v.removeCycle(null, done) != done) throw new RuntimeException();
                            cycles.add(curCycle);
                            continue outer;
                        }
                    break;
                }
                if (cycles.size() <= 1) {
                    out.println("No");
                } else if (cycles.size() >= 3) {
                    out.println("Yes");
                } else {
                    int bigDeg = 0;
                    for (TaskC.Vertex v : vList) {
                        if (v.adjSeq.size() > 2)
                            ++bigDeg;
                    }
                    if (bigDeg >= 3) {
                        out.println("Yes");
                    } else {
                        out.println("No");
                    }
                }


                return;
            }
            out.println("No");
        }

        static class Vertex {
            int state = 0;
            Set<TaskC.Vertex> adj = new HashSet<>();
            List<TaskC.Vertex> adjSeq = new ArrayList<>();

            public TaskC.Vertex removeCycle(TaskC.Vertex parent, TaskC.Vertex done) {
                state = 1;
                for (TaskC.Vertex v : adjSeq)
                    if (v != parent && adj.contains(v)) {
                        if (v.state == 1) {
                            adj.remove(v);
                            v.adj.remove(this);
                            return v;
                        } else if (v.state == 0) {
                            TaskC.Vertex got = v.removeCycle(this, done);
                            if (got == done) {
                                return done;
                            }
                            if (got != null) {
                                adj.remove(v);
                                v.adj.remove(this);
                                curCycle.add(this);
                                if (got == this) return done;
                                else return got;
                            }
                        }
                    }
                state = 2;
                return null;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

Submission Info

Submission Time
Task C - Three Circuits
User Petr
Language Java8 (OpenJDK 1.8.0)
Score 800
Code Size 5768 Byte
Status
Exec Time 786 ms
Memory 106440 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt
All 800 / 800 sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt
Case Name Status Exec Time Memory
sample_01.txt 72 ms 20052 KB
sample_02.txt 72 ms 20564 KB
sample_03.txt 77 ms 21972 KB
test_01.txt 247 ms 50636 KB
test_02.txt 171 ms 29924 KB
test_03.txt 73 ms 20052 KB
test_04.txt 570 ms 93992 KB
test_05.txt 268 ms 50736 KB
test_06.txt 444 ms 59228 KB
test_07.txt 72 ms 19924 KB
test_08.txt 74 ms 19156 KB
test_09.txt 786 ms 106440 KB
test_10.txt 743 ms 104956 KB
test_11.txt 310 ms 53472 KB
test_12.txt 306 ms 54136 KB
test_13.txt 314 ms 52172 KB
test_14.txt 310 ms 49108 KB
test_15.txt 385 ms 64760 KB
test_16.txt 371 ms 61504 KB
test_17.txt 272 ms 49180 KB
test_18.txt 287 ms 49720 KB
test_19.txt 366 ms 64388 KB
test_20.txt 420 ms 59824 KB
test_21.txt 377 ms 64620 KB
test_22.txt 273 ms 49956 KB
test_23.txt 105 ms 19668 KB
test_24.txt 74 ms 20308 KB
test_25.txt 75 ms 20820 KB
test_26.txt 74 ms 19284 KB
test_27.txt 367 ms 62808 KB
test_28.txt 417 ms 63660 KB
test_29.txt 301 ms 50736 KB
test_30.txt 300 ms 50296 KB
test_31.txt 477 ms 61904 KB
test_32.txt 380 ms 62328 KB
test_33.txt 343 ms 62016 KB
test_34.txt 429 ms 62240 KB
test_35.txt 355 ms 61824 KB
test_36.txt 382 ms 60316 KB
test_37.txt 364 ms 60348 KB
test_38.txt 388 ms 64528 KB
test_39.txt 73 ms 18516 KB
test_40.txt 681 ms 103224 KB
test_41.txt 383 ms 62168 KB
test_42.txt 364 ms 65296 KB
test_43.txt 368 ms 57816 KB
test_44.txt 357 ms 54696 KB
test_45.txt 354 ms 56656 KB
test_46.txt 341 ms 53764 KB
test_47.txt 364 ms 52452 KB
test_48.txt 357 ms 57156 KB
test_49.txt 386 ms 55672 KB
test_50.txt 366 ms 56680 KB
test_51.txt 336 ms 54588 KB
test_52.txt 349 ms 56076 KB
test_53.txt 172 ms 37460 KB
test_54.txt 205 ms 49248 KB
test_55.txt 215 ms 40604 KB
test_56.txt 185 ms 32872 KB
test_57.txt 208 ms 41848 KB
test_58.txt 186 ms 38952 KB
test_59.txt 213 ms 40616 KB