{"id":"3670872a-5279-473d-9060-7a174684fab5","name":"Tree And Extra Edge","description":"<a href=\"https://www.codechef.com/problems/TREEDGE\">https://www.codechef.com/problems/TREEDGE</a>","inputFormat":".","outputFormat":".","constraints":".","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static InputReader in = new InputReader(System.in);\r\n static PrintWriter out = new PrintWriter(System.out);\r\n /*\r\n input functions in.nextInt(); in.nextIntArray(); in.nextIntArray1();\r\n output fnctions out.println(); out.print();\r\n\r\n use in for reading input\r\n use out for printing output\r\n */\r\n\r\n\r\n public static void main(String[] args) throws IOException {\r\n\r\n // write your code here.\r\n\r\n\r\n out.close();\r\n }\r\n\r\n private static class InputReader implements AutoCloseable {\r\n\r\n private static final int DEFAULT_BUFFER_SIZE = 1 << 16;\r\n\r\n\r\n private static final InputStream DEFAULT_STREAM = System.in;\r\n\r\n private static final int MAX_DECIMAL_PRECISION = 21;\r\n\r\n private int c;\r\n\r\n private final byte[] buf;\r\n private final int bufferSize;\r\n private int bufIndex;\r\n private int numBytesRead;\r\n\r\n private final InputStream stream;\r\n\r\n private static final byte EOF = -1;\r\n\r\n private static final byte NEW_LINE = 10;\r\n\r\n private static final byte SPACE = 32;\r\n\r\n private static final byte DASH = 45;\r\n\r\n private static final byte DOT = 46;\r\n private char[] charBuffer;\r\n private static final byte[] bytes = new byte[58];\r\n private static final int[] ints = new int[58];\r\n private static final char[] chars = new char[128];\r\n\r\n static {\r\n char ch = '' '';\r\n int value = 0;\r\n byte _byte = 0;\r\n for (int i = 48; i < 58; i++)\r\n bytes[i] = _byte++;\r\n for (int i = 48; i < 58; i++)\r\n ints[i] = value++;\r\n for (int i = 32; i < 128; i++)\r\n chars[i] = ch++;\r\n }\r\n\r\n public InputReader() {\r\n this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(int bufferSize) {\r\n this(DEFAULT_STREAM, bufferSize);\r\n }\r\n public InputReader(InputStream stream) {\r\n this(stream, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(InputStream stream, int bufferSize) {\r\n if (stream == null || bufferSize <= 0)\r\n throw new IllegalArgumentException();\r\n buf = new byte[bufferSize];\r\n charBuffer = new char[128];\r\n this.bufferSize = bufferSize;\r\n this.stream = stream;\r\n }\r\n private byte read() throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n throw new IOException();\r\n\r\n if (bufIndex >= numBytesRead) {\r\n bufIndex = 0;\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n }\r\n\r\n return buf[bufIndex++];\r\n }\r\n private int readJunk(int token) throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > token)\r\n return 0;\r\n bufIndex++;\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public byte nextByte() throws IOException {\r\n return (byte) nextInt();\r\n }\r\n public int nextInt() throws IOException {\r\n\r\n if (readJunk(DASH - 1) == EOF)\r\n throw new IOException();\r\n int sgn = 1, res = 0;\r\n\r\n c = buf[bufIndex];\r\n if (c == DASH) {\r\n sgn = -1;\r\n bufIndex++;\r\n }\r\n\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > SPACE) {\r\n res = (res << 3) + (res << 1);\r\n res += ints[buf[bufIndex++]];\r\n } else {\r\n bufIndex++;\r\n return res * sgn;\r\n }\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return res * sgn;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public int[] nextIntArray(int n) throws IOException {\r\n int[] ar = new int[n];\r\n for (int i = 0; i < n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public int[] nextIntArray1(int n) throws IOException {\r\n int[] ar = new int[n + 1];\r\n for (int i = 1; i <= n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public void close() throws IOException {\r\n stream.close();\r\n }\r\n\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"1\r\n7 3\r\n1 2 1\r\n1 3 -2\r\n2 4 3\r\n2 5 -4\r\n5 7 5\r\n3 6 6\r\n2 3 1\r\n5 4 2\r\n5 6 0","sampleOutput":"10\r\n7\r\n5","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ebd4bd7d-3745-4e3b-9603-da905237841b","name":"Binary Lifting For Experts","slug":"binary-lifting-for-experts-971","type":0},{"id":"83ae5fef-9140-4730-980b-b9dcf39c2799","name":"Tree And Extra Edge","slug":"tree-and-extra-edge","type":1}],"next":null,"prev":{"id":"6b8f1dd2-aad0-4142-8531-d8402642326c","name":"Longest Good Segment","type":1,"slug":"longest-good-segment"}}}

Tree And Extra Edge

<a href="https://www.codechef.com/problems/TREEDGE">https://www.codechef.com/problems/TREEDGE</a>

{"id":"3670872a-5279-473d-9060-7a174684fab5","name":"Tree And Extra Edge","description":"<a href=\"https://www.codechef.com/problems/TREEDGE\">https://www.codechef.com/problems/TREEDGE</a>","inputFormat":".","outputFormat":".","constraints":".","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n static InputReader in = new InputReader(System.in);\r\n static PrintWriter out = new PrintWriter(System.out);\r\n /*\r\n input functions in.nextInt(); in.nextIntArray(); in.nextIntArray1();\r\n output fnctions out.println(); out.print();\r\n\r\n use in for reading input\r\n use out for printing output\r\n */\r\n\r\n\r\n public static void main(String[] args) throws IOException {\r\n\r\n // write your code here.\r\n\r\n\r\n out.close();\r\n }\r\n\r\n private static class InputReader implements AutoCloseable {\r\n\r\n private static final int DEFAULT_BUFFER_SIZE = 1 << 16;\r\n\r\n\r\n private static final InputStream DEFAULT_STREAM = System.in;\r\n\r\n private static final int MAX_DECIMAL_PRECISION = 21;\r\n\r\n private int c;\r\n\r\n private final byte[] buf;\r\n private final int bufferSize;\r\n private int bufIndex;\r\n private int numBytesRead;\r\n\r\n private final InputStream stream;\r\n\r\n private static final byte EOF = -1;\r\n\r\n private static final byte NEW_LINE = 10;\r\n\r\n private static final byte SPACE = 32;\r\n\r\n private static final byte DASH = 45;\r\n\r\n private static final byte DOT = 46;\r\n private char[] charBuffer;\r\n private static final byte[] bytes = new byte[58];\r\n private static final int[] ints = new int[58];\r\n private static final char[] chars = new char[128];\r\n\r\n static {\r\n char ch = '' '';\r\n int value = 0;\r\n byte _byte = 0;\r\n for (int i = 48; i < 58; i++)\r\n bytes[i] = _byte++;\r\n for (int i = 48; i < 58; i++)\r\n ints[i] = value++;\r\n for (int i = 32; i < 128; i++)\r\n chars[i] = ch++;\r\n }\r\n\r\n public InputReader() {\r\n this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(int bufferSize) {\r\n this(DEFAULT_STREAM, bufferSize);\r\n }\r\n public InputReader(InputStream stream) {\r\n this(stream, DEFAULT_BUFFER_SIZE);\r\n }\r\n public InputReader(InputStream stream, int bufferSize) {\r\n if (stream == null || bufferSize <= 0)\r\n throw new IllegalArgumentException();\r\n buf = new byte[bufferSize];\r\n charBuffer = new char[128];\r\n this.bufferSize = bufferSize;\r\n this.stream = stream;\r\n }\r\n private byte read() throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n throw new IOException();\r\n\r\n if (bufIndex >= numBytesRead) {\r\n bufIndex = 0;\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n }\r\n\r\n return buf[bufIndex++];\r\n }\r\n private int readJunk(int token) throws IOException {\r\n\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > token)\r\n return 0;\r\n bufIndex++;\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return EOF;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public byte nextByte() throws IOException {\r\n return (byte) nextInt();\r\n }\r\n public int nextInt() throws IOException {\r\n\r\n if (readJunk(DASH - 1) == EOF)\r\n throw new IOException();\r\n int sgn = 1, res = 0;\r\n\r\n c = buf[bufIndex];\r\n if (c == DASH) {\r\n sgn = -1;\r\n bufIndex++;\r\n }\r\n\r\n do {\r\n\r\n while (bufIndex < numBytesRead) {\r\n if (buf[bufIndex] > SPACE) {\r\n res = (res << 3) + (res << 1);\r\n res += ints[buf[bufIndex++]];\r\n } else {\r\n bufIndex++;\r\n return res * sgn;\r\n }\r\n }\r\n numBytesRead = stream.read(buf);\r\n if (numBytesRead == EOF)\r\n return res * sgn;\r\n bufIndex = 0;\r\n\r\n } while (true);\r\n\r\n }\r\n public int[] nextIntArray(int n) throws IOException {\r\n int[] ar = new int[n];\r\n for (int i = 0; i < n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public int[] nextIntArray1(int n) throws IOException {\r\n int[] ar = new int[n + 1];\r\n for (int i = 1; i <= n; i++)\r\n ar[i] = nextInt();\r\n return ar;\r\n }\r\n public void close() throws IOException {\r\n stream.close();\r\n }\r\n\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"1\r\n7 3\r\n1 2 1\r\n1 3 -2\r\n2 4 3\r\n2 5 -4\r\n5 7 5\r\n3 6 6\r\n2 3 1\r\n5 4 2\r\n5 6 0","sampleOutput":"10\r\n7\r\n5","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ebd4bd7d-3745-4e3b-9603-da905237841b","name":"Binary Lifting For Experts","slug":"binary-lifting-for-experts-971","type":0},{"id":"83ae5fef-9140-4730-980b-b9dcf39c2799","name":"Tree And Extra Edge","slug":"tree-and-extra-edge","type":1}],"next":null,"prev":{"id":"6b8f1dd2-aad0-4142-8531-d8402642326c","name":"Longest Good Segment","type":1,"slug":"longest-good-segment"}}}
plane

Editor


Loading...

Tree And Extra Edge

hard

https://www.codechef.com/problems/TREEDGE

Constraints

.

Format

Input

.

Output

.

Example

Sample Input

1 7 3 1 2 1 1 3 -2 2 4 3 2 5 -4 5 7 5 3 6 6 2 3 1 5 4 2 5 6 0

Sample Output

10 7 5

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode