Submission details
Task:Fragile network
Sender:aalto26fw_005
Submission time:2026-10-07 17:38:03 +0300
Language:Java
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.09 sdetails
#2ACCEPTED0.09 sdetails
#3ACCEPTED0.09 sdetails
#4ACCEPTED0.08 sdetails
#5ACCEPTED0.09 sdetails
#6ACCEPTED0.84 sdetails
#7ACCEPTED0.31 sdetails
#8ACCEPTED0.67 sdetails
#90.35 sdetails
#10ACCEPTED0.33 sdetails
#110.09 sdetails
#120.09 sdetails
#13ACCEPTED0.09 sdetails
#14ACCEPTED0.54 sdetails
#15ACCEPTED0.09 sdetails
#16ACCEPTED0.09 sdetails
#17ACCEPTED0.09 sdetails
#18ACCEPTED0.09 sdetails
#19ACCEPTED0.09 sdetails
#20ACCEPTED0.09 sdetails
#21ACCEPTED0.09 sdetails

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class FragileNetwork {
    public static void main(String[] args) {
        FastScanner input = new FastScanner(System.in);
        int n = input.nextInt();

        int[] nodeCounts = new int[n+1];

        for (int i = 1; i < n; i++){
            nodeCounts[input.nextInt()] += 1;
            nodeCounts[input.nextInt()] += 1;
        }

        List<Integer> lowCon = new ArrayList<Integer>();
        for (int i = 1; i <= n; i++){
            if (nodeCounts[i] == 1) {
                lowCon.add(i);
            }
        }

        int length = lowCon.size();
        if (length%2 == 0){
            System.out.println(length/2);
        } else {
            System.out.println(length/2 + 1);
        }

        Collections.shuffle(lowCon);

        int i = 0;
        while (i < length - 1){
            System.out.println(lowCon.get(i) + " " + lowCon.get(i+1));
            i += 2;
        }
        if(i == length - 1){
            System.out.println(lowCon.get(i-1) + " " + lowCon.get(i));
        }
    }

    static class FastScanner { //static class fastScanner
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

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

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

        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

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

    public double nextDouble() {
    	 return Double.parseDouble(next());
	 }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }
}
}

Test details

Test 1

Verdict: ACCEPTED

input
10
1 5
1 7
1 8
1 3
...

correct output
5
5 2
7 9
8 6
3 10
...

user output
5
10 6
7 5
8 4
2 3
...

Test 2

Verdict: ACCEPTED

input
10
4 5
3 4
2 3
9 10
...

correct output
1
10 1

user output
1
1 10

Test 3

Verdict: ACCEPTED

input
10
1 8
1 3
3 5
5 7
...

correct output
3
7 10
8 2
1 9

user output
3
10 7
9 8
8 2

Test 4

Verdict: ACCEPTED

input
10
1 5
3 7
2 10
3 8
...

correct output
3
10 8
6 4
5 9

user output
3
5 9
6 10
8 4

Test 5

Verdict: ACCEPTED

input
10
4 8
3 4
4 6
2 3
...

correct output
3
8 7
10 9
1 6

user output
3
9 6
7 10
10 8

Test 6

Verdict: ACCEPTED

input
100000
1 56967
1 56618
1 42321
1 82550
...

correct output
50000
56967 16911
56618 39942
42321 99902
82550 2538
...

user output
50000
90003 4328
2273 8774
21495 2810
52466 54244
...

Test 7

Verdict: ACCEPTED

input
100000
92297 92298
23511 23512
68057 68058
65434 65435
...

correct output
1
100000 1

user output
1
1 100000

Test 8

Verdict: ACCEPTED

input
100000
17747 97512
10397 12053
679 6975
4013 14565
...

correct output
25057
92881 76094
20353 87429
16069 96487
71186 52809
...

user output
25057
98157 30156
97440 82488
98376 89014
40331 87039
...

Test 9

Verdict:

input
100000
72941 72942
11232 11233
73464 73465
30042 30043
...

correct output
489
16423 85168
20707 94190
36505 54940
96411 44067
...

user output
489
38235 88353
29418 71163
47516 94749
44678 31055
...

Test 10

Verdict: ACCEPTED

input
100000
31451 31452
7473 7474
24056 24057
85181 85182
...

correct output
51
25638 2983
87594 87371
92001 50610
46744 100000
...

user output
51
87371 42465
51374 37058
79428 32629
22961 50610
...

Test 11

Verdict:

input
10
1 2
1 3
3 4
3 5
...

correct output
2
2 6
4 10

user output
2
4 6
10 2

Test 12

Verdict:

input
7
1 2
2 3
2 4
1 5
...

correct output
2
4 7
3 6

user output
2
7 6
3 4

Test 13

Verdict: ACCEPTED

input
6
1 2
1 3
1 4
4 5
...

correct output
2
3 6
2 5

user output
2
5 2
3 6

Test 14

Verdict: ACCEPTED

input
65538
1 2
1 3
1 4
3 5
...

correct output
16385
34 36
40 42
35 41
48 50
...

user output
16385
61811 34376
20311 39914
21189 38296
63588 45084
...

Test 15

Verdict: ACCEPTED

input
11
1 2
1 3
2 4
2 5
...

correct output
2
9 11
8 10

user output
2
8 10
11 9

Test 16

Verdict: ACCEPTED

input
7
1 2
1 3
2 4
2 5
...

correct output
2
5 7
4 6

user output
2
5 7
6 4

Test 17

Verdict: ACCEPTED

input
7
1 2
1 3
2 4
2 5
...

correct output
2
5 7
4 6

user output
2
7 4
5 6

Test 18

Verdict: ACCEPTED

input
10
8 4
3 4
4 6
2 3
...

correct output
3
8 7
10 9
1 6

user output
3
10 6
8 9
9 7

Test 19

Verdict: ACCEPTED

input
7
1 2
1 5
2 3
2 6
...

correct output
2
6 7
3 4

user output
2
7 3
4 6

Test 20

Verdict: ACCEPTED

input
8
1 2
1 3
2 4
2 5
...

correct output
3
4 7
6 8
1 5

user output
3
4 8
7 5
5 6

Test 21

Verdict: ACCEPTED

input
10
2 1
3 1
4 2
5 4
...

correct output
3
9 8
6 10
3 7

user output
3
9 3
6 10
7 8