CSES - Datatähti Open 2017 - Results
Submission details
Task:Tunnels
Sender:ruhanhabib39
Submission time:2017-01-20 18:06:16 +0200
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.24 s1details
#2ACCEPTED0.25 s1details
#30.24 s1details
#40.25 s1details
#50.23 s1details
#60.24 s2details
#70.25 s2details
#80.23 s2details
#90.23 s2details
#100.23 s2details
#110.98 s3details
#120.70 s3details
#130.54 s3details
#140.56 s3details
#150.46 s3details

Code

import java.io.*;
import java.util.*;

public class C {
   public static void main(String[] args) throws IOException {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
      (new C()).solve(in, out);
      out.close();
   }

   private int n, m;
   private ArrayList<ArrayList<Integer>> next;
   private int[] cc;
   private boolean[] vis;

   private void input(BufferedReader in, PrintWriter out) throws IOException {
      String[] tok = in.readLine().split(" ");
      n = Integer.parseInt(tok[0]);
      m = Integer.parseInt(tok[1]);
      next = new ArrayList<ArrayList<Integer>>(n+1);
      for(int i = 0; i <= n; i++) {
         next.add(new ArrayList<Integer>());
      }
      for(int i = 0; i < m; i++) {
         tok = in.readLine().split(" ");
         int u = Integer.parseInt(tok[0]);
         int v = Integer.parseInt(tok[1]);
         next.get(u).add(v);
      }
   }

   void dfs(int u) {
      if(cc[u] != -1) return;
      cc[u] = 1;
      for(Integer v : next.get(u)) {
         dfs(v);
         cc[u] += cc[v];
      }
   }

   int calc(int u) {
      if(vis[u]) return 0;
      vis[u] = true;
      int res = 0;
      for(Integer v : next.get(u)) {
         if(!vis[v]) {
            res += calc(v);
         }
      }
      return Integer.max(1, res);
   }

   public void solve(BufferedReader in, PrintWriter out) throws IOException {
      input(in, out);
      cc = new int[n + 1];
      vis = new boolean[n + 1];
      Arrays.fill(cc, -1);
      Arrays.fill(vis, false);
      for(int u = 1; u <= n; u++) {
         dfs(u);
      }
      Integer[] arr = new Integer[n];
      for(int i = 0; i < n; i++) {
         arr[i] = i+1;
      }
      Arrays.sort(arr,
         (u, v) -> cc[v] - cc[u]);
      int res = 0;
      for(int i = 0; i < n; i++) {
         res += calc(arr[i]);
      }
      out.println(res);
   }
}

Test details

Test 1

Group: 1

Verdict:

input
10 20
4 5
6 4
5 1
5 9
...

correct output
11

user output
6

Test 2

Group: 1

Verdict: ACCEPTED

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

correct output
5

user output
5

Test 3

Group: 1

Verdict:

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

correct output
4

user output
8

Test 4

Group: 1

Verdict:

input
10 4
9 1
6 8
7 1
5 7

correct output
3

user output
7

Test 5

Group: 1

Verdict:

input
10 2
10 6
2 1

correct output
2

user output
8

Test 6

Group: 2

Verdict:

input
100 200
24 40
25 6
36 93
92 90
...

correct output
97

user output
51

Test 7

Group: 2

Verdict:

input
100 100
98 37
91 37
60 92
46 27
...

correct output
60

user output
59

Test 8

Group: 2

Verdict:

input
100 50
74 95
53 72
69 85
14 13
...

correct output
34

user output
74

Test 9

Group: 2

Verdict:

input
100 40
28 76
10 81
13 52
46 83
...

correct output
29

user output
78

Test 10

Group: 2

Verdict:

input
100 20
27 35
72 92
56 4
64 80
...

correct output
18

user output
81

Test 11

Group: 3

Verdict:

input
100000 200000
89244 59358
22943 56710
63331 89437
56581 38400
...

correct output
102510

user output
57087

Test 12

Group: 3

Verdict:

input
100000 100000
21701 85599
61542 21474
38081 29362
46316 64038
...

correct output
60593

user output
64082

Test 13

Group: 3

Verdict:

input
100000 50000
86469 4833
16351 35505
59315 33011
95464 16985
...

correct output
35933

user output
73393

Test 14

Group: 3

Verdict:

input
100000 40000
5392 23534
63204 45619
74330 25925
59678 88427
...

correct output
30074

user output
76354

Test 15

Group: 3

Verdict:

input
100000 20000
80156 16531
71753 77661
7028 33389
17168 646
...

correct output
16882

user output
84864