exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
βŒ€
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
βŒ€
difficulty
int64
-1
3.5k
βŒ€
prob_desc_output_spec
stringlengths
17
1.47k
βŒ€
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
359eb52ff3f7032231f45ea28692374a
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class file{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int m = scn.nextInt(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for(int i=0; i<n; i++){ graph.add(new ArrayList<Integer>()); } for(int i=0; i<m; i++){ int u = scn.nextInt(); int v = scn.nextInt(); graph.get(u-1).add(v-1); graph.get(v-1).add(u-1); } if(bus(graph)){ System.out.println("bus topology"); }else if(star(graph)){ System.out.println("star topology"); }else if(ring(graph)){ System.out.println("ring topology"); }else{ System.out.println("unknown topology"); } } public static boolean bus(ArrayList<ArrayList<Integer>> graph){ int c1e = 0, c2e = 0; for(int i=0; i<graph.size(); i++){ if(graph.get(i).size() == 1){ c1e++; }else if(graph.get(i).size() == 2){ c2e++; }else{ return false; } } if(c1e == 2 && c2e == graph.size() - 2){ return true; } return false; } public static boolean ring(ArrayList<ArrayList<Integer>> graph){ int c2e = 0; for(int i=0; i<graph.size(); i++){ if(graph.get(i).size() == 2){ c2e++; }else{ return false; } } if(c2e == graph.size()){ return true; } return false; } public static boolean star(ArrayList<ArrayList<Integer>> graph){ int c1e = 0, cnm1e = 0; for(int i=0; i<graph.size(); i++){ if(graph.get(i).size() == 1){ c1e++; }else if(graph.get(i).size() == graph.size() - 1){ cnm1e++; }else{ return false; } } if(c1e == graph.size() - 1 && cnm1e == 1){ return true; } return false; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
65c418b6f5c70526eae7351a4a935e9b
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class network{ public static void main(String[] args){ Scanner scn=new Scanner(System.in); int n=scn.nextInt(); int m=scn.nextInt(); ArrayList<ArrayList<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<Integer>()); } for(int i=0;i<m;i++){ int u=scn.nextInt(); int v=scn.nextInt(); graph.get(u).add(v); graph.get(v).add(u); } solve(graph,n,m); } public static void solve(ArrayList<ArrayList<Integer>> graph,int n,int m){ int c1=0; int c2=0; int cn1=0; for(int i=1;i<graph.size();i++){ if(graph.get(i).size()==1){ c1++; }else if(graph.get(i).size()==2){ c2++; }else if(graph.get(i).size()==n-1){ cn1++; } } if(c1==2&&c2==n-2&&cn1==0){ System.out.println("bus topology"); }else if(c1==0&&c2==n&&cn1==0){ System.out.println("ring topology"); }else if(c1==n-1&&c2==0&&cn1==1){ System.out.println("star topology"); }else{ System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
2259114b29be9408e5465332ded0aefb
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import java.math.MathContext; import java.text.DecimalFormat; import java.text.Format; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.awt.List; public class Main { public static void main(String[] args){ InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { //public ArrayList<Integer> ar; public boolean[] bs; // GCD && LCM long gcd(long a ,long b){return b == 0 ? a : gcd(b , a % b);} long lcm(long a , long b){return a*(b/gcd(a, b));} // Sieve of erothenese void sieve(int UB){ // ar = new ArrayList(); bs = new boolean[UB+1]; bs[0] = bs[1] = true; for(int i = 2; i*i <= UB; i++){ if(!bs[i]){ for(int j = i*i ; j <= UB ; j += i) bs[j] = true; } } /* for(int i = 2; i <= UB; i++){ if(!bs[i])ar.add(i); }*/ } // REverse a String String rev(String s){ return new StringBuilder(s).reverse().toString(); } /* SOLUTION IS RIGHT HERE */ public void solve(int testNumber, InputReader in, PrintWriter out) { // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Scanner sc = new Scanner(System.in); int n = in.nextInt() , m = in.nextInt() ; int[] node = new int[n+1]; for(int i = 0; i < m; i++){ node[in.nextInt()]++; node[in.nextInt()]++; } int one = 0, two = 0, n_1 = 0; for(int i = 1; i <= n; i++){ if(node[i] == 1)one++; else if(node[i] == 2)two++; else if(node[i] == n-1)n_1++; } if(one == 2 && two == n-2 && n_1 == 0) out.println("bus topology" ); else if(one == 0 && two == n && n_1 == 0) out.println("ring topology" ); else if(one == n-1 && two == 0 && n_1 == 1) out.println("star topology"); else out.println("unknown topology"); } } // pair class static class pair implements Comparable<pair>{ int first , second ; public pair(){this.first = 0; this.second = 0;} public pair(int f , int s){ this.first = f; this.second = s; } @Override public int compareTo(pair one) { if(this.first < one.first) return -1; if(this.first > one.first) return 1; if(this.second < one.second) return -1; if(this.second > one.second) return 1; return 0; } } // ||||||| INPUT READER |||||||| static class InputReader { private byte[] buf = new byte[2048]; private int index; private int total; private InputStream in; public InputReader(InputStream stream){ in = stream; } public int scan(){ if(total == -1) throw new InputMismatchException(); if(index >= total){ index = 0; try{ total = in.read(buf); }catch(IOException e){ throw new InputMismatchException(); } if(total <= 0) return -1; } return buf[index++]; } public long scanlong(){ long integer = 0; int n = scan(); while(isWhiteSpace(n)) n = scan(); int neg = 1; if(n == '-'){ neg = -1; n = scan(); } while(!isWhiteSpace(n)){ if(n >= '0' && n <= '9'){ integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg*integer; } private int scanInt() { int integer = 0; int n = scan(); while(isWhiteSpace(n)) n = scan(); int neg = 1; if(n == '-'){ neg = -1; n = scan(); } while(!isWhiteSpace(n)){ if(n >= '0' && n <= '9'){ integer *= 10; integer += n - '0'; n = scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scandouble(){ double doubll = 0; int n = scan(); int neg = 1; while(isWhiteSpace(n)) n = scan(); if(n == '-'){ neg = -1; n = scan(); } while(!isWhiteSpace(n) && n != '.'){ if(n >= '0' && n <= '9'){ doubll *= 10; doubll += n - '0'; n = scan(); } } if(n == '.'){ n = scan(); double temp = 1; while(!isWhiteSpace(n)){ if(n >= '0' && n <= '9'){ temp /= 10; doubll += (n - '0')*temp; n = scan(); } } } return neg*doubll; } private float scanfloat() { float doubll = 0; int n = scan(); int neg = 1; while(isWhiteSpace(n)) n = scan(); if(n == '-'){ neg = -1; n = scan(); } while(!isWhiteSpace(n) && n != '.'){ if(n >= '0' && n <= '9'){ doubll *= 10; doubll += n - '0'; n = scan(); } } if(n == '.'){ n = scan(); double temp = 1; while(!isWhiteSpace(n)){ if(n >= '0' && n <= '9'){ temp /= 10; doubll += (n - '0')*temp; n = scan(); } } } return neg*doubll; } public String scanstring(){ StringBuilder sb = new StringBuilder(); int n = scan(); while(isWhiteSpace(n)) n = scan(); while(!isWhiteSpace(n)){ sb.append((char)n); n = scan(); } return sb.toString(); } public boolean isWhiteSpace(int n){ if(n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; return false; } /// Input module public int nextInt(){ return scanInt(); } public long nextLong(){ return scanlong(); } public double nextDouble(){ return scandouble(); } public float nextFloat(){ return scanfloat(); } public String next(){ return scanstring(); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
a94fdbde0a233f578c0a8b92b39a11cf
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] inputs = in.readLine().split(" "); int nodes = Integer.parseInt(inputs[0]); int edges = Integer.parseInt(inputs[1]); int[] adjacentCount = new int[nodes + 1]; for (int i =0; i< edges; i++) { String[] str = in.readLine().split(" "); int start = Integer.parseInt(str[0]); int end = Integer.parseInt(str[1]); adjacentCount[start]++; adjacentCount[end]++; } int edgesCount1 = 0; int edgesCount2 = 0; int edgesCountx = 0; for (int i = 1; i < adjacentCount.length; i++) { int adj = adjacentCount[i]; if (adj == 1) edgesCount1++; else if (adj == 2) edgesCount2++; else edgesCountx++; } if (edgesCount2 == nodes) System.out.println("ring topology"); else if (edgesCount1 == 2 && edgesCount2 == nodes - 2) System.out.println("bus topology"); else if (edgesCount1 == nodes - 1 && edgesCountx == 1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
cc384bb4a3acd93d0251b5717291c757
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { private static int n,m; private static int[] numcon; private static boolean tree; private static boolean bus = true; private static boolean star; private static boolean ring; public static void main(String[] args) { InputStream inputStream = System.in; InputReader in = new InputReader(inputStream); n = in.nextInt(); m = in.nextInt(); numcon = new int[n]; tree = n==m+1; for(int i=0;i<n;i++){ numcon[i] = 0; } int a,b; for(int i=0;i<m;i++){ a = in.nextInt(); b = in.nextInt(); numcon[a-1]++; numcon[b-1]++; } if(tree){ for(int i=0;i<n;i++) { if (numcon[i] > 2){ bus = false; break; } } if(bus){ System.out.println("bus topology"); return; } int center = 0; for(int i=0;i<n;i++){ if(numcon[i]!=1) center++; } if(center==1){ System.out.println("star topology"); return; } else{ System.out.println("unknown topology"); return; } }else if(n==m){ for(int i=0;i<n;i++){ if(numcon[i]!=2){ System.out.println("unknown topology"); return; } } System.out.println("ring topology"); }else{ System.out.println("unknown topology"); } } } 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()); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
351305644cbc7699214867624922233f
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import org.omg.CORBA.INTERNAL; import java.io.*; import java.math.BigInteger; import java.util.*; public class Newbie { static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { solver s = new solver(); long t = 1; while (t > 0) { s.solve(); t--; } out.close(); } /* static class descend implements Comparator<pair1> { public int compare(pair1 o1, pair1 o2) { if (o1.pop != o2.pop) return (int) (o1.pop - o2.pop); else return o1.in - o2.in; } }*/ static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); token = null; } public String next() { while (token == null || !token.hasMoreTokens()) { try { token = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static class card { long a; int cnt; int i; public card(long a, int cnt, int i) { this.a = a; this.cnt = cnt; this.i = i; } } static class ascend implements Comparator<pair> { public int compare(pair o1, pair o2) { if (o1.a != o2.a) return (int) (o1.a - o2.a); else return o1.b - o2.b; } } static class extra { static boolean v[] = new boolean[100001]; static List<Integer> l = new ArrayList<>(); static int t; static void shuffle(long a[]) { List<Long> l = new ArrayList<>(); for (int i = 0; i < a.length; i++) l.add(a[i]); Collections.shuffle(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean valid(int i, int j, int r, int c, char ch[][]) { if (i >= 0 && i < r && j >= 0 && j < c && ch[i][j] != '#') { // System.out.println(i+" /// "+j); return true; } else { // System.out.println(i+" //f "+j); return false; } } static void seive() { for (int i = 2; i < 100001; i++) { if (!v[i]) { t++; l.add(i); for (int j = 2 * i; j < 100001; j += i) v[j] = true; } } } static int binary(LinkedList<Integer> a, long val, int n) { int mid = 0, l = 0, r = n - 1, ans = 0; while (l <= r) { mid = (l + r) >> 1; if (a.get(mid) == val) { r = mid - 1; ans = mid; } else if (a.get(mid) > val) r = mid - 1; else { l = mid + 1; ans = l; } } return (ans + 1); } static long fastexpo(int x, int y) { long res = 1; while (y > 0) { if ((y & 1) == 1) { res *= x; } y = y >> 1; x = x * x; } return res; } static long lfastexpo(int x, int y, int p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } /* void dijsktra(int s, List<pair> l[], int n) { PriorityQueue<pair> pq = new PriorityQueue<>(new ascend()); int dist[] = new int[100005]; boolean v[] = new boolean[100005]; for (int i = 1; i <= n; i++) dist[i] = 1000000000; dist[s] = 0; for (int i = 1; i < n; i++) { if (i == s) pq.add(new pair(s, 0)); else pq.add(new pair(i, 1000000000)); } while (!pq.isEmpty()) { pair node = pq.remove(); v[node.a] = true; for (int i = 0; i < l[node.a].size(); i++) { int v1 = l[node.a].get(i).a; int w = l[node.a].get(i).b; if (v[v1]) continue; if ((dist[node.a] + w) < dist[v1]) { dist[v1] = dist[node.a] + w; pq.add(new pair(v1, dist[v1])); } } } }*/ } static class pair { long a; int b; public pair(long a, int i) { this.a = a; this.b = i; } } static class pair1 { pair p; int in; public pair1(pair a, int n) { this.p = a; this.in = n; } } static int inf = (int) 1e9 + 7; static class solver { void solve() { int n = sc.nextInt(); int m = sc.nextInt(); List<Integer> l[] = new ArrayList[n]; for (int i = 0; i < n; i++) l[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; l[u].add(v); l[v].add(u); } int cntt = 0, cnt1 = 0; for (int i = 0; i < n; i++) { int len = l[i].size(); if (len == 2) cntt++; else if (len == 1) cnt1++; } if (cntt == (n - 2) && cnt1 == 2) System.out.println("bus topology"); else if (cntt == n) System.out.println("ring topology"); else if (cnt1 == n - 1) System.out.println("star topology"); else System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
4a637d98b7e14a39acbffdef666c3ca4
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(), deg[] = new int[n]; for(int i = 0; i < m; ++i) { int u = sc.nextInt() - 1, v = sc.nextInt() - 1; deg[u]++; deg[v]++; } int two = 0, full = 0; for(int x: deg) if(x == 2) ++two; else if(x == n-1) ++full; String b = "bus topology", s = "star topology", u = "unknown topology", r = "ring topology"; if(m == n-1) if(two == n - 2) out.println(b); else if(full == 1) out.println(s); else out.println(u); else if(m == n && two == n) out.println(r); else out.println(u); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
d0971dbcd7cfdbdca97cdc60220f87e9
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ArrayList<Integer>adj[]; static boolean vis[]; static int level[]; static int freq[]; static long q,sum; static Set<Integer>su=new TreeSet<>(); public static void main(String args[])throws UnsupportedEncodingException, IOException, Exception { Reader.init(System.in); PrintWriter pw=new PrintWriter(System.out); int n=Reader.nextInt(); int m=Reader.nextInt(); freq=new int[n]; adj=new ArrayList[n]; for(int i=0;i<m;i++) { int u=Reader.nextInt();u--; int v=Reader.nextInt();v--; freq[u]++; freq[v]++; } int on=0,tw=0,tr=0,ano=0; for(int i=0;i<n;i++) { if(freq[i]==1)on++; else if(freq[i]==2)tw++; else if(freq[i]==n-1)ano++; else tr++; } if(tw==n&&tr==0&&on==0)pw.println("ring topology"); else if (tw==n-2&&on==2&&tr==0)pw.println("bus topology"); else if (tr==0&&on==n-1&&ano==1)pw.println("star topology"); else pw.println("unknown topology"); pw.close(); } private static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; a = temp; } return a; } private static long lcm(long a, long b) { return a * (b / gcd(a, b)); } public static boolean isPrime(long n) { boolean flag = false; for(int i = 2; i <= n/2; ++i) { if(n% i == 0) { flag = true; break; } } if (!flag) return true; else return false; } public static void dfs(int i) { vis[i]=true; for(int j=0;j<adj[i].size();j++) { if(!vis[adj[i].get(j)]) dfs(adj[i].get(j)); } } public static int count() { int cnt=0; int index; while((index=check())!=-1) {dfs(index);cnt++;} return cnt; } public static int check() { for(int i=0;i<vis.length; i++) if(!vis[i])return i; return -1; } } class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> { S first; T second; Pair(S f, T s) { first = f; second = s; } @Override public int compareTo(Pair<S, T> o) { int t = first.compareTo(o.first); if (t == 0) return second.compareTo(o.second); return t; } @Override public int hashCode() { return (31 + first.hashCode()) * 31 + second.hashCode(); } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) return false; if (o == this) return true; Pair p = (Pair) o; return first.equals(p.first) && second.equals(p.second); } @Override public String toString() { return "Pair{" + first + ", " + second + "}"; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) throws UnsupportedEncodingException { reader = new BufferedReader( new InputStreamReader(input, "UTF-8")); tokenizer = new StringTokenizer(""); } static void init(String url) throws FileNotFoundException { reader = new BufferedReader(new FileReader(url)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
dca956cc2b0538205a2eaa650bd0ace1
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; import java.util.Vector; public class BNetworkTopology { public static void main(String[] args) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(reader.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); Vector <Integer>vectX1=new Vector(); Vector <Integer>vectX2=new Vector(); Vector <Integer>vect=new Vector(); String ch=""; for(int i=0;i<m;i++) { StringTokenizer st1=new StringTokenizer(reader.readLine()); vectX1.add(Integer.parseInt(st1.nextToken())); vectX2.add(Integer.parseInt(st1.nextToken())); vect.add(vectX1.elementAt(i)); vect.add(vectX2.elementAt(i)); } HashMap<Integer, Integer> charCountMap = new HashMap(); for (Integer c : vect) { if(charCountMap.containsKey(c)) { charCountMap.put(c, charCountMap.get(c)+1); } else { charCountMap.put(c, 1); } } vect.clear(); StringTokenizer st1=new StringTokenizer(charCountMap.entrySet().toString(),"= [ ] ,"); while(st1.hasMoreTokens()) { String c=st1.nextToken(); vect.add(Integer.parseInt(st1.nextToken())); } if(vect.contains(m)) System.out.println("star topology"); else if(isBus(vect)&&!IsAnyOne(vect)) System.out.println("bus topology"); else if(!IsAnyOne(vect)) System.out.println("ring topology"); else System.out.println("unknown topology"); } static boolean IsEnEtoile(Vector <Integer>vect1) { int x=vect1.elementAt(0); for(int i=1;i<vect1.size();i++) { if(!vect1.elementAt(i).equals(x)) return false; } return true; } static boolean isBus(Vector <Integer>vect) { int ones=0; for(int i=0;i<vect.size();i++) { if(vect.elementAt(i)==1) ones++; } return ones == 2; } static boolean IsAnyOne(Vector <Integer>vect) { for(int i=0;i<vect.size();i++) { if(vect.elementAt(i)>2) return true; } return false; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
77f7cc6b8aa458feabe47b56294c7dca
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.Arrays; import java.util.ArrayList; import java.lang.Math; import java.util.Arrays; import java.util.Comparator; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int binarySearch(int a[] ,int k,int l,int h){ while(l<=h){ int mid = (l+h)/2; if(a[mid]==k) return mid; else if(a[mid]>k) h=mid-1; else if(a[mid]<k) l=mid+1; } return -1; } static int binarySearch(ArrayList<Integer> a ,int k,int l,int h){ while(l<=h){ int mid = (l+h)/2; if(a.get(mid)==k) return mid; else if(a.get(mid)>k) h=mid-1; else if(a.get(mid)<k) l=mid+1; } return -1; } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = 0; r = a.length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static int solve(int A, int B) { int count = 0; for (int i = 0; i < 21; i++) { if (((A >> i) & 1) != ((B >> i) & 1)) { count++; } } return count; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static long count(long k) { return k * (k - 1) / 2; } static boolean isPrime(int n) { // if(n==1) return false; if(n==2) return true; if (n%2==0) return false; int l = (int)Math.sqrt(n); for(int i=3;i<=l;i+=2) { if(n%i==0) return false; } return true; } static int negMod(int n){ int a = (n % 1000000007 + 1000000007) % 1000000007; return a; } public static int sum(long x) { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } static ArrayList<Long> a = new ArrayList<>(); static void genLucky(long val , int f , int s){ if(val>10000000000l) return ; if(f==s) a.add(val); genLucky(val*10+4, f+1, s); genLucky(val*10+7, f, s+1); } public static int max(int x, int y, int z) { return (int)Math.max(Math.max(x, y), z); } public static int min(int x, int y, int z) { return (int)Math.min(Math.min(x, y), z); } static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) { adj.get(u).add(v); adj.get(v).add(u); } static int mod=1000003; public static void main(String[] args) throws Exception { OutputStream outputStream = System.out; PrintWriter w = new PrintWriter(outputStream); FastReader sc = new FastReader(); // Scanner sc = new Scanner(new File("input.txt")); // PrintWriter out = new PrintWriter(new File("output.txt")); int i,j=-1; int t = 1; while(t>0){ int n = sc.nextInt(); int m = sc.nextInt(); ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(n+1); for(i=0;i<=n;i++) adj.add(new ArrayList<Integer>()); for(i=0;i<m;i++){ int u = sc.nextInt(); int v = sc.nextInt(); addEdge(adj, u, v); } int dis[] = new int[n+1]; int ones = 0,two = 0, all=0; for(i=1;i<=n;i++){ dis[i] = adj.get(i).size(); if(dis[i]==1) ones++; else if(dis[i]==2) two++; else if(dis[i]==n-1) all++; } if(ones == 2 && two==n-2) w.println("bus topology"); else if(ones==0 && two==n) w.println("ring topology"); else if(ones==n-1 && all==1) w.println("star topology"); else w.println("unknown topology"); t--; } w.close(); } } // System.out.println();
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
702093fc557e9291a55cd0aee206c9cf
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; /** * http://codeforces.ru/contest/292/problem/B * * @author scorpion@yandex-team on 08.01.15. */ public class Solution292B { public static void main(final String[] args) throws Exception { final Scanner sc = new Scanner(System.in); final int n = sc.nextInt(); final int m = sc.nextInt(); final int[] degree = new int[n]; for (int i = 0; i < m; i++) { final int v1 = sc.nextInt() - 1; final int v2 = sc.nextInt() - 1; degree[v1]++; degree[v2]++; } int min = m; int minCount = 0; int max = 0; int maxCount = 0; for (int i = 0; i < n; i++) { if (degree[i] < min) { min = degree[i]; minCount = 1; } else if (degree[i] == min) { minCount++; } if (degree[i] > max) { max = degree[i]; maxCount = 1; } else if (degree[i] == max) { maxCount++; } } // System.out.println(max + " " + maxCount + " " + min + " " + minCount); if (min == max && min == 2) { System.out.println("ring topology"); return; } if (min == 1 && minCount == n - 1 && max == n - 1 && maxCount == 1) { System.out.println("star topology"); return; } if (min == 1 && minCount == 2 && max == 2 && maxCount == n - 2) { System.out.println("bus topology"); return; } System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
14df3bbee2d7e866ddd24231e47fc593
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static PrintWriter out=new PrintWriter(System.out);static FastScanner in = new FastScanner(System.in);static class FastScanner {BufferedReader br;StringTokenizer stok;FastScanner(InputStream is) {br = new BufferedReader(new InputStreamReader(is));} String next() throws IOException {while (stok == null || !stok.hasMoreTokens()) {String s = br.readLine();if (s == null) {return null;} stok = new StringTokenizer(s);}return stok.nextToken();} int ni() throws IOException {return Integer.parseInt(next());}long nl() throws IOException {return Long.parseLong(next());}double nd() throws IOException {return Double.parseDouble(next());}char nc() throws IOException {return (char) (br.read());}String ns() throws IOException {return br.readLine();} int[] nia(int n) throws IOException{int a[] = new int[n];for (int i = 0; i < n; i++)a[i] = ni();return a;}long[] nla(int n) throws IOException {long a[] = new long[n];for (int i = 0; i < n; i++)a[i] = nl();return a;} double[] nda(int n)throws IOException {double a[] = new double[n];for (int i = 0; i < n; i++)a[i] = nd();return a;}int [][] imat(int n,int m) throws IOException{int mat[][]=new int[n][m];for(int i=0;i<n;i++){for(int j=0;j<m;j++)mat[i][j]=ni();}return mat;} } static long mod=Long.MAX_VALUE; public static void main (String[] args) throws java.lang.Exception { int i,j; HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); /* if(hm.containsKey(z)) hm.put(z,hm.get(z)+1); else hm.put(z,1); */ HashSet<Integer> set=new HashSet<Integer>(); PriorityQueue<Integer> pq=new PriorityQueue<Integer>(); int n=in.ni(); int m=in.ni(); ArrayList<Integer> arr[]=new ArrayList[n+1]; for(i=0;i<=n;i++) arr[i]=new ArrayList<Integer>(); for(i=0;i<m;i++) { int x=in.ni(); int y=in.ni(); arr[x].add(y); arr[y].add(x); } ArrayList<Integer> a=new ArrayList<>(); for(i=1;i<=n;i++) a.add(arr[i].size()); Collections.sort(a); int one=0,two=0,cc=0; for(int c:a) { if(c==1) one++; else if(c==2) two++; else if(c==n-1) cc++; } if(one==2 && two==n-2) out.println("bus topology"); else if(two==n) out.println("ring topology"); else if(one==n-1 && cc==1) out.println("star topology"); else out.println("unknown topology"); out.close(); } static class pair implements Comparable<pair>{ int x, y; public pair(int x, int y){this.x = x; this.y = y;} @Override public int compareTo(pair arg0) { if(x<arg0.x) return -1; else if(x==arg0.x) { if(y<arg0.y) return -1; else if(y>arg0.y) return 1; else return 0; } else return 1; } } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long exponent(long a,long n) { long ans=1; while(n!=0) { if(n%2==1) ans=(ans*a)%mod; a=(a*a)%mod; n=n>>1; } return ans; } static int binarySearch(int a[], int item, int low, int high) { if (high <= low) return (item > a[low])? (low + 1): low; int mid = (low + high)/2; if(item == a[mid]) return mid+1; if(item > a[mid]) return binarySearch(a, item, mid+1, high); return binarySearch(a, item, low, mid-1); } static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } while (i < n1){ arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void Sort(int arr[], int l, int r) {if (l < r) { int m = (l+r)/2; Sort(arr, l, m); Sort(arr , m+1, r); merge(arr, l, m, r); } } static void sort(int a[]) {Sort(a,0,a.length-1);} }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
6469b7367e183db1232260fca33e7ed5
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.PrintStream; import java.lang.*; import static java.lang.Character.isUpperCase; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.*; import java.util.TreeMap; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class simple5 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader in = new FastReader(); int n = in.nextInt(); int m = in.nextInt(); HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i=0;i<m;i++) { int x = in.nextInt(); int y = in.nextInt(); if(map.containsKey(x)) { int z = map.get(x); z++; map.put(x, z); } else map.put(x, 1); if(map.containsKey(y)) { int z = map.get(y); z++; map.put(y, z); } else map.put(y, 1); } int count1=0; int count2=0; int count3=0; for(int i:map.keySet()) { int a = map.get(i); if(a==1) count1++; else if(a==2) count2++; else { count3++; } } if(count3==1 && count2==0 && count1==n-1) { System.out.println("star topology"); } else if(count2==n-2 && count1==2) { System.out.println("bus topology"); } else if(count2==n) { System.out.println("ring topology"); } else { System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
aa09f1e45dcf6dac5e012b97b7c3237d
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
/* * Author- Priyam Vora * BTech 2nd Year DAIICT */ import java.io.*; import java.math.*; import java.util.*; import javax.print.attribute.SetOfIntegerSyntax; public class Graph1 { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long count = 0,mod=1000000007; private static TreeSet<Integer>ts[]=new TreeSet[200000]; private static HashSet hs=new HashSet(); public static void main(String args[]) throws Exception { InputReader(System.in); pw = new PrintWriter(System.out); //ans(); soln(); pw.close(); } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd( y % x,x); } private static int BinarySearch(int a[], int low, int high, int target) { if (low > high) return -1; int mid = low + (high - low) / 2; if (a[mid] == target) return mid; if (a[mid] > target) high = mid - 1; if (a[mid] < target) low = mid + 1; return BinarySearch(a, low, high, target); } public static String reverseString(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return (sb.toString()); } public static long pow(long n, long p) { if(p==0) return 1; if(p==1) return n%mod; if(p%2==0){ long temp=pow(n, p/2); return (temp*temp)%mod; }else{ long temp=pow(n,p/2); temp=(temp*temp)%mod; return(temp*n)%mod; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } public static int nextPowerOf2(final int a) { int b = 1; while (b < a) { b = b << 1; } return b; } public static boolean PointInTriangle(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) { int s = p2 * p5 - p1 * p6 + (p6 - p2) * p7 + (p1 - p5) * p8; int t = p1 * p4 - p2 * p3 + (p2 - p4) * p7 + (p3 - p1) * p8; if ((s < 0) != (t < 0)) return false; int A = -p4 * p5 + p2 * (p5 - p3) + p1 * (p4 - p6) + p3 * p6; if (A < 0.0) { s = -s; t = -t; A = -A; } return s > 0 && t > 0 && (s + t) <= A; } public static float area(int x1, int y1, int x2, int y2, int x3, int y3) { return (float) Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); } public static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } //merge Sort static long sort(int a[]) { int n=a.length; int b[]=new int[n]; return mergeSort(a,b,0,n-1);} static long mergeSort(int a[],int b[],long left,long right) { long c=0;if(left<right) { long mid=left+(right-left)/2; c= mergeSort(a,b,left,mid); c+=mergeSort(a,b,mid+1,right); c+=merge(a,b,left,mid+1,right); } return c; } static long merge(int a[],int b[],long left,long mid,long right) {long c=0;int i=(int)left;int j=(int)mid; int k=(int)left; while(i<=(int)mid-1&&j<=(int)right) { if(a[i]>a[j]) {b[k++]=a[i++]; } else { b[k++]=a[j++];c+=mid-i;}} while (i <= (int)mid - 1) b[k++] = a[i++]; while (j <= (int)right) b[k++] = a[j++]; for (i=(int)left; i <= (int)right; i++) a[i] = b[i]; return c; } public static boolean isSubSequence(String large, String small, int largeLen, int smallLen) { //base cases if (largeLen == 0) return false; if (smallLen == 0) return true; // If last characters of two strings are matching if (large.charAt(largeLen - 1) == small.charAt(smallLen - 1)) isSubSequence(large, small, largeLen - 1, smallLen - 1); // If last characters are not matching return isSubSequence(large, small, largeLen - 1, smallLen); } // To Get Input // Some Buffer Methods public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private static int[][] next2dArray(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextInt(); } } return arr; } private static char[][] nextCharArray(int n,int m){ char [][]c=new char[n][m]; for(int i=0;i<n;i++){ String s=nextLine(); for(int j=0;j<s.length();j++){ c[i][j]=s.charAt(j); } } return c; } private static long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(boolean[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } static int col[]; static boolean vis[]; //----------------------------------------My Code------------------------------------------------// private static void soln() { //-1 west 1 east 2 north -2 south int n=nextInt(); int m=nextInt(); Graph g=new Graph(n); for(int i=0;i<m;i++){ int v=nextInt(); int w=nextInt(); g.addEdge(v, w); g.addEdge(w, v); } g.getAns(n); } //-----------------------------------------The End--------------------------------------------------------------------------// } class Pair implements Comparable<Pair>{ int ind; int len; Pair(int ind,int len){ this.ind=ind; this.len=len; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub // Sort in increasing order return 0; } } class Graph{ private static int V,level[][],count=-1,lev_dfs[],degree=0,no_vert_conn_comp=0; private Stack <Integer>st=new Stack(); private static LinkedList<Integer > adj[]; private boolean[][] Visite; private static boolean [] Visited; private static HashSet<Integer> Vis=new HashSet(); private static Stack<Integer> topo_sort=new Stack<>(); private static HashMap<Integer,Character> hm=new HashMap<>(); private static ArrayList<Integer> col[]; private static int color[]; Graph(int V){ V++; this.V=(V); adj=new LinkedList[V]; Visite=new boolean[100][100]; Visited=new boolean[V]; level=new int[100][100]; lev_dfs=new int[V]; for(int i=0;i<V;i++) adj[i]=new LinkedList<Integer>(); } void setup(int n,int colo[]){ color=new int[n+1]; col=new ArrayList[100001]; for(int i=1;i<=100000;i++){ col[i]=new ArrayList<Integer>(); } for(int i=1;i<=n;i++){ col[colo[i]].add(i); } for(int i=1;i<=n;i++){ color[i]=colo[i]; } } void addEdge(int v,int w){ if(adj[v]==null){ adj[v]=new LinkedList(); } adj[v].add(w); } public static void top_ans(){ while(topo_sort.size()!=0) { System.out.print(hm.get(topo_sort.peek())); topo_sort.pop(); } } public static void topoSort(int startVert){ if(!Vis.contains(startVert)){ topoSortUtil(startVert); } } public static void topoSortUtil(int curr_Vert){ Vis.add(curr_Vert); Iterator<Integer> it=adj[curr_Vert].listIterator(); while(it.hasNext()){ int n=it.next(); // System.out.println(n); if(!Vis.contains(n)){ topoSortUtil(n); } } topo_sort.push(curr_Vert); } public static int BFS2(int startVert){ Visited=new boolean[V]; for(int i=1;i<V;i++){ lev_dfs[i]=-1; } System.out.println(startVert); Queue<Integer> q=new LinkedList<Integer>(); q.add(startVert); lev_dfs[startVert]=0; while(!q.isEmpty()){ int top=q.poll(); Iterator<Integer> i= adj[top].listIterator(); while(i.hasNext()){ int n=i.next(); System.out.println(top+" "+n); if(!Visited[n]){ q.add(n); Visited[n]=true; lev_dfs[n]=lev_dfs[top]+1; } } } for(int i=1;i<V;i++){ System.out.println(lev_dfs[i]); } // q.clear(); return -1; } public int getEd(){ return degree/2; } public void get(int from,int to){ int h=lev_dfs[from]-lev_dfs[to]; if(h<=0){ System.out.println(-1); }else{ System.out.println(h-1); } } private static boolean check(int x,int y,char c[][]){ if((x>=0 && y>=0) && (x<c.length && y<c[0].length) && c[x][y]!='#'){ return true; } return false; } public int BFS(int x,int y,int k,char[][] c) { LinkedList<Pair> queue = new LinkedList<Pair>(); //Visited[s]=true; // queue.add(new Pair(x,y)); int count=0; level[x][y]=-1; c[x][y]='M'; while (!queue.isEmpty()) { Pair temp = queue.poll(); //x=temp.idx; //y=temp.val; c[x][y]='M'; // System.out.println(x+" "+y+" ---"+count); count++; if(count==k) { for(int i=0;i<c.length;i++){ for(int j=0;j<c[0].length;j++){ if(c[i][j]=='M'){ System.out.print("."); } else if(c[i][j]=='.') System.out.print("X"); else System.out.print(c[i][j]); } System.out.println(); } System.exit(0); } // System.out.println(x+" "+y); // V--; } return V; } public static void getAns(int n){ //ring int flag=1; int degree2=0; for(int i=1;i<V;i++){ if(adj[i].size()==2){ degree2++; } } if(degree2==(n)){ System.out.println("ring topology"); }else{ int de_2=0,de_1=0; for(int i=1;i<V;i++){ if(adj[i].size()==2){ de_2++; } if(adj[i].size()==1) de_1++; } if(de_2==(n-2) && de_1==2){ System.out.println("bus topology"); }else{ int deg_1=0,deg_n_1=0; for(int i=1;i<V;i++){ if(adj[i].size()==1) deg_1++; if(adj[i].size()==(n-1)) deg_n_1++; } if(deg_1==(n-1) && deg_n_1==1) System.out.println("star topology"); else System.out.println("unknown topology"); } } } public long dfs(int startVertex,int[] col){ // getAns(startVertex); if(!Visited[startVertex]) { return dfsUtil(startVertex,col); //return getAns(); } return 0; } private long dfsUtil(int startVertex,int[] col) {//0-Blue 1-Pink // col_freq=new int[200001]; // col_freq[col[startVertex-1]]++; HashMap<Integer,Integer> hm=new HashMap<>(); int c=1; hm.put(col[startVertex-1], 1); long cout=0; degree=0; Visited[startVertex]=true; lev_dfs[startVertex]=1; st.push(startVertex); while(!st.isEmpty()){ int top=st.pop(); // ts.add(top); Iterator<Integer> i=adj[top].listIterator(); while(i.hasNext()){ // System.out.println(top); int n=i.next(); if( !Visited[n]){ Visited[n]=true; // col_freq[col[n-1]]++; c++; if(hm.containsKey(col[n-1])){ hm.replace(col[n-1],hm.get(col[n-1])+1); }else{ hm.put(col[n-1],1); } st.push(n); lev_dfs[n]=lev_dfs[top]+1; } } } int ans=0; for(int x:hm.keySet()){ ans=Math.max(ans, hm.get(x)); } return c-ans; // System.out.println("NO"); // return c; } } class Dsu{ private int rank[], parent[] ,n; Dsu(int size){ this.n=size+1; rank=new int[n]; parent=new int[n]; makeSet(); } void makeSet(){ for(int i=0;i<n;i++){ parent[i]=i; } } int find(int x){ if(parent[x]!=x){ parent[x]=find(parent[x]); } return parent[x]; } void union(int x,int y){ int xRoot=find(x); int yRoot=find(y); if(xRoot==yRoot) return; if(rank[xRoot]<rank[yRoot]){ parent[xRoot]=yRoot; }else if(rank[yRoot]<rank[xRoot]){ parent[yRoot]=xRoot; }else{ parent[yRoot]=xRoot; rank[xRoot]++; } } } class Heap{ public static void build_max_heap(long []a,int size){ for(int i=size/2;i>0;i--){ max_heapify(a, i,size); } } private static void max_heapify(long[] a,int i,int size){ int left_child=2*i; int right_child=(2*i+1); int largest=0; if(left_child<size && a[left_child]>a[i]){ largest=left_child; }else{ largest=i; } if(right_child<size && a[right_child]>a[largest]){ largest=right_child; } if(largest!=i){ long temp=a[largest]; a[largest]=a[i]; a[i]=temp; max_heapify(a, largest,size); } } private static void min_heapify(int[] a,int i){ int left_child=2*i; int right_child=(2*i+1); int largest=0; if(left_child<a.length && a[left_child]<a[i]){ largest=left_child; }else{ largest=i; } if(right_child<a.length && a[right_child]<a[largest]){ largest=right_child; } if(largest!=i){ int temp=a[largest]; a[largest]=a[i]; a[i]=temp; min_heapify(a, largest); } } public static void extract_max(int size,long a[]){ if(a.length>1){ long max=a[1]; a[1]=a[a.length-1]; size--; max_heapify(a, 1,a.length-1); } } } class MyComp implements Comparator<Long>{ @Override public int compare(Long o1, Long o2) { if(o1<o2){ return 1; }else if(o1>o2){ return -1; } return 0; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
ab73d3ec601a0add0edf2c885e55b1d2
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.io.*; public class solution{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); int N=sc.nextInt(); int E=sc.nextInt(); int[]deg=new int[N+1]; for(int i=1;i<=E;i++){ deg[sc.nextInt()]++; deg[sc.nextInt()]++; } int one=0; int two=0; int more=0; for(int i=1;i<deg.length;i++){ if(deg[i]==1) one++; else if(deg[i]==2) two++; else more++; } if(two==N) System.out.println("ring topology"); else if(one==N-1 && more==1) System.out.println("star topology"); else if(two==N-2 && one==2) System.out.println("bus topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
05da8fd8dd8124bb3f691483e67d66bc
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Network_Topology { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); PrintWriter pw=new PrintWriter(System.out); int t1=1; m:while(t1-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int deg[]=new int[n+1]; for(int i=0;i<m;i++) { int x=sc.nextInt(); int y=sc.nextInt(); deg[x]++; deg[y]++; } TreeMap<Integer,Integer> mp=new TreeMap<>(); for(int i=1;i<n+1;i++) { mp.put(deg[i],mp.getOrDefault(deg[i],0)+1); } if(mp.size()==1) { if((int)mp.firstKey()==2) { pw.println("ring topology"); } else { pw.println("unknown topology"); } } else if(mp.size()==2) { if((int)mp.firstKey()==1 && (int)mp.lastKey()==n-1) { pw.println("star topology"); } else if((int)mp.firstKey()==1 && (int)mp.lastKey()==2) { pw.println("bus topology"); } else { pw.println("unknown topology"); } } else { pw.println("unknown topology"); } } pw.flush(); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
0507b57074025fb528cb60188424d5a0
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; 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); topology solver = new topology(); solver.solve(1, in, out); out.close(); } static class topology { public void solve(int n, InputReader in, PrintWriter out) { n = in.readInt(); int m = in.readInt(); List[] arr; arr = new ArrayList[n + 1]; for (int i = 0; i <= n; i++) { arr[i] = new ArrayList(); } for (int i = 0; i < m; i++) { int a = in.readInt(); int b = in.readInt(); arr[a].add(b); arr[b].add(a); } HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); for (int i = 1; i <= n; i++) { int count = 0; for (int j = 0; j < arr[i].size(); j++) { if ((int) arr[i].get(j) != 0) count++; } hmap.put(i, count); } int c = 0; int d = 0; int e = 0; for (int i = 1; i <= n; i++) { if (hmap.get(i) == 1) c++; if (hmap.get(i) == 2) d++; if (hmap.get(i) == n - 1) e++; } if (c == 2 && d == n - 2) out.println("bus topology"); if (d == n) out.println("ring topology"); if (c == n - 1 && e == 1) out.println("star topology"); if (c != 2 || d != n - 2) { if (d != n) { if (c != n - 1 || e != 1) out.println("unknown topology"); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
d06dee2529cd758b7b2ed3dc1eaea1b5
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; public class networkTopology{ public static void main(String []args){ Scanner in = new Scanner(System.in); int n, m; n = in.nextInt(); m = in.nextInt(); ArrayList<Integer> graph[]; graph = new ArrayList[n+1]; for(int i=1;i<=n;i++) graph[i] = new ArrayList<>(); for(int i=0;i<m;i++){ int x, y; x = in.nextInt(); y = in.nextInt(); graph[x].add(y); graph[y].add(x); } int deg1 = 0; int deg2 = 0; int degx = 0; for(int i=1;i<=n;i++){ if(graph[i].size() == 1) deg1++; if(graph[i].size() == 2) deg2++; if(graph[i].size() != 1 && graph[i].size() != 2) degx++; } if(deg2 == n){ System.out.println("ring topology"); System.exit(0); } if(deg2 == n-2 && deg1 == 2){ System.out.println("bus topology"); System.exit(0); } if(deg1 == n-1 && degx == 1){ System.out.println("star topology"); System.exit(0); } else{ System.out.println("unknown topology"); System.exit(0); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
48a2de3b7e947ee80950dcb143bdcdb6
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; public class networkTopology{ public static void main(String []args){ Scanner in = new Scanner(System.in); int n, m; n = in.nextInt(); m = in.nextInt(); Vector<Integer> graph[]; graph = new Vector[n+1]; for(int i=1;i<=n;i++) graph[i] = new Vector<>(); for(int i=0;i<m;i++){ int x, y; x = in.nextInt(); y = in.nextInt(); graph[x].add(y); graph[y].add(x); } int deg1 = 0; int deg2 = 0; int degx = 0; for(int i=1;i<=n;i++){ if(graph[i].size() == 1) deg1++; if(graph[i].size() == 2) deg2++; if(graph[i].size() != 1 && graph[i].size() != 2) degx++; } if(deg2 == n){ System.out.println("ring topology"); System.exit(0); } if(deg2 == n-2 && deg1 == 2){ System.out.println("bus topology"); System.exit(0); } if(deg1 == n-1 && degx == 1){ System.out.println("star topology"); System.exit(0); } else{ System.out.println("unknown topology"); System.exit(0); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
f9c9bbb199f18c93ecffdaa143653cb4
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; public class networkTopology{ public static void main(String []args){ Scanner in = new Scanner(System.in); int n, m; n = in.nextInt(); m = in.nextInt(); LinkedList<Integer> graph[]; graph = new LinkedList[n+1]; for(int i=1;i<=n;i++) graph[i] = new LinkedList<>(); for(int i=0;i<m;i++){ int x, y; x = in.nextInt(); y = in.nextInt(); graph[x].add(y); graph[y].add(x); } int deg1 = 0; int deg2 = 0; int degx = 0; for(int i=1;i<=n;i++){ if(graph[i].size() == 1) deg1++; if(graph[i].size() == 2) deg2++; if(graph[i].size() != 1 && graph[i].size() != 2) degx++; } if(deg2 == n){ System.out.println("ring topology"); System.exit(0); } if(deg2 == n-2 && deg1 == 2){ System.out.println("bus topology"); System.exit(0); } if(deg1 == n-1 && degx == 1){ System.out.println("star topology"); System.exit(0); } else{ System.out.println("unknown topology"); System.exit(0); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
81b13490cb989eb43001ab2c72b457a4
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.String.*; public class Main { static int cnt; static List<Integer> [] g; static boolean [] vis; static boolean f; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder out = new StringBuilder(); StringTokenizer tk; tk = new StringTokenizer(in.readLine()); int n = parseInt(tk.nextToken()),m = parseInt(tk.nextToken()); g = new List[n]; for(int i=0; i<n; i++) g[i] = new ArrayList<>(); for(int i=0; i<m; i++) { tk = new StringTokenizer(in.readLine()); int a = parseInt(tk.nextToken())-1,b = parseInt(tk.nextToken())-1; g[a].add(b); g[b].add(a); } if(m==n) { vis = new boolean[n]; f = false; cnt = 0; dfs(0); int ff = 0; for(int i=0; i<n; i++) ff = max(ff, g[i].size()); if(f && cnt==n && ff==2) System.out.println("ring topology"); else System.out.println("unknown topology"); } else if(m==n-1) { int s = -1,t = -1,ff = 0; for(int i=0; i<n; i++) { if(g[i].size()==1) { s = i; } else if(g[i].size()==n-1) { t = i; } ff = max(ff, g[i].size()); } vis = new boolean[n]; cnt = 0; if(s!=-1) dfs(s); else dfs(t); if(cnt==n) { if(s!=-1 && ff==2) System.out.println("bus topology"); else if(t!=-1 && ff==n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } else System.out.println("unknown topology"); } else System.out.println("unknown topology"); } static void dfs(int u) { cnt++; vis[u] = true; for(int v : g[u]) if(!vis[v]) dfs(v); else f = true; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
8375e42ac7a8d161f4c70fa20cbd8b71
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Arrays; import java.util.Scanner; /** * Created by Ashiq on 5/14/2016. */ public class Main { public static void main(String []args) { Scanner s=new Scanner(System.in); int node, edge; node=s.nextInt(); edge=s.nextInt(); int [] ara=new int[node+1]; Arrays.fill(ara,0); for(int n=0;n<edge;n++) { int a,b; a=s.nextInt(); b=s.nextInt(); ara[a]++; ara[b]++; } int no1=0; int no2=0; int more=0; for(int n=1;n<=node;n++) { int x=ara[n]; if(x==1) no1++; else if(x==2) no2++; else if(x==node-1) more++; } if(no1==2 && no2==node-2 ) System.out.println("bus topology"); else if(no2==node) System.out.println("ring topology"); else if(more==1 && no1==node-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
7e20f3caec7b05520b559fc34f3ddc63
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); int[] x=new int[n+1]; int[] y=new int[n+1]; int two=0,one=0; for(int j=0;j<m;j++) { int a=sc.nextInt(),b=sc.nextInt(); x[a]++; x[b]++; y[a]=b; y[b]=a; } int max=0; for(int i=1;i<=n;i++) { if(x[i]==1) one++; else if(x[i]==2) two++; max=Math.max(max, x[i]); } if(max==m) System.out.println("star topology"); else if(two==n ) System.out.println("ring topology"); else if(two==n-2 && one==2) System.out.println("bus topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
0bd9ec31095e0c55f29872332914432b
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
/** * * @author sarthak */ import java.util.*; import java.math.*; import java.io.*; public class crocChamp2013_B { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { st = null; try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static class P implements Comparable{ private int x, y; public P(int x, int y) { this.x = x; this.y = y; } public int hashCode() { return (x * 31) ^ y; } public boolean equals(Object o) { if (o instanceof P) { P other = (P) o; return (x == other.x && y == other.y); } return false; } public int compareTo(Object obj) { P l = (P) obj; if (this.x == l.x){ if (this.y == l.y) return 0; return (this.y < l.y)? -1: 1; } return (this.x < l.x)? -1: 1; } } public static void main(String[] args){ FastScanner s = new FastScanner(System.in); StringBuilder op=new StringBuilder(); int n=s.nextInt(); int m=s.nextInt(); int[] deg=new int[n+1]; for(int i=1;i<=m;i++){ deg[s.nextInt()]++; deg[s.nextInt()]++; } if(m==n-1){ //check fr bus int[] cd=new int[n+2]; for(int i=1;i<=n;i++)cd[deg[i]]++; if(cd[1]==2&&cd[2]==n-2){ System.out.println("bus topology");return; } if(cd[1]==n-1&&cd[n-1]==1) { System.out.println("star topology");return; } } if(m==n){ int[] cd=new int[n+2]; for(int i=1;i<=n;i++)cd[deg[i]]++; if(cd[2]==n) {System.out.println("ring topology");return;} } System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
eeb09f5c850f91fb4e73167b55671e68
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; import java.util.TreeSet; public class B292 { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } static String nextLine() throws IOException { return br.readLine(); } static char nextChar() throws IOException { return (char) br.read(); } public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); run(); pw.close(); } private static void run() { int n = nextInt(); int m = nextInt(); int[] graf = new int[n]; for (int i = 0; i < m; i++) { graf[nextInt() - 1]++; graf[nextInt() - 1]++; } int one = 0; int two = 0; int howewer = 0; for (int i = 0; i < n; i++) { if (graf[i] == 1) { one++; } else if (graf[i] == 2) { two++; } else { howewer++; } } if (one == 0 && howewer == 0) { pw.print("ring topology"); } else if (one == 2 && howewer == 0) { pw.print("bus topology"); } else if (howewer == 1 && two == 0) { pw.print("star topology"); } else { pw.print("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
0dfdb7a404cbdb564fd2c41d0eeda9f5
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; import java.util.TreeSet; public class B292 { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; static String nextToken() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } static int nextInt() { return Integer.parseInt(nextToken()); } static long nextLong() { return Long.parseLong(nextToken()); } static String nextLine() throws IOException { return br.readLine(); } static char nextChar() throws IOException { return (char) br.read(); } public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); run(); pw.close(); } static ArrayList<Integer>[] graf; private static void run() { int n = nextInt(); int m = nextInt(); graf = new ArrayList[n]; for (int i = 0; i < n; i++) { graf[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int x = nextInt() - 1; int y = nextInt() - 1; graf[x].add(y); graf[y].add(x); } int one = 0; int two = 0; int howewer = 0; for (int i = 0; i < n; i++) { if (graf[i].size() == 1) { one++; } else if (graf[i].size() == 2) { two++; } else { howewer++; } } if (one == 0 && howewer == 0) { pw.print("ring topology"); } else if (one == 2 && howewer == 0) { pw.print("bus topology"); } else if (howewer == 1 && two == 0) { pw.print("star topology"); } else { pw.print("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
29884497d4c5b4be5fbfca73d3b080d0
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.io.PrintWriter; public class Main{ public static long arr[]; public static int c1,c2,c3; public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); Map<Integer,List<Integer>> graph = new HashMap<>(); for(int i=1;i<=m;i++) { int src = sc.nextInt(); int dst = sc.nextInt(); addEdge(graph, src,dst); } c1=0;c2=0;c3=0; countEdge(graph); if(c1==2 && c2== n-2 && c3==0) out.println("bus topology"); else if(c1==0 && c2==n && c3==0) out.println("ring topology"); else if(c1==n-1 && c3==1 && c2==0) out.println("star topology"); else out.println("unknown topology"); out.flush(); } private static void countEdge(Map<Integer,List<Integer>>graph) { for(Map.Entry<Integer,List<Integer>>gg:graph.entrySet()){ List<Integer> list = gg.getValue(); if(list.size() == 1) c1++; else if(list.size() == 2) c2++; else c3++; } } private static void addEdge(Map<Integer,List<Integer>>graph,int src,int dst) { List<Integer> list = graph.get(src); if(list == null) { list = new ArrayList<>(); graph.put(src,list); } list.add(dst); list = graph.get(dst); if(list == null) { list = new ArrayList<>(); graph.put(dst,list); } list.add(src); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
ae78c250b4fecd86714be3cf16137a5e
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.security.KeyPair; import java.util.*; public class Main { public static boolean isbus=true; public static boolean allvisited =false; public static boolean star =false; public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<Integer,List<Integer>> graph = new HashMap<Integer,List<Integer>>(); int vertex = sc.nextInt(); for(int i = 1; i <= vertex;i++) { graph.put(i,new ArrayList<Integer>()); } int relations = sc.nextInt(); int r = relations; while(relations-- > 0) { int u = sc.nextInt(); int v = sc.nextInt(); graph.get(u).add(v); graph.get(v).add(u); } if(checkstar(graph,vertex-1,r)) { System.out.println("star topology"); }else if(checkbus(graph)) { System.out.println("bus topology"); }else if(checkring(graph)) { System.out.println("ring topology"); }else { System.out.println("unknown topology"); } } public static boolean checkstar(HashMap<Integer,List<Integer>> graph,int keys,int relations) { for(int i = 1; i < graph.size()+1;i++) { if(graph.get(i).size() == relations) { return true; } } return false; } public static boolean checkbus(HashMap<Integer,List<Integer>> graph) { int onepoint = 0; int twopoint = 0; for(int i = 1; i < graph.size()+1;i++) { if(graph.get(i).size() == 1) { onepoint++; } if(graph.get(i).size() == 2) { twopoint++; } } if(onepoint == 2 && twopoint == graph.size()-2) { return true; } return false; } public static boolean checkring(HashMap<Integer,List<Integer>> graph) { for(int i = 1; i < graph.size()+1;i++) { if(graph.get(i).size() != 2) { return false; } } return true; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
9293757e984f6dfdf0d077e045d0d289
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class B292 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int v = in.nextInt(); int e = in.nextInt(); int[] vertices = new int[v]; int a, b; for( int i = 0; i < e; i++ ) { a = in.nextInt()-1; b = in.nextInt()-1; vertices[a]++; vertices[b]++; } // bus boolean bus, star, ring; int count11 = 0; int count22 = 0; bus = false; for( int i = 0; i < v; i++ ) { if( vertices[i] == 2 ) { count22++; } else if (vertices[i] == 1) { count11++; } } if( count11 == 2 && count22 == v-2 ) { bus = true; } // ring int count2 = 0; ring = true; for( int i = 0; i < v; i++ ) { if( vertices[i] != 2 ) { ring = false; break; } } int count1 = 0; int countMax = 0; star = false; for( int i = 0; i < v; i++ ) { if( vertices[i] == 1 ) { count1++; } else if (vertices[i] == e) { countMax++; } } if( count1 == v-1 && countMax == 1 ) { star = true; } if( star ) { System.out.println("star topology"); } else if ( ring ) { System.out.println("ring topology"); } else if ( bus ) { System.out.println("bus topology"); } else { System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
f2104d61d891602e661cc8e225b6de32
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main extends PrintWriter { BufferedReader in; StringTokenizer stok; final Random rand = new Random(31); final int inf = (int) 1e9; final long linf = (long) 1e18; final static String IO = "_std"; List<Integer>[] g; int[] used; boolean hasCycle = false; void dfs(int v, int p) { used[v] = 1; for (Integer to : g[v]) { if (to == p) { continue; } if (used[to] == 0) { dfs(to, v); } else { hasCycle = true; } } used[v] = 2; } void solve() throws IOException { int n = nextInt(); int m = nextInt(); g = createAdjacencyList(n); used = new int[n]; for (int i = 0; i < m; i++) { int x = nextInt() - 1; int y = nextInt() - 1; g[x].add(y); g[y].add(x); } dfs(0, -1); Arrays.sort(g, (a, b) -> Integer.compare(a.size(), b.size())); boolean visitedAll = true; for (int i = 0; i < n; i++) { if (used[i] == 0) { visitedAll = false; } } final String UNKNOWN = "unknown topology"; final String BUS = "bus topology"; final String RING = "ring topology"; final String STAR = "star topology"; if (!visitedAll) { println(UNKNOWN); return; } if (!hasCycle) { if (g[0].size() == 1 && g[1].size() == 1 && g[2].size() == 2 && g[n - 1].size() == 2) { println(BUS); } else { boolean allOne = true; for (int i = 0; i < n - 1; i++) { if (g[i].size() != 1) { allOne = false; } } if (allOne && g[n - 1].size() == n - 1) { print(STAR); } else { print(UNKNOWN); } } } else { if (g[0].size() == 2 && g[n - 1].size() == 2) { println(RING); } else { println(UNKNOWN); } } } Main() { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } Main(String fileIn, String fileOut) throws IOException { super(fileOut); in = new BufferedReader(new FileReader(fileIn)); } public static void main(String[] args) throws IOException { Main main; if ("_std".equals(IO)) { main = new Main(); } else if ("_iotxt".equals(IO)) { main = new Main("input.txt", "output.txt"); } else { main = new Main(IO + ".in", IO + ".out"); } main.solve(); main.close(); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int[] nextIntArray(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = nextInt(); } return a; } int[] nextIntArraySorted(int len) throws IOException { int[] a = nextIntArray(len); shuffle(a); Arrays.sort(a); return a; } void shuffle(int[] a) { for (int i = 1; i < a.length; i++) { int x = rand.nextInt(i + 1); int _ = a[i]; a[i] = a[x]; a[x] = _; } } void shuffleAndSort(int[] a) { shuffle(a); Arrays.sort(a); } boolean nextPermutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) { if (p[a] < p[a + 1]) for (int b = p.length - 1; ; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } return false; } <T> List<T>[] createAdjacencyList(int n) { List<T>[] res = new List[n]; for (int i = 0; i < n; i++) { res[i] = new ArrayList<>(); } return res; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
ccbc004a13a1e7d0a2cecf4d948e6e2d
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer sz; sz = new StringTokenizer(reader.readLine()); int v = Integer.parseInt(sz.nextToken()); int e = Integer.parseInt(sz.nextToken()); if (e > v || e < v - 1) { System.out.println("unknown topology"); return; } int freq[] = new int[v + 1]; int from,to; int max = Integer.MIN_VALUE; for (int i=1;i <= e;++i) { sz = new StringTokenizer(reader.readLine()); from = Integer.parseInt(sz.nextToken()); to = Integer.parseInt(sz.nextToken()); ++freq[to]; ++freq[from]; if (freq[to] > max) max = freq[to]; if (freq[from] > max) max = freq[from]; } if (max == v - 1 && e == v - 1) writer.print("star topology"); else { int freq1,freq2; freq1 = freq2 = 0; for (int i=1;i <= v;++i) { if (freq[i] == 1) ++freq1; else if (freq[i] == 2) ++freq2; else { System.out.println("unknown topology"); return; } } if (freq1 == 2 && e==v-1) writer.print("bus topology"); else if (freq1 == 0 && e==v) writer.print("ring topology"); else writer.print("unknown topology"); } writer.flush(); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
304da053ed83d150cb8fa5e54a6e2633
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class NetworkTopology { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); int[] degree = new int[n]; for (int i = 0; i < m; i++) { line = br.readLine().split(" "); int from = Integer.parseInt(line[0]); int to = Integer.parseInt(line[1]); degree[from-1]++; degree[to-1]++; } boolean bus = true, star = true; // bus int ends = 0; for (int i = 0; i < degree.length; i++) { if(degree[i] == 1) ends++; if(degree[i] > 2) bus = false; } if(bus && ends == 2){ System.out.println("bus topology"); return; } if(bus && ends == 0) { System.out.println("ring topology"); return; } //star boolean foundCenter = false; for (int i = 0; i < degree.length; i++) { if(degree[i] == n-1){ if(!foundCenter) foundCenter = true; else star = false; } else if(degree[i] != 1) { star = false; } } if(star) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
06ff24fa10290ffbe0e977a80baeda6a
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
//package bfs; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; //public class FastReader { //} class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Graph { int n; List<List<Integer>> adjList; boolean visited[]; int countNeighbours[]; Graph(int nodes) { n=nodes; visited = new boolean[n]; adjList = new ArrayList<>(n); countNeighbours = new int[n]; for(int i=0;i<n;i++) { adjList.add(new ArrayList<>()); } } void printList() { for(int i=0;i<n;i++) { System.out.print(i); List<Integer> list = adjList.get(i); for(int node:list) { System.out.print("->"+node); } System.out.println(); } } int[] getNeighbours() { return countNeighbours; } void addEdge(int m,int n) { adjList.get(m).add(n); adjList.get(n).add(m); countNeighbours[m]++; countNeighbours[n]++; } void bfs(int start) { Queue <Integer> q = new LinkedList<Integer>(); q.add(start); visited[start]=true; System.out.print(start+" "); while(!q.isEmpty()) { start=q.poll(); //visited[start]=true; //System.out.println(start+" "); List<Integer>adjNodes = adjList.get(start); for(int neighbour:adjNodes) { if(!visited[neighbour]) { q.add(neighbour); visited[neighbour]=true; System.out.print(neighbour+" "); } } } } } public class Main { public static void main(String args[]) { FastReader sc = new FastReader(); int n = sc.nextInt(); Graph g = new Graph(n); int m = sc.nextInt(); for(int i=0;i<m;i++) { int x = sc.nextInt(); int y = sc.nextInt(); g.addEdge(x-1,y-1); } //g.printList(); int count[] =g.getNeighbours(); //HashMap<Integer,Integer> map = new HashMap<>(); int ans[] = new int[3]; boolean possible=true; for(int i=0;i<n;i++) { //System.out.println("neighbours of "+i+"is "+count[i]); if(count[i]==1 || count[i]==2) { ans[count[i]-1]++; } else if(count[i]==n-1) { ans[2]=1; } else { possible=false; break; } } //System.out.println(map); if(!possible) { System.out.println("unknown topology"); } else if(ans[0]==2 && ans[1]==n-2) { System.out.println("bus topology"); } else if(ans[1]==n) { System.out.println("ring topology"); } else if(ans[2]==1 && ans[0]==n-1) { System.out.println("star topology"); } else { System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
7b6deb114c5f953c2593ee44e098b57f
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
//package CodeForces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner; public class NetworkTopology { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); // Scanner sc = new Scanner(System.in); // int n = sc.nextInt(), m = sc.nextInt(); int [] num = new int[n]; for(int i = 0; i<m; i++) { line = br.readLine().split(" "); int v = Integer.parseInt(line[0])-1; int u = Integer.parseInt(line[1])-1; num[v]++; num[u]++; } int one = 0, two = 0, nMinusMinus = 0; for(int i = 0; i<n; i++) { if(num[i] == 1) one++; else if(num[i] == 2) two++; else if(num[i] == n-1) nMinusMinus++; } if( one == 2 && two == n-2) System.out.println("bus topology"); else if(two == n) System.out.println("ring topology"); else if(nMinusMinus == 1 && one == n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
8644e0f5843ce1c658892bf60542841f
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
//package CodeForces; import java.util.ArrayList; import java.util.Scanner; public class NetworkTopology { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); ArrayList [] graph = new ArrayList[n]; for(int i = 0; i<n; i++) graph[i] = new ArrayList<>(); for(int i = 0; i<m; i++) { int v = sc.nextInt()-1, u = sc.nextInt()-1; graph[u].add(v); graph[v].add(u); } int one = 0, two = 0, nMinusMinus = 0; for(int i = 0; i<n; i++) { if(graph[i].size() == 1) one++; else if(graph[i].size() == 2) two++; else if(graph[i].size() == n-1) nMinusMinus++; } if( one == 2 && two == n-2) System.out.println("bus topology"); else if(two == n) System.out.println("ring topology"); else if(nMinusMinus == 1 && one == n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
1cda0928637943962b87b1c08f758625
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
//package CodeForces; import java.util.ArrayList; import java.util.Scanner; public class NetworkTopology { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int [] num = new int[n]; for(int i = 0; i<2*m; i++) { int v = sc.nextInt()-1; num[v]++; } int one = 0, two = 0, nMinusMinus = 0; for(int i = 0; i<n; i++) { if(num[i] == 1) one++; else if(num[i] == 2) two++; else if(num[i] == n-1) nMinusMinus++; } if( one == 2 && two == n-2) System.out.println("bus topology"); else if(two == n) System.out.println("ring topology"); else if(nMinusMinus == 1 && one == n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
a4d1d860a49b8a05f26b06e7122c8922
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
//package CodeForces; import java.util.ArrayList; import java.util.Scanner; public class NetworkTopology { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int [] num = new int[n]; for(int i = 0; i<2*m; i++) { int v = sc.nextInt()-1; num[v]++; } int [] arr = new int [3]; for(int i = 0; i<n; i++) { if(num[i] == 1) arr[0]++; else if(num[i] == 2) arr[1]++; else if(num[i] == n-1) arr[2]++; } if( arr[0] == 2 && arr[1] == n-2) System.out.println("bus topology"); else if(arr[1] == n) System.out.println("ring topology"); else if(arr[2] == 1 && arr[0] == n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
234b09ac5178b3497ef4a092be17031c
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main{ static int V , E ; static ArrayList<Integer> adjList[]; public static void main(String[] args)throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); V = sc.nextInt() ; E = sc.nextInt(); adjList = new ArrayList[V]; for(int i = 0 ; i < V ; ++i) adjList[i] = new ArrayList<Integer>(); while(E-->0){int u = sc.nextInt() , v = sc.nextInt(); --u;--v; adjList[u].add(v) ; adjList[v].add(u);} int cnt = 0 , cnt1 =0 , cntAll =0; for(int i = 0 ; i < V ; ++i) if(adjList[i].size() == 2) cnt++; else if(adjList[i].size() ==1 ) cnt1 ++; else if(adjList[i].size()==V-1) cntAll++; if(cnt == V -2 && cnt1 ==2) System.out.println("bus topology"); else if(cnt == V ) System.out.println("ring topology"); else if(cnt == 0 && cnt1 == V-1 && cntAll == 1) System.out.println("star topology"); else System.out.println("unknown topology"); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
c14eda5ae8bb7bc00cfa9826d44c89c4
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.IOException; public class Topology { public static void main(String[] args) throws IOException { Scanner in = new Scanner(); int n = in.nextInt(); int m = in.nextInt(); Graph topology = new Graph(n, m); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); topology.graph[u].add(v); topology.graph[v].add(u); } if (checkRing(topology)) println("ring topology"); else if (checkStar(topology)) println("star topology"); else if (checkBus(topology)) println("bus topology"); else println("unknown topology"); } /* [ PROPERTIES OF RING TOPOLOGY ] 1) |E| = |V| 2) deg(V) = 2 3) |V| > 2 */ static boolean checkRing(Graph topology) { if (topology.nNodes < 3) return false; if (topology.nEdges != topology.nNodes) return false; for (int i = 1; i <= topology.nNodes; i++) { if (topology.graph[i].size() != 2) return false; } return true; } /* [ PROPERTIES OF STAR ] 1) ONE NODE IS CENTRAL - HAVING DEGREE |V| - 1 2) NUMBER OF EDGES - |V| - 1 3) ALL OTHER NODES HAVE DEGREE 1 */ static boolean checkStar(Graph topology) { if (topology.nEdges != topology.nNodes - 1) return false; int singles = 0; int parents = 0; for (int i = 1; i <= topology.nNodes; i++) { if (topology.graph[i].size() == 1) singles++; else if (topology.graph[i].size() == topology.nNodes - 1) parents++; } if (parents != 1 || singles != topology.nNodes - 1) return false; return true; } /* [ PROPERTIES OF BUS TOPOLOGY ] 1) ALL VERTICES OTHER THAN THE 1st AND Nth VERTEX HAVE A DEGREE 2. 2) 1st AND Nth VERTEX HAVE A DEGREE OF 1 3) |E| = |V| - 1 */ static boolean checkBus(Graph topology) { if (topology.nEdges != topology.nNodes - 1) return false; int degOne = 0; int degTwo = 0; for (int i = 1; i <= topology.nNodes; i++) { if (topology.graph[i].size() == 1) degOne++; else if (topology.graph[i].size() == 2) degTwo++; } if (degOne != 2 || degTwo != topology.nNodes - 2) return false; return true; } static class Graph { ArrayList<Integer>[] graph; int nNodes = 0; int nEdges = 0; Graph(int n, int e) { graph = new ArrayList[n + 1]; nNodes = n; nEdges = e; for (int i = 0; i <= n; i++) { graph[i] = new ArrayList(); } } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static void println(Object obj) { System.out.println(obj); } static void print(Object obj) { System.out.print(obj); } /* CUSTOM IMPLEMENTATION OF Scanner FOR FASTER IO */ public static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk = null; String next() throws IOException { while (tk == null || !tk.hasMoreTokens()) { tk = new StringTokenizer(br.readLine()); } return tk.nextToken(); } String nextLine() throws IOException { String toReturn = new String(); try { toReturn = br.readLine(); } catch(Exception e) { e.printStackTrace(); } return toReturn; } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
d7f6c077deda3de10dbe078b121a9358
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] degree = new int[n + 1]; for (int i = 0; i < m; i++) { degree[in.nextInt()]++; degree[in.nextInt()]++; } if (ring(degree)) out.println("ring topology"); else if (star(degree)) out.println("star topology"); else if (bus(degree)) out.println("bus topology"); else out.println("unknown topology"); } boolean ring(int[] degree) { for (int i = 1; i < degree.length; i++) { if (degree[i] != 2) return false; } return true; } boolean star(int[] degree) { int center = 0; int leaf = 0; for (int i = 1; i < degree.length; i++) { if (degree[i] == 1) leaf++; if (degree[i] == degree.length - 2) center++; } if (center == 1 && leaf == degree.length - 2) return true; return false; } boolean bus(int[] degree) { for (int i = 1; i < degree.length; i++) { if (!(degree[i] == 2 || degree[i] == 1)) return false; } return true; } } static class InputReader { private StringTokenizer tokenizer; private BufferedReader reader; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private void fillTokenizer() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } } public String next() { fillTokenizer(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
6b26f741805e3fddbfee31ccf296dfcf
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class B2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] data = new int[n]; int max = 0; int twoTimes = 0; for(int i = 0 ; i<m; i++) { int k = sc.nextInt(); int v = sc.nextInt(); data[k-1]++; data[v-1]++; if(data[k-1] == 2)twoTimes++; if(data[v-1] == 2)twoTimes++; max = Math.max(max, data[k-1]); max = Math.max(max, data[v-1]); } if(max == 2 && twoTimes == n) { System.out.println("ring topology"); } else if(max == 2 && twoTimes == n-2) { System.out.println("bus topology"); } else if(max == m) { System.out.println("star topology"); } else { System.out.println("unknown topology"); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
fb135498259a2a40a82a07733ce5d6b7
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static StringBuilder out = new StringBuilder(); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static LinkedList<Integer> arr[]; static int degree[]; static int n,m,s,t; static int color[]; static int oo = 20000000; public static void main(String[]args)throws Throwable{ st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); arr = new LinkedList[n]; color = new int[n]; degree = new int[n]; for(int i=0;i<n;i++) arr[i] = new LinkedList(); for(int i=0;i<m;i++){ st = new StringTokenizer(br.readLine()); s= Integer.parseInt(st.nextToken())-1; t = Integer.parseInt(st.nextToken())-1; arr[s].add(t); arr[t].add(s); } solver(); System.out.print(out); } static void solver(){ calcDegree(); boolean found = false; if(bfs()){ if(star()) { out.append("star topology"); found = true; } else if(bus()) { out.append("bus topology"); found = true; } else if((n&1)==0) if(ring()){ out.append("ring topology"); found = true; } } else{ if(ring()){ out.append("ring topology"); found = true; } } if(!found)out.append("unknown topology"); } static boolean star(){ boolean found = false; for(int i=0;i<degree.length;i++){ if(degree[i]==n-1){ if(found) return false; else found = true; } else if(degree[i]!=1) return false; } return true; } static boolean ring(){ for(int i=0;i<degree.length;i++)if(degree[i]!=2) return false; return true; } static boolean bus(){ int ones = 0; for(int i=0;i<degree.length;i++){ if(degree[i]!=2){ if(degree[i]==1){ ones++; if(ones>2) return false;} else return false; } } if(ones!=2) return false; return true; } static void calcDegree(){ for(int i=0;i<n;i++) degree[i] = arr[i].size(); } static boolean bfs(){ Arrays.fill(color,oo); color[0] = 1; Queue<Integer> q = new LinkedList(); q.add(0); while(!q.isEmpty()){ int v = q.poll(); for(Integer k:arr[v] ){ if(color[k]==oo){ color[k]=1-color[v]; q.add(k); } else if(color[k]==color[v]) return false; } } return true; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
52ff542edda765af3b383c9784ea1b40
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static StringBuilder out = new StringBuilder(); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static LinkedList<Integer> arr[]; static int degree[]; static int n,m,s,t; static int color[]; static int oo = 20000000; public static void main(String[]args)throws Throwable{ st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); arr = new LinkedList[n]; color = new int[n]; degree = new int[n]; for(int i=0;i<n;i++) arr[i] = new LinkedList(); for(int i=0;i<m;i++){ st = new StringTokenizer(br.readLine()); s= Integer.parseInt(st.nextToken())-1; t = Integer.parseInt(st.nextToken())-1; arr[s].add(t); arr[t].add(s); } solver(); System.out.print(out); } static void solver(){ calcDegree(); boolean found = false; if(bfs()){ if(star()) { out.append("star topology"); found = true; } else if(bus()) { out.append("bus topology"); found = true; } else if((n&1)==0) if(ring()){ out.append("ring topology"); found = true; } } else{ if(((n&1)!=0)&&ring()){ out.append("ring topology"); found = true; } } if(!found)out.append("unknown topology"); } static boolean star(){ boolean found = false; for(int i=0;i<degree.length;i++){ if(degree[i]==n-1){ if(found) return false; else found = true; } else if(degree[i]!=1) return false; } return true; } static boolean ring(){ for(int i=0;i<degree.length;i++)if(degree[i]!=2) return false; return true; } static boolean bus(){ int ones = 0; for(int i=0;i<degree.length;i++){ if(degree[i]!=2){ if(degree[i]==1){ ones++; if(ones>2) return false;} else return false; } } if(ones!=2) return false; return true; } static void calcDegree(){ for(int i=0;i<n;i++) degree[i] = arr[i].size(); } static boolean bfs(){ Arrays.fill(color,oo); color[0] = 1; Queue<Integer> q = new LinkedList(); q.add(0); while(!q.isEmpty()){ int v = q.poll(); for(Integer k:arr[v] ){ if(color[k]==oo){ color[k]=1-color[v]; q.add(k); } else if(color[k]==color[v]) return false; } } return true; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
dcf486dc5d6ac8643f0f2dd0989e02e3
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
/** * _292B */ import java.util.*; public class _292B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); ArrayList<ArrayList<Integer>> adjList=new ArrayList<>(); for(int i=0;i<n;i++){ adjList.add(i,new ArrayList<>()); } for(int i=0;i<m;i++){ int src=sc.nextInt(); int des=sc.nextInt(); adjList.get(src-1).add(des-1); adjList.get(des-1).add(src-1); } // int degree[]=new int[n]; int count1=0,count2=0,countn=0; for(int i=0;i<n;i++){ int degree=adjList.get(i).size(); if(degree==1) count1++; else if(degree==2) count2++; else if(degree==n-1) countn++; } if(count1==2&&count2==(n-2)&&countn==0) System.out.println("bus topology"); else if(count1==(n-1)&&countn==1&&count2==0){ System.out.println("star topology"); } else if(count1==0&&count2==n&&countn==0) System.out.println("ring topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
cfe3b74d4e9a69c029097a13336a708d
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class NetworkTopology { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); ArrayList<Integer>[] adj = new ArrayList[n]; for (int i = 0; i < n; i++) adj[i] = new ArrayList<Integer>(); for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; adj[u].add(v); adj[v].add(u); } int[] degCount = new int[n]; for (int i = 0; i < n; i++) { degCount[adj[i].size()]++; } if (degCount[1] == 2 && degCount[2] == n - 2) System.out.println("bus topology"); else if (degCount[2] == n) System.out.println("ring topology"); else if (degCount[1] == n - 1 && degCount[n - 1] == 1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
5704465c00a54f280bb376040d261564
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class RookBishopKing { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int v = sc.nextInt(); int e = sc.nextInt(); int deg[] = new int[v]; for (int i=0;i<e;++i){ int a = sc.nextInt(); int b = sc.nextInt(); a--; b--; deg[a]++; deg[b]++; } boolean isStart = false; if (e==v-1){ for (int i=0;i<v;++i){ if (deg[i]==e){ isStart = true; } } } if (isStart) System.out.println("star topology"); else{ if (e!=v && e!=(v-1)){ System.out.println("unknown topology"); }else{ boolean isBus = true; int deg1 = 0,deg2=0; for (int i=0;i<v;++i){ if (deg[i] == 1) deg1++; else if (deg[i]==2) deg2++; else{ isBus = false; break; } } if (isBus == false) System.out.println("unknown topology"); else{ if (deg1==2) System.out.println("bus topology"); else System.out.println("ring topology"); } } } sc.close(); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
f5189fa8d524c5fa1f959e4a666671ca
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Task solver = new TaskB(); solver.solve(1, in, out); out.close(); } } interface Task { public void solve(int testNumber, InputReader in, OutputWriter out); } class TaskA implements Task { boolean[][] madj; int[] aux={0, 2, 3, 3, 2}; public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(), p=in.readInt(); madj=new boolean[n][n]; for (int i=0; i<n; i+=5) { int x=MiscUtils.min(i+5, n); for (int j=i; j<x; j++) for (int k=j+1; k<x; k++) madj[j][k]=madj[k][j]=true; if (x-i<5 && x-i>0) for (int j=0; j<aux[x-i]; j++) madj[j][i+j/2]=madj[i+j/2][j]=true; } for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) if (!madj[i][j] && p>0) { p--; madj[i][j]=madj[j][i]=true; } for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) if (madj[i][j]) out.printLine(i+1, j+1); } } class TaskB implements Task { int[] arr, rev; ArrayList<Pair<Integer, Integer>> list; public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(), m=in.readInt(); arr=new int[n]; rev=new int[m+1]; while (m-->0) { arr[in.readInt()-1]++; arr[in.readInt()-1]++; } for (int i=0; i<n; i++) rev[arr[i]]++; if (rev[2]==n-2 && rev[1]==2) out.print("bus"); else if (rev[2]==n) out.print("ring"); else if (rev[n-1]==1 && rev[1]==n-1) out.print("star"); else out.print("unknown"); out.printLine(" topology"); } } class TaskC implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { } } class TaskD implements Task { char[][] tab; Graph graph; int[] DX = { -1, -1, 1, 1 }, DY = { -1, 1, -1, 1 }; boolean[][] val; public void solve(int testNumber, InputReader in, OutputWriter out) { while (true) { int n=in.readInt(), m=in.readInt(), s=n*m, t=n*m+1, ret=0; if (n==0 && m==0) break; val=new boolean[n][m]; tab=IOUtils.readTable(in, n, m); graph=new Graph(t+1); for (int i=0; i<n; i++) for (int j=0; j<m; j++) { val[i][j]=tab[i][j]=='F'; for (int k=0; k<4; k++) { int i2=i+2*DX[k], j2=j+2*DY[k], i3=i+DX[k], j3=j+DY[k]; if (MiscUtils.isValidCell(i2, j2, n, m) && (tab[i2][j2]=='G' && tab[i3][j3]!='P')) val[i][j]=false; } if (val[i][j]) ret++; } for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (val[i][j]) { int u=m*i+j; if (i%4>1) { graph.addFlowEdge(u, t, 1); continue; } graph.addFlowEdge(s, u, 1); for (int k=0; k<4; k++) { int i2=i+2*DX[k], j2=j+2*DY[k], i3=i+DX[k], j3=j+DY[k], v=m*i2+j2; if (MiscUtils.isValidCell(i2, j2, n, m) && val[i2][j2] && tab[i3][j3]!='P') graph.addFlowEdge(u, v, 1); } } ret-=MaxFlow.dinic(graph, s, t); out.printLine(ret); } } } class TaskE implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { int x = in.readInt(), k = in.readInt(), p = in.readInt(), c = 0; for (; x % 2 == 0; x /= 2) c++; double prob = 0.01 * p, ret = Math.pow(prob, k) * (k + c); for (int i = 1; i <= k; i++) ret += (1.0 - prob) * Math.pow(prob, k - i) * (k - i); out.printLine(ret); } } class TaskF implements Task { public void solve(int testNumber, InputReader in, OutputWriter out) { } } class MaxFlow { private final Graph graph; private int source; private int destination; private int[] queue; private int[] distance; private int[] nextEdge; private MaxFlow(Graph graph, int source, int destination) { this.graph = graph; this.source = source; this.destination = destination; int vertexCount = graph.vertexCount(); queue = new int[vertexCount]; distance = new int[vertexCount]; nextEdge = new int[vertexCount]; } public static long dinic(Graph graph, int source, int destination) { return new MaxFlow(graph, source, destination).dinic(); } private long dinic() { long totalFlow = 0; while (true) { edgeDistances(); if (distance[destination] == -1) break; Arrays.fill(nextEdge, -2); totalFlow += dinicImpl(source, Long.MAX_VALUE); } return totalFlow; } private void edgeDistances() { Arrays.fill(distance, -1); distance[source] = 0; int size = 1; queue[0] = source; for (int i = 0; i < size; i++) { int current = queue[i]; int id = graph.firstOutbound(current); while (id != -1) { if (graph.capacity(id) != 0) { int next = graph.destination(id); if (distance[next] == -1) { distance[next] = distance[current] + 1; queue[size++] = next; } } id = graph.nextOutbound(id); } } } private long dinicImpl(int source, long flow) { if (source == destination) return flow; if (flow == 0 || distance[source] == distance[destination]) return 0; int id = nextEdge[source]; if (id == -2) nextEdge[source] = id = graph.firstOutbound(source); long totalPushed = 0; while (id != -1) { int nextDestinationID = graph.destination(id); if (graph.capacity(id) != 0 && distance[nextDestinationID] == distance[source] + 1) { long pushed = dinicImpl(nextDestinationID, Math.min(flow, graph.capacity(id))); if (pushed != 0) { graph.pushFlow(id, pushed); flow -= pushed; totalPushed += pushed; if (flow == 0) return totalPushed; } } nextEdge[source] = id = graph.nextOutbound(id); } return totalPushed; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(char[] array) { writer.print(array); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void print(long[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void print(Collection<Integer> collection) { boolean first = true; for (Integer iterator : collection) { if (first) first = false; else writer.print(' '); writer.print(iterator); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(long[] array) { print(array); writer.println(); } public void printLine(Collection<Integer> collection) { print(collection); writer.println(); } public void printLine() { writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void print(char i) { writer.print(i); } public void printLine(char i) { writer.println(i); } public void printLine(char[] array) { writer.println(array); } public void printFormat(String format, Object... objects) { writer.printf(format, objects); } public void close() { writer.close(); } public void flush() { writer.flush(); } public void print(long i) { writer.print(i); } public void printLine(long i) { writer.println(i); } public void print(int i) { writer.print(i); } public void printLine(int i) { writer.println(i); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class IOUtils { public static Pair<Integer, Integer> readIntPair(InputReader in) { int first = in.readInt(); int second = in.readInt(); return Pair.makePair(first, second); } public static Pair<Long, Long> readLongPair(InputReader in) { long first = in.readLong(); long second = in.readLong(); return Pair.makePair(first, second); } public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } public static long[] readLongArray(InputReader in, int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = in.readLong(); return array; } public static double[] readDoubleArray(InputReader in, int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) array[i] = in.readDouble(); return array; } public static String[] readStringArray(InputReader in, int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) array[i] = in.readString(); return array; } public static char[] readCharArray(InputReader in, int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) array[i] = in.readCharacter(); return array; } public static Pair<Integer, Integer>[] readIntPairArray(InputReader in, int size) { @SuppressWarnings({ "unchecked" }) Pair<Integer, Integer>[] result = new Pair[size]; for (int i = 0; i < size; i++) result[i] = readIntPair(in); return result; } public static Pair<Long, Long>[] readLongPairArray(InputReader in, int size) { @SuppressWarnings({ "unchecked" }) Pair<Long, Long>[] result = new Pair[size]; for (int i = 0; i < size; i++) result[i] = readLongPair(in); return result; } public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readInt(); } } public static void readLongArrays(InputReader in, long[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readLong(); } } public static void readDoubleArrays(InputReader in, double[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readDouble(); } } public static char[][] readTable(InputReader in, int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readCharArray(in, columnCount); return table; } public static int[][] readIntTable(InputReader in, int rowCount, int columnCount) { int[][] table = new int[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readIntArray(in, columnCount); return table; } public static double[][] readDoubleTable(InputReader in, int rowCount, int columnCount) { double[][] table = new double[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readDoubleArray(in, columnCount); return table; } public static long[][] readLongTable(InputReader in, int rowCount, int columnCount) { long[][] table = new long[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readLongArray(in, columnCount); return table; } public static String[][] readStringTable(InputReader in, int rowCount, int columnCount) { String[][] table = new String[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = readStringArray(in, columnCount); return table; } public static String readText(InputReader in) { StringBuilder result = new StringBuilder(); while (true) { int character = in.read(); if (character == '\r') continue; if (character == -1) break; result.append((char) character); } return result.toString(); } public static void readStringArrays(InputReader in, String[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) arrays[j][i] = in.readString(); } } public static void printTable(OutputWriter out, char[][] table) { for (char[] row : table) out.printLine(new String(row)); } } class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public final V second; public static <U, V> Pair<U, V> makePair(U first, V second) { return new Pair<U, V>(first, second); } private Pair(U first, V second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public Pair<V, U> swap() { return makePair(second, first); } @Override public String toString() { return "(" + first + "," + second + ")"; } @SuppressWarnings({ "unchecked" }) public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) return value; return ((Comparable<V>) second).compareTo(o.second); } } class BidirectionalGraph extends Graph { public int[] transposedEdge; public BidirectionalGraph(int vertexCount) { this(vertexCount, vertexCount); } public BidirectionalGraph(int vertexCount, int edgeCapacity) { super(vertexCount, 2 * edgeCapacity); transposedEdge = new int[2 * edgeCapacity]; } public static BidirectionalGraph createGraph(int vertexCount, int[] from, int[] to) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addSimpleEdge(from[i], to[i]); return graph; } public static BidirectionalGraph createWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addWeightedEdge(from[i], to[i], weight[i]); return graph; } public static BidirectionalGraph createFlowGraph(int vertexCount, int[] from, int[] to, long[] capacity) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowEdge(from[i], to[i], capacity[i]); return graph; } public static BidirectionalGraph createFlowWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight, long[] capacity) { BidirectionalGraph graph = new BidirectionalGraph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowWeightedEdge(from[i], to[i], weight[i], capacity[i]); return graph; } @Override public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { int lastEdgeCount = edgeCount; super.addEdge(fromID, toID, weight, capacity, reverseEdge); super.addEdge(toID, fromID, weight, capacity, reverseEdge == -1 ? -1 : reverseEdge + 1); this.transposedEdge[lastEdgeCount] = lastEdgeCount + 1; this.transposedEdge[lastEdgeCount + 1] = lastEdgeCount; return lastEdgeCount; } @Override protected int entriesPerEdge() { return 2; } @Override public final int transposed(int id) { return transposedEdge[id]; } @Override protected void ensureEdgeCapacity(int size) { if (size > edgeCapacity()) { super.ensureEdgeCapacity(size); transposedEdge = resize(transposedEdge, edgeCapacity()); } } } class Graph { public static final int REMOVED_BIT = 0; protected int vertexCount; protected int edgeCount; private int[] firstOutbound; private int[] firstInbound; private Edge[] edges; private int[] nextInbound; private int[] nextOutbound; private int[] from; private int[] to; private long[] weight; private long[] capacity; private int[] reverseEdge; private int[] flags; public Graph(int vertexCount) { this(vertexCount, vertexCount); } public Graph(int vertexCount, int edgeCapacity) { this.vertexCount = vertexCount; firstOutbound = new int[vertexCount]; Arrays.fill(firstOutbound, -1); from = new int[edgeCapacity]; to = new int[edgeCapacity]; nextOutbound = new int[edgeCapacity]; flags = new int[edgeCapacity]; } public static Graph createGraph(int vertexCount, int[] from, int[] to) { Graph graph = new Graph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addSimpleEdge(from[i], to[i]); return graph; } public static Graph createWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight) { Graph graph = new Graph(vertexCount, from.length); for (int i = 0; i < from.length; i++) graph.addWeightedEdge(from[i], to[i], weight[i]); return graph; } public static Graph createFlowGraph(int vertexCount, int[] from, int[] to, long[] capacity) { Graph graph = new Graph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowEdge(from[i], to[i], capacity[i]); return graph; } public static Graph createFlowWeightedGraph(int vertexCount, int[] from, int[] to, long[] weight, long[] capacity) { Graph graph = new Graph(vertexCount, from.length * 2); for (int i = 0; i < from.length; i++) graph.addFlowWeightedEdge(from[i], to[i], weight[i], capacity[i]); return graph; } public static Graph createTree(int[] parent) { Graph graph = new Graph(parent.length + 1, parent.length); for (int i = 0; i < parent.length; i++) graph.addSimpleEdge(parent[i], i + 1); return graph; } public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) { ensureEdgeCapacity(edgeCount + 1); if (firstOutbound[fromID] != -1) nextOutbound[edgeCount] = firstOutbound[fromID]; else nextOutbound[edgeCount] = -1; firstOutbound[fromID] = edgeCount; if (firstInbound != null) { if (firstInbound[toID] != -1) nextInbound[edgeCount] = firstInbound[toID]; else nextInbound[edgeCount] = -1; firstInbound[toID] = edgeCount; } this.from[edgeCount] = fromID; this.to[edgeCount] = toID; if (capacity != 0) { if (this.capacity == null) this.capacity = new long[from.length]; this.capacity[edgeCount] = capacity; } if (weight != 0) { if (this.weight == null) this.weight = new long[from.length]; this.weight[edgeCount] = weight; } if (reverseEdge != -1) { if (this.reverseEdge == null) { this.reverseEdge = new int[from.length]; Arrays.fill(this.reverseEdge, 0, edgeCount, -1); } this.reverseEdge[edgeCount] = reverseEdge; } if (edges != null) edges[edgeCount] = createEdge(edgeCount); return edgeCount++; } protected final GraphEdge createEdge(int id) { return new GraphEdge(id); } public final int addFlowWeightedEdge(int from, int to, long weight, long capacity) { if (capacity == 0) { return addEdge(from, to, weight, 0, -1); } else { int lastEdgeCount = edgeCount; addEdge(to, from, -weight, 0, lastEdgeCount + entriesPerEdge()); return addEdge(from, to, weight, capacity, lastEdgeCount); } } protected int entriesPerEdge() { return 1; } public final int addFlowEdge(int from, int to, long capacity) { return addFlowWeightedEdge(from, to, 0, capacity); } public final int addWeightedEdge(int from, int to, long weight) { return addFlowWeightedEdge(from, to, weight, 0); } public final int addSimpleEdge(int from, int to) { return addWeightedEdge(from, to, 0); } public final int vertexCount() { return vertexCount; } public final int edgeCount() { return edgeCount; } protected final int edgeCapacity() { return from.length; } public final Edge edge(int id) { initEdges(); return edges[id]; } public final int firstOutbound(int vertex) { int id = firstOutbound[vertex]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int nextOutbound(int id) { id = nextOutbound[id]; while (id != -1 && isRemoved(id)) id = nextOutbound[id]; return id; } public final int firstInbound(int vertex) { initInbound(); int id = firstInbound[vertex]; while (id != -1 && isRemoved(id)) id = nextInbound[id]; return id; } public final int nextInbound(int id) { initInbound(); id = nextInbound[id]; while (id != -1 && isRemoved(id)) id = nextInbound[id]; return id; } public final int source(int id) { return from[id]; } public final int destination(int id) { return to[id]; } public final long weight(int id) { if (weight == null) return 0; return weight[id]; } public final long capacity(int id) { if (capacity == null) return 0; return capacity[id]; } public final long flow(int id) { if (reverseEdge == null) return 0; return capacity[reverseEdge[id]]; } public final void pushFlow(int id, long flow) { if (flow == 0) return; if (flow > 0) { if (capacity(id) < flow) throw new IllegalArgumentException("Not enough capacity"); } else { if (flow(id) < -flow) throw new IllegalArgumentException("Not enough capacity"); } capacity[id] -= flow; capacity[reverseEdge[id]] += flow; } public int transposed(int id) { return -1; } public final int reverse(int id) { if (reverseEdge == null) return -1; return reverseEdge[id]; } public final void addVertices(int count) { ensureVertexCapacity(vertexCount + count); Arrays.fill(firstOutbound, vertexCount, vertexCount + count, -1); if (firstInbound != null) Arrays.fill(firstInbound, vertexCount, vertexCount + count, -1); vertexCount += count; } protected final void initEdges() { if (edges == null) { edges = new Edge[from.length]; for (int i = 0; i < edgeCount; i++) edges[i] = createEdge(i); } } public final void removeVertex(int vertex) { int id = firstOutbound[vertex]; while (id != -1) { removeEdge(id); id = nextOutbound[id]; } initInbound(); id = firstInbound[vertex]; while (id != -1) { removeEdge(id); id = nextInbound[id]; } } private void initInbound() { if (firstInbound == null) { firstInbound = new int[firstOutbound.length]; Arrays.fill(firstInbound, 0, vertexCount, -1); nextInbound = new int[from.length]; for (int i = 0; i < edgeCount; i++) { nextInbound[i] = firstInbound[to[i]]; firstInbound[to[i]] = i; } } } public final boolean flag(int id, int bit) { return (flags[id] >> bit & 1) != 0; } public final void setFlag(int id, int bit) { flags[id] |= 1 << bit; } public final void removeFlag(int id, int bit) { flags[id] &= -1 - (1 << bit); } public final void removeEdge(int id) { setFlag(id, REMOVED_BIT); } public final void restoreEdge(int id) { removeFlag(id, REMOVED_BIT); } public final boolean isRemoved(int id) { return flag(id, REMOVED_BIT); } public final Iterable<Edge> outbound(final int id) { initEdges(); return new Iterable<Edge>() { public Iterator<Edge> iterator() { return new EdgeIterator(id, firstOutbound, nextOutbound); } }; } public final Iterable<Edge> inbound(final int id) { initEdges(); initInbound(); return new Iterable<Edge>() { public Iterator<Edge> iterator() { return new EdgeIterator(id, firstInbound, nextInbound); } }; } protected void ensureEdgeCapacity(int size) { if (from.length < size) { int newSize = Math.max(size, 2 * from.length); if (edges != null) edges = resize(edges, newSize); from = resize(from, newSize); to = resize(to, newSize); nextOutbound = resize(nextOutbound, newSize); if (nextInbound != null) nextInbound = resize(nextInbound, newSize); if (weight != null) weight = resize(weight, newSize); if (capacity != null) capacity = resize(capacity, newSize); if (reverseEdge != null) reverseEdge = resize(reverseEdge, newSize); flags = resize(flags, newSize); } } private void ensureVertexCapacity(int size) { if (firstOutbound.length < size) { int newSize = Math.max(size, 2 * from.length); firstOutbound = resize(firstOutbound, newSize); if (firstInbound != null) firstInbound = resize(firstInbound, newSize); } } protected final int[] resize(int[] array, int size) { int[] newArray = new int[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private long[] resize(long[] array, int size) { long[] newArray = new long[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } private Edge[] resize(Edge[] array, int size) { Edge[] newArray = new Edge[size]; System.arraycopy(array, 0, newArray, 0, array.length); return newArray; } public final boolean isSparse() { return vertexCount == 0 || edgeCount * 20 / vertexCount <= vertexCount; } protected class GraphEdge implements Edge { protected int id; protected GraphEdge(int id) { this.id = id; } public int getSource() { return source(id); } public int getDestination() { return destination(id); } public long getWeight() { return weight(id); } public long getCapacity() { return capacity(id); } public long getFlow() { return flow(id); } public void pushFlow(long flow) { Graph.this.pushFlow(id, flow); } public boolean getFlag(int bit) { return flag(id, bit); } public void setFlag(int bit) { Graph.this.setFlag(id, bit); } public void removeFlag(int bit) { Graph.this.removeFlag(id, bit); } public int getTransposedID() { return transposed(id); } public Edge getTransposedEdge() { int reverseID = getTransposedID(); if (reverseID == -1) return null; initEdges(); return edge(reverseID); } public int getReverseID() { return reverse(id); } public Edge getReverseEdge() { int reverseID = getReverseID(); if (reverseID == -1) return null; initEdges(); return edge(reverseID); } public int getID() { return id; } public void remove() { removeEdge(id); } public void restore() { restoreEdge(id); } } public class EdgeIterator implements Iterator<Edge> { private int edgeID; private final int[] next; private int lastID = -1; public EdgeIterator(int id, int[] first, int[] next) { this.next = next; edgeID = nextEdge(first[id]); } private int nextEdge(int id) { while (id != -1 && isRemoved(id)) id = next[id]; return id; } public boolean hasNext() { return edgeID != -1; } public Edge next() { if (edgeID == -1) throw new NoSuchElementException(); lastID = edgeID; edgeID = nextEdge(next[lastID]); return edges[lastID]; } public void remove() { if (lastID == -1) throw new IllegalStateException(); removeEdge(lastID); lastID = -1; } } } interface Edge { public int getSource(); public int getDestination(); public long getWeight(); public long getCapacity(); public long getFlow(); public void pushFlow(long flow); public boolean getFlag(int bit); public void setFlag(int bit); public void removeFlag(int bit); public int getTransposedID(); public Edge getTransposedEdge(); public int getReverseID(); public Edge getReverseEdge(); public int getID(); public void remove(); public void restore(); } class MiscUtils { public static final int[] DX4 = { 1, 0, -1, 0 }; public static final int[] DY4 = { 0, -1, 0, 1 }; public static final int[] DX8 = { 1, 1, 1, 0, -1, -1, -1, 0 }; public static final int[] DY8 = { -1, 0, 1, 1, 1, 0, -1, -1 }; public static final int[] DX_KNIGHT = { 2, 1, -1, -2, -2, -1, 1, 2 }; public static final int[] DY_KNIGHT = { 1, 2, 2, 1, -1, -2, -2, -1 }; private static final String[] ROMAN_TOKENS = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; private static final int[] ROMAN_VALUES = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; public static long josephProblem(long n, int k) { if (n == 1) return 0; if (k == 1) return n - 1; if (k > n) return (josephProblem(n - 1, k) + k) % n; long count = n / k; long result = josephProblem(n - count, k); result -= n % k; if (result < 0) result += n; else result += result / (k - 1); return result; } public static boolean isValidCell(int row, int column, int rowCount, int columnCount) { return row >= 0 && row < rowCount && column >= 0 && column < columnCount; } public static List<Integer> getPath(int[] last, int destination) { List<Integer> path = new ArrayList<Integer>(); while (destination != -1) { path.add(destination); destination = last[destination]; } Collections.reverse(path); return path; } public static List<Integer> getPath(int[][] lastIndex, int[][] lastPathNumber, int destination, int pathNumber) { List<Integer> path = new ArrayList<Integer>(); while (destination != -1 || pathNumber != 0) { path.add(destination); int nextDestination = lastIndex[destination][pathNumber]; pathNumber = lastPathNumber[destination][pathNumber]; destination = nextDestination; } Collections.reverse(path); return path; } public static long maximalRectangleSum(long[][] array) { int n = array.length; int m = array[0].length; long[][] partialSums = new long[n + 1][m + 1]; for (int i = 0; i < n; i++) { long rowSum = 0; for (int j = 0; j < m; j++) { rowSum += array[i][j]; partialSums[i + 1][j + 1] = partialSums[i][j + 1] + rowSum; } } long result = Long.MIN_VALUE; for (int i = 0; i < m; i++) { for (int j = i; j < m; j++) { long minPartialSum = 0; for (int k = 1; k <= n; k++) { long current = partialSums[k][j + 1] - partialSums[k][i]; result = Math.max(result, current - minPartialSum); minPartialSum = Math.min(minPartialSum, current); } } } return result; } public static int parseIP(String ip) { String[] components = ip.split("[.]"); int result = 0; for (int i = 0; i < 4; i++) result += (1 << (24 - 8 * i)) * Integer.parseInt(components[i]); return result; } public static String buildIP(int mask) { StringBuilder result = new StringBuilder(); for (int i = 0; i < 4; i++) { if (i != 0) result.append('.'); result.append(mask >> (24 - 8 * i) & 255); } return result.toString(); } public static long binarySearch(long from, long to, Function<Long, Boolean> function) { while (from < to) { long argument = from + (to - from) / 2; if (function.value(argument)) to = argument; else from = argument + 1; } return from; } public static <T> boolean equals(T first, T second) { return first == null && second == null || first != null && first.equals(second); } public static boolean isVowel(char ch) { ch = Character.toUpperCase(ch); return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y'; } public static boolean isStrictVowel(char ch) { ch = Character.toUpperCase(ch); return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'; } public static String convertToRoman(int number) { StringBuilder result = new StringBuilder(); for (int i = 0; i < ROMAN_TOKENS.length; i++) { while (number >= ROMAN_VALUES[i]) { number -= ROMAN_VALUES[i]; result.append(ROMAN_TOKENS[i]); } } return result.toString(); } public static int convertFromRoman(String number) { int result = 0; for (int i = 0; i < ROMAN_TOKENS.length; i++) { while (number.startsWith(ROMAN_TOKENS[i])) { number = number.substring(ROMAN_TOKENS[i].length()); result += ROMAN_VALUES[i]; } } return result; } public static int distance(int x1, int y1, int x2, int y2) { int dx = x1 - x2; int dy = y1 - y2; return dx * dx + dy * dy; } public static <T extends Comparable<T>> T min(T first, T second) { if (first.compareTo(second) <= 0) return first; return second; } public static <T extends Comparable<T>> T max(T first, T second) { if (first.compareTo(second) <= 0) return second; return first; } public static void decreaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) array[i]--; } } public static int[] getIntArray(String s) { String[] tokens = s.split(" "); int[] result = new int[tokens.length]; for (int i = 0; i < result.length; i++) result[i] = Integer.parseInt(tokens[i]); return result; } } interface Function<A, V> { public abstract V value(A argument); } interface IntComparator { public static final IntComparator DEFAULT = new IntComparator() { public int compare(int first, int second) { if (first < second) return -1; if (first > second) return 1; return 0; } }; public static final IntComparator REVERSE = new IntComparator() { public int compare(int first, int second) { if (first < second) return 1; if (first > second) return -1; return 0; } }; public int compare(int first, int second); } class DFSOrder { public final int[] position; public final int[] end; public DFSOrder(Graph graph) { this(graph, 0); } public DFSOrder(Graph graph, int root) { int count = graph.vertexCount(); position = new int[count]; end = new int[count]; int[] edge = new int[count]; int[] stack = new int[count]; for (int i = 0; i < count; i++) edge[i] = graph.firstOutbound(i); stack[0] = root; int size = 1; position[root] = 0; int index = 0; while (size > 0) { int current = stack[size - 1]; if (edge[current] == -1) { end[current] = index; size--; } else { int next = graph.destination(edge[current]); edge[current] = graph.nextOutbound(edge[current]); position[next] = ++index; stack[size++] = next; } } } public DFSOrder(BidirectionalGraph graph) { this(graph, 0); } public DFSOrder(BidirectionalGraph graph, int root) { int count = graph.vertexCount(); position = new int[count]; end = new int[count]; int[] edge = new int[count]; int[] stack = new int[count]; int[] last = new int[count]; for (int i = 0; i < count; i++) edge[i] = graph.firstOutbound(i); stack[0] = root; last[root] = -1; int size = 1; position[root] = 0; int index = 0; while (size > 0) { int current = stack[size - 1]; if (edge[current] == -1) { end[current] = index; size--; } else { int next = graph.destination(edge[current]); if (next == last[current]) { edge[current] = graph.nextOutbound(edge[current]); continue; } edge[current] = graph.nextOutbound(edge[current]); position[next] = ++index; last[next] = current; stack[size++] = next; } } } } abstract class IntervalTree { protected int size; protected IntervalTree(int size) { this(size, true); } public IntervalTree(int size, boolean shouldInit) { this.size = size; int nodeCount = Math.max(1, Integer.highestOneBit(size) << 2); initData(size, nodeCount); if (shouldInit) init(); } protected abstract void initData(int size, int nodeCount); protected abstract void initAfter(int root, int left, int right, int middle); protected abstract void initBefore(int root, int left, int right, int middle); protected abstract void initLeaf(int root, int index); protected abstract void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle); protected abstract void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle); protected abstract void updateFull(int root, int left, int right, int from, int to, long delta); protected abstract long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult); protected abstract void queryPreProcess(int root, int left, int right, int from, int to, int middle); protected abstract long queryFull(int root, int left, int right, int from, int to); protected abstract long emptySegmentResult(); public void init() { if (size == 0) return; init(0, 0, size - 1); } private void init(int root, int left, int right) { if (left == right) { initLeaf(root, left); } else { int middle = (left + right) >> 1; initBefore(root, left, right, middle); init(2 * root + 1, left, middle); init(2 * root + 2, middle + 1, right); initAfter(root, left, right, middle); } } public void update(int from, int to, long delta) { update(0, 0, size - 1, from, to, delta); } protected void update(int root, int left, int right, int from, int to, long delta) { if (left > to || right < from) return; if (left >= from && right <= to) { updateFull(root, left, right, from, to, delta); return; } int middle = (left + right) >> 1; updatePreProcess(root, left, right, from, to, delta, middle); update(2 * root + 1, left, middle, from, to, delta); update(2 * root + 2, middle + 1, right, from, to, delta); updatePostProcess(root, left, right, from, to, delta, middle); } public long query(int from, int to) { return query(0, 0, size - 1, from, to); } protected long query(int root, int left, int right, int from, int to) { if (left > to || right < from) return emptySegmentResult(); if (left >= from && right <= to) return queryFull(root, left, right, from, to); int middle = (left + right) >> 1; queryPreProcess(root, left, right, from, to, middle); long leftResult = query(2 * root + 1, left, middle, from, to); long rightResult = query(2 * root + 2, middle + 1, right, from, to); return queryPostProcess(root, left, right, from, to, middle, leftResult, rightResult); } } class LCA { private final long[] order; private final int[] position; private final IntervalTree lcaTree; private final int[] level; public LCA(Graph graph) { this(graph, 0); } public LCA(Graph graph, int root) { order = new long[2 * graph.vertexCount() - 1]; position = new int[graph.vertexCount()]; level = new int[graph.vertexCount()]; int[] index = new int[graph.vertexCount()]; for (int i = 0; i < index.length; i++) index[i] = graph.firstOutbound(i); int[] last = new int[graph.vertexCount()]; int[] stack = new int[graph.vertexCount()]; stack[0] = root; int size = 1; int j = 0; last[root] = -1; Arrays.fill(position, -1); while (size > 0) { int vertex = stack[--size]; if (position[vertex] == -1) position[vertex] = j; order[j++] = vertex; if (last[vertex] != -1) level[vertex] = level[last[vertex]] + 1; while (index[vertex] != -1 && last[vertex] == graph.destination(index[vertex])) index[vertex] = graph.nextOutbound(index[vertex]); if (index[vertex] != -1) { stack[size++] = vertex; stack[size++] = graph.destination(index[vertex]); last[graph.destination(index[vertex])] = vertex; index[vertex] = graph.nextOutbound(index[vertex]); } } lcaTree = new ReadOnlyIntervalTree(order) { @Override protected long joinValue(long left, long right) { if (left == -1) return right; if (right == -1) return left; if (level[((int) left)] < level[((int) right)]) return left; return right; } @Override protected long neutralValue() { return -1; } }; lcaTree.init(); } public int getPosition(int vertex) { return position[vertex]; } public int getLCA(int first, int second) { return (int) lcaTree.query(Math.min(position[first], position[second]), Math.max(position[first], position[second])); } public int getLevel(int vertex) { return level[vertex]; } public int getPathLength(int first, int second) { return level[first] + level[second] - 2 * level[getLCA(first, second)]; } } abstract class LongIntervalTree extends IntervalTree { protected long[] value; protected long[] delta; protected LongIntervalTree(int size) { this(size, true); } public LongIntervalTree(int size, boolean shouldInit) { super(size, shouldInit); } @Override protected void initData(int size, int nodeCount) { value = new long[nodeCount]; delta = new long[nodeCount]; } protected abstract long joinValue(long left, long right); protected abstract long joinDelta(long was, long delta); protected abstract long accumulate(long value, long delta, int length); protected abstract long neutralValue(); protected abstract long neutralDelta(); protected long initValue(int index) { return neutralValue(); } @Override protected void initAfter(int root, int left, int right, int middle) { value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]); delta[root] = neutralDelta(); } @Override protected void initBefore(int root, int left, int right, int middle) { } @Override protected void initLeaf(int root, int index) { value[root] = initValue(index); delta[root] = neutralDelta(); } @Override protected void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle) { value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]); } @Override protected void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle) { pushDown(root, left, middle, right); } protected void pushDown(int root, int left, int middle, int right) { value[2 * root + 1] = accumulate(value[2 * root + 1], delta[root], middle - left + 1); value[2 * root + 2] = accumulate(value[2 * root + 2], delta[root], right - middle); delta[2 * root + 1] = joinDelta(delta[2 * root + 1], delta[root]); delta[2 * root + 2] = joinDelta(delta[2 * root + 2], delta[root]); delta[root] = neutralDelta(); } @Override protected void updateFull(int root, int left, int right, int from, int to, long delta) { value[root] = accumulate(value[root], delta, right - left + 1); this.delta[root] = joinDelta(this.delta[root], delta); } @Override protected long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult) { return joinValue(leftResult, rightResult); } @Override protected void queryPreProcess(int root, int left, int right, int from, int to, int middle) { pushDown(root, left, middle, right); } @Override protected long queryFull(int root, int left, int right, int from, int to) { return value[root]; } @Override protected long emptySegmentResult() { return neutralValue(); } } class SumIntervalTree extends LongIntervalTree { public SumIntervalTree(int size) { super(size); } @Override protected long joinValue(long left, long right) { return left + right; } @Override protected long joinDelta(long was, long delta) { return was + delta; } @Override protected long accumulate(long value, long delta, int length) { return value + delta * length; } @Override protected long neutralValue() { return 0; } @Override protected long neutralDelta() { return 0; } } abstract class ReadOnlyIntervalTree extends IntervalTree { protected long[] value; protected long[] array; protected ReadOnlyIntervalTree(long[] array) { super(array.length, false); this.array = array; init(); } @Override protected void initData(int size, int nodeCount) { value = new long[nodeCount]; } @Override protected void initAfter(int root, int left, int right, int middle) { value[root] = joinValue(value[2 * root + 1], value[2 * root + 2]); } @Override protected void initBefore(int root, int left, int right, int middle) { } @Override protected void initLeaf(int root, int index) { value[root] = array[index]; } @Override protected void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle) { throw new UnsupportedOperationException(); } @Override protected void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle) { throw new UnsupportedOperationException(); } @Override protected void updateFull(int root, int left, int right, int from, int to, long delta) { throw new UnsupportedOperationException(); } @Override protected long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult) { return joinValue(leftResult, rightResult); } @Override protected void queryPreProcess(int root, int left, int right, int from, int to, int middle) { } @Override protected long queryFull(int root, int left, int right, int from, int to) { return value[root]; } @Override protected long emptySegmentResult() { return neutralValue(); } @Override public void update(int from, int to, long delta) { throw new UnsupportedOperationException(); } @Override protected void update(int root, int left, int right, int from, int to, long delta) { throw new UnsupportedOperationException(); } protected abstract long neutralValue(); protected abstract long joinValue(long left, long right); }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
f03ea110f74b553d09eb7d5d32c7d6fe
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; public class b292{ public static void main(String[] args) throws Exception { //new FileInputStream("input.txt"); //new FileOutputStream("output.txt") InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n=in.ri(); int m=in.ri(); int d[]=new int[n+2]; for(int i=0;i<m;i++) { d[in.ri()]+=1; d[in.ri()]+=1; } int flag=0; int cnt1=0,cnt2=0,cntn=0; for(int i=1;i<=n;i++) { if(d[i]==2)cnt2+=1; if(d[i]==1)cnt1+=1; if(d[i]==n-1)cntn+=1; } if(cnt2==n&&cnt1==0&&cntn==0)out.pl("ring topology"); else if(cnt2==n-2&&cnt1==2&&cntn==0)out.pl("bus topology"); else if (cnt2==0&&cnt1==n-1&&cntn==1)out.pl("star topology"); else out.pl("unknown topology"); //out.pl(sum); out.close(); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int ri() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String rs() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } private static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void pf(double n) { writer.printf("%.2f%n", n); } public void pl(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
e98fcfc6e11aa0e299c60791d04d6f9c
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; public class Network_Tropology { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < m; ++i) { ++a[sc.nextInt() - 1]; ++a[sc.nextInt() - 1]; } sc.close(); int min = m, minCounter = 0, max = 0, maxCounter = 0; for (int i = 0; i < n; ++i) { if (a[i] > max) { max = a[i]; maxCounter = 1; } else if (a[i] == max) { ++maxCounter; } if (a[i] < min) { min = a[i]; minCounter = 1; } else if (a[i] == min) { ++minCounter; } } String type = null; if (max == 2 && max == min) { type = "ring"; } else if (max == 2 && min == 1 && minCounter == 2 && maxCounter == n - 2) { type = "bus"; } else if (max == n - 1 && min == 1 && minCounter == n - 1 && maxCounter == 1) { type = "star"; } System.out.print(type != null ? type : "unknown"); System.out.println(" topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
501fc76d349722fcd5e9a00744a087b6
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); String s=in.nextLine(); String[]parts=s.split(" "); int n=Integer.parseInt(parts[0]); int m=Integer.parseInt(parts[1]); Node[]graph=new Node[n]; for(int i=0;i<n;i++) { graph[i]=new Node(); } for(int i=0;i<m;i++) { int a=in.nextInt()-1; int b=in.nextInt()-1; graph[a].sosedi.add(b); graph[b].sosedi.add(a); } int temp=0; boolean flag=true; for(int i=0;i<n;i++) { if(graph[i].sosedi.size()!=2) { flag=false; } } if(flag) { System.out.println("ring topology"); return; } flag=true; int br=0;int br2=0; for(int i=0;i<n;i++) { if(graph[i].sosedi.size()==1) { br++; if(br>2) break; } else if(graph[i].sosedi.size()==2) { br2++; } } if(br2+br==n) { System.out.println("bus topology"); return; } flag=true; br=0; br2=0; for(int i=0;i<n;i++) { if(graph[i].sosedi.size()==1) { br++; if(br>n-1) break; } else if(graph[i].sosedi.size()==n-1) { br2++; if(br2>1) break; } } if(br2+br==n) { System.out.println("star topology"); return; } System.out.println("unknown topology"); } } class Node { public ArrayList<Integer> sosedi; public Node() { sosedi=new ArrayList<Integer>(); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
93c24a23e6fd1f7c7f65cb4f6f7fbaf2
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class NetworkTopology { public static final String[] ans = {"unknown topology", "bus topology", "ring topology", "star topology"}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split("\\s+"); int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]); int[] fre = new int[n+1]; for(int i = 0 ; i < m ; i++){ s = br.readLine().split("\\s+"); int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]); fre[u]++; fre[v]++; } int min = Integer.MAX_VALUE, max = 0, ones = 0, twos = 0, moreThanTwos = 0; for(int i = 0 ; i <= n ; i++){ if(fre[i] == 0) continue; if(fre[i] == 1) ones++; if(fre[i] == 2) twos++; if(fre[i] > 2) moreThanTwos++; min = Math.min(min, fre[i]); max = Math.min(max, fre[i]); } if(ones == 2 && moreThanTwos == 0) System.out.println(ans[1]); else if(ones == 0 && moreThanTwos == 0) System.out.println(ans[2]); else if(twos == 0 && moreThanTwos == 1) System.out.println(ans[3]); else System.out.println(ans[0]); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
fb5a5d0eea2adbbc0863fa9819132a8e
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class a { public static LinkedList<Integer>ll[]; public static int count=0; public static int vertices[]; public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int m=s.nextInt(); ll=new LinkedList[n]; for(int i=0;i<n;i++) ll[i]=new LinkedList<Integer>(); for(int i=0;i<m;i++){ int x=s.nextInt(); int y=s.nextInt(); ll[x-1].add(y-1); ll[y-1].add(x-1); } int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=ll[i].size(); Arrays.sort(a); int flag=0; if(ring(a)){ System.out.println("ring topology"); flag=1; } if(star(a)){ System.out.println("star topology"); flag=1; } if(line(a)){ System.out.println("bus topology"); flag=1; } if(flag==0) System.out.println("unknown topology"); } public static boolean ring(int a[]){ for(int i=0;i<a.length;i++) if(a[i]!=2) return false; return true; } public static boolean line(int a[]){ if(!(a[0]==1&&a[1]==1)) return false; for(int i=2;i<a.length;i++) if(a[i]!=2) return false; return true; } public static boolean star(int a[]){ for(int i=2;i<a.length-1;i++) if(a[i]!=1) return false; if(a[a.length-1]!=a.length-1) return false; return true; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
a603d9aace6a9736a5800c39f61d00ff
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import com.sun.org.apache.xalan.internal.xsltc.trax.XSLTCSource; import org.omg.CORBA.INTERNAL; import java.awt.*; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.List; public class AA implements Runnable { static int mod9=1000000007; public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int i=0,j=0,k=0; int t=0; //t=sc.nextInt(); while (t-->0) { } int n=sc.nextInt(); int edges=sc.nextInt(); List<ArrayList<Integer>> nodes=new ArrayList<>(); for (i=0;i<=n;i++) { ArrayList<Integer> temp=new ArrayList<Integer>(); nodes.add(temp); } for (i=0;i<edges;i++) { int a=sc.nextInt(); int b=sc.nextInt(); nodes.get(a).add(b); nodes.get(b).add(a); } int ones=0; int twos=0; int threes=0; for (i=1;i<=n;i++) { int temp=nodes.get(i).size(); if (temp==1) { ones++; } else if (temp==2) twos++; else threes++; } if (threes==1&&ones==n-1) out.println("star topology"); else if (twos==n) out.println("ring topology"); else if (ones==2&&twos==n-2) out.println("bus topology"); else out.println("unknown topology"); //======================================================================================================================================= out.flush(); out.close(); } //======================================================================================================================================= static boolean isValid(Pair x,int n,int m) { return x.a>=0 && x.a<n && x.b>=0 && x.b<m; } static int dx[]={0,0,1,-1}; static int dy[]={1,-1,0,0}; static class Pair { int a,b; Pair(int aa,int bb) { a=aa; b=bb; } public String toString() { return a+" "+b; } } int[] sa(int n,InputReader sc) { int arr[]=new int[n]; for (int i=0;i<n;i++) arr[i]=sc.nextInt(); return arr; } static class PairSort implements Comparator<Pair> { public int compare(Pair a,Pair b) { return b.b-a.b; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new AA(),"Main",1<<27).start(); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
52dd32f659304c17f781c1dff249c0ed
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
/* @author : Abhimanyu Chauhan */ import java.util.*; import java.io.*; public class TaskB implements Runnable{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } Reader scan; PrintWriter out; @Override public void run() { try{ scan = new Reader(); out = new PrintWriter(System.out); solve(); out.flush(); }catch(IOException ex){ ex.printStackTrace(); } } void solve() throws IOException{ int n = scan.nextInt(); int m = scan.nextInt(); int deg[] = new int[n]; for(int i=0;i<m;i++){ int u = scan.nextInt()-1; int v = scan.nextInt()-1; deg[u]++; deg[v]++; } int count1=0,count2=0,count3=0; for(int i=0;i<n;i++){ if(deg[i]==1){ count1++; } if(deg[i]==2) count2++; if(deg[i]==n-1) count3++; } //check for bus: if(count1==2 && count2==n-2) out.println("bus topology"); else if(count2==n && count1==0) out.println("ring topology"); else if(count3==1 && count1==n-1) out.println("star topology"); else{ out.println("unknown topology"); } } public static void main(String[] args) throws IOException { new Thread(null,new TaskB(),"Main",1<<26).run();; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
09063aadc66f91ac9d1a2e3ee3d0c9bd
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; public class CF292B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] degs = new int[n]; for (int i = 0; i < 2*m; ++i) degs[in.nextInt()-1]++; int[] cnts = new int[4]; for (int i = 0; i < n; ++i) { if (degs[i] <= 2) cnts[degs[i]]++; else cnts[3]++; } if (cnts[2] == n) System.out.println("ring topology"); else if (cnts[1] + cnts[2] == n) System.out.println("bus topology"); else if (cnts[1] == n-1) System.out.println("star topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
c10de68f8936ba88d6b2d6a641ca8ece
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int degrees[] = new int[100000+5]; int n = sc.nextInt(); int m = sc.nextInt(); int deg1 = 0; int deg2 = 0; int degn1 = 0; for(int i = 0; i < m; i++){ int a = sc.nextInt(); int b = sc.nextInt(); degrees[a]++; degrees[b]++; } //start from 1, since no 0 value node for (int i = 1; i <= n; i++) { //if a node has degree of 1, add to ctr if (degrees[i] == 1) deg1++; //if a node has degree of 2, add to ctr if (degrees[i] == 2) deg2++; //if a node has degree n-1, add to ctr if (degrees[i] == n - 1) degn1++; } //observations: bus topo has 2 nodes with deg1 and remaining is deg2 if (deg1 == 2 && deg2 == n - 2) System.out.println("bus topology"); //a ring topo has all nodes with deg2 else if (deg2 == n) System.out.println("ring topology"); //a star topo has a nodes with n-1 deg, the rest have deg1 else if (degn1 == 1 && deg1 == n-1) System.out.println("star topology"); //else, we dont know what it is else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
aa493fba2fe69585ded1b9d70067456d
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static ArrayList<Integer> [] a; static boolean[] visited; static int l=0,c=-1; public static void main (String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); int n = fr.nextInt(),m=fr.nextInt(); a = new ArrayList[n]; visited = new boolean[n]; for(int i=0;i<n;i++){ a[i] = new ArrayList<>(); } for(int i=0;i<m;i++){ int u=fr.nextInt()-1,v = fr.nextInt()-1; a[u].add(v);a[v].add(u); } dfs(1); if(n==m||n-m==1){ if(n==m){ if(l==0&&c==-1){ System.out.println("ring topology"); }else{ System.out.println("unknown topology"); } }else if(n-m==1){ if(l==2&&c==-1){ System.out.println("bus topology"); }else if(c!=-1&&l==n-1&&a[c].size()==n-1){ System.out.println("star topology"); }else{ System.out.println("unknown topology"); } } }else{ System.out.println("unknown topology"); } } public static void dfs(int s){ visited[s] = true; if(a[s].size()==1){ l++; } if(a[s].size()>2){ c = s; } for(int i=0;i<a[s].size();i++){ int y = a[s].get(i); if(!visited[y]){ dfs(y); } } } } class FastReader { BufferedReader bf; StringTokenizer st; public FastReader() { bf = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (Exception e) { System.out.println(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = bf.readLine(); } catch (Exception e) { System.out.println(e); } return str; } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
4e0b64f81e569b3c37eb78b35b2b8a04
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Main{ static class Node{ int e; ArrayList<Integer> arr; public Node(int entry){ e = entry; arr = new ArrayList<Integer>(); } public void add(int elem){ arr.add(elem); } public int getL(){ int temp = arr.size(); return temp; } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); ArrayList<Node> arrN = new ArrayList<Node>(); for(int i = 1; i <= n; i++){ arrN.add(new Node(i)); } for(int i = 0; i < m; i++){ int temp1 = sc.nextInt(); int temp2 = sc.nextInt(); arrN.get(temp1-1).add(temp2); arrN.get(temp2-1).add(temp1); } int[] gg = new int[n]; for(int i = 0; i < n; i++){ gg[i] = arrN.get(i).getL(); } Arrays.sort(gg); boolean run = true; if(run && gg[0] == 2 && gg[n-1] == 2){ System.out.println("ring topology"); run = false; } if(run && gg[n-2] == 1 && gg[n-1] == n-1){ System.out.println("star topology"); run = false; } if(gg[1] == 1 && gg[2] == 2 && gg[n-1] == 2){ System.out.println("bus topology"); run = false; } if(run) System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
2e9166dbb6394aba6f54c66219c6f7f2
train_002.jsonl
1366040100
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class CF292B{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] nodeMention = new int[n]; for(int i = 0; i < m; i++){ nodeMention[sc.nextInt()-1]++; nodeMention[sc.nextInt()-1]++; } Arrays.sort(nodeMention); if(nodeMention[0]==2 && nodeMention[n-1]==2) System.out.println("ring topology"); else if(nodeMention[n-2]==1 && nodeMention[n-1]==m) System.out.println("star topology"); else if(nodeMention[n-1]==2 && nodeMention[1]==1) System.out.println("bus topology"); else System.out.println("unknown topology"); } }
Java
["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"]
2 seconds
["bus topology", "ring topology", "star topology", "unknown topology"]
null
Java 8
standard input
[ "implementation", "graphs" ]
7bb088ce5e4e2101221c706ff87841e4
The first line contains two space-separated integers n and m (4 ≀ n ≀ 105;Β 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
1,200
In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
standard output
PASSED
3674f4f2407bf2b6af12a22bdc83e5ac
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static PrintWriter out=new PrintWriter(System.out);static FastScanner in = new FastScanner(System.in);static class FastScanner {BufferedReader br;StringTokenizer stok;FastScanner(InputStream is) {br = new BufferedReader(new InputStreamReader(is));} String next() throws IOException {while (stok == null || !stok.hasMoreTokens()) {String s = br.readLine();if (s == null) {return null;} stok = new StringTokenizer(s);}return stok.nextToken();} int ni() throws IOException {return Integer.parseInt(next());}long nl() throws IOException {return Long.parseLong(next());}double nd() throws IOException {return Double.parseDouble(next());}char nc() throws IOException {return (char) (br.read());}String ns() throws IOException {return br.readLine();} int[] nia(int n) throws IOException{int a[] = new int[n];for (int i = 0; i < n; i++)a[i] = ni();return a;}long[] nla(int n) throws IOException {long a[] = new long[n];for (int i = 0; i < n; i++)a[i] = nl();return a;} double[] nda(int n)throws IOException {double a[] = new double[n];for (int i = 0; i < n; i++)a[i] = nd();return a;}int [][] imat(int n,int m) throws IOException{int mat[][]=new int[n][m];for(int i=0;i<n;i++){for(int j=0;j<m;j++)mat[i][j]=ni();}return mat;} } static long mod=Long.MAX_VALUE; public static void main (String[] args) throws java.lang.Exception { int i,j; HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); /* if(hm.containsKey(z)) hm.put(z,hm.get(z)+1); else hm.put(z,1); */ HashSet<Integer> set=new HashSet<Integer>(); int t=in.ni(); while(t--!=0) { int n=in.ni(); long x=in.nl(),y=in.nl(); long z=y-x; ArrayList<Long> arr=new ArrayList<>(); for(i=1;i<=Math.sqrt(z);i++) { if(z%i!=0) continue; long d=z/(long)i; arr.add((long)i); if(i!=d) arr.add(d); } Collections.sort(arr); long d=-1; for(long c:arr) { long count=z/c+1; if(count>n) continue; d=c; break; } long low=x,high=y; ArrayList<Long> temp=new ArrayList<>(); while(low<=high) { temp.add(low); low+=d; } low=x;high=y; while(temp.size()!=n) { long p=low-d; if(p>0) temp.add(p); else break; low-=d; } while(temp.size()!=n) { high+=d; temp.add(high); } for(long c:temp) System.out.print(c+" "); System.out.println(); } out.close(); } static class pair implements Comparable<pair>{ int x, y; public pair(int x, int y){this.x = x; this.y = y;} @Override public int compareTo(pair arg0) { if(x<arg0.x) return -1; else if(x==arg0.x) { if(y<arg0.y) return -1; else if(y>arg0.y) return 1; else return 0; } else return 1; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof pair)) return false; pair key = (pair) o; return x == key.x && y == key.y;} public int hashCode() {int result = x;result = 31 * result + y;return result;} } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long exponent(long a,long n) { long ans=1; while(n!=0) { if(n%2==1) ans=(ans*a)%mod; a=(a*a)%mod; n=n>>1; } return ans; } static int binarySearch(int a[], int item, int low, int high) { if (high <= low) return (item > a[low])? (low + 1): low; int mid = (low + high)/2; if(item == a[mid]) return mid+1; if(item > a[mid]) return binarySearch(a, item, mid+1, high); return binarySearch(a, item, low, mid-1); } static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } while (i < n1){ arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void Sort(int arr[], int l, int r) {if (l < r) { int m = (l+r)/2; Sort(arr, l, m); Sort(arr , m+1, r); merge(arr, l, m, r); } } static void sort(int a[]) {Sort(a,0,a.length-1);} }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
be94d5c58f17ecaa1abb903df548888d
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.awt.*; public class Main { static long sx = 0, sy = 0, mod = (long) (1e9 + 7); static HashSet<Integer>[] a; static Long[][][] dp; static long[] fa; static long[] farr; public static PrintWriter out = new PrintWriter(System.out); static ArrayList<pair> pa = new ArrayList<>(); static StringBuilder sb = new StringBuilder(); static boolean cycle = false; static long m = 998244353; static long[] no, col; static String s; static long k = 0, n = 0, ans = 0; static long cnt; // static long[] dp; // static long[] p; // static long[] ans; // static Double[][] a = new Double[2][2]; static Double[][] b = new Double[2][2]; static Double[] x; static Double[] y; static long[] d; // static boolean b = true; static int[][] par; static int[] depth; static HashMap<Integer, Integer> map; // static ArrayList<Integer> p; static int time = 0; static int[] val; static int[] arr; static pair[] p; public static void main(String[] args) throws IOException { // Scanner scn = new Scanner(new BufferedReader(new // InputStreamReader(System.in))); Reader scn = new Reader(); int t = scn.nextInt(); while (t-- != 0) { int n = scn.nextInt(), x = scn.nextInt(), y = scn.nextInt(); int[] a = new int[n]; if (y <= n) { for (int i = 0; i < n; i++) a[i] = i + 1; } else { int diff = 0; for (int i = 1; i < 51; i++) { if ((y - x) % i == 0 && (y - x) / i < n) { diff = i; break; } } int i = 0; int val = x; while (i < a.length && val <= y) { a[i++] = val; val += diff; } val = x - diff; while (i < a.length && val > 0) { a[i++] = val; val -= diff; } val = y + diff; while (i < a.length) { a[i++] = val; val += diff; } } for (int i : a) System.out.print(i + " "); System.out.println(); } } // _________________________TEMPLATE_____________________________________________________________ // public static long lcm(long x, long y) { // // return (x * y) / gcd(x, y); // } // // private static long gcd(long x, long y) { // if (x == 0) // return y; // // return gcd(y % x, x); // } // // static class comp implements Comparator<Integer> { // // @Override // public int compare(Integer p1, Integer p2) { // // return p2 - p1; // // } // } // // } // // public static long pow(long a, long b) { // // if (b < 0) // return 0; // if (b == 0 || b == 1) // return (long) Math.pow(a, b); // // if (b % 2 == 0) { // // long ret = pow(a, b / 2); // ret = (ret % mod * ret % mod) % mod; // return ret; // } // // else { // return ((pow(a, b - 1) % mod) * a % mod) % mod; // } // } private static class pair implements Comparable<pair> { int a, b; pair(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(pair o) { if (this.a < o.a) return -1; else if (this.a > o.a) return 1; else return 0; } // @Override // public int hashCode() { // return val * 17 + cnt * 31; // } // // @Override // // public boolean equals(Object o) { // // pair p = (pair) o; // return this.val == p.val && this.cnt == p.cnt; // } } private static String reverse(String s) { return new StringBuilder(s).reverse().toString(); } public static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[1000000 + 1]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } public long[][] nextInt2DArrayL(int m, int n) throws IOException { long[][] arr = new long[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } // kickstart - Solution // atcoder - Main } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
7685e17e9a7193e9d47aceb8cb29f55f
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class ArrRest { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); for (int iter = 0; iter < t; iter++) { String[] input = br.readLine().trim().split(" "); int n = Integer.parseInt(input[0]); int x = Integer.parseInt(input[1]); int y = Integer.parseInt(input[2]); int dist = y - x; int step = -1; for (int i = 1; i <= dist; i++) { if (dist % i == 0 && dist / i < n) { step = i; break; } } int min = x % step; if (min == 0) { min += step; } min = Math.max(min, y - step * (n - 1)); for (int i = 0; i < n - 1; i++) { bw.write((min + i * step) + " "); } bw.write(min + ((n - 1) * step) + "\n"); } br.close(); bw.close(); } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
4c5011c1488cbfd8e105aef97b78f3f3
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class B { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0) { int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int diff1 = x; int diff2 = y-x; int init = x; int f = n-1; int a = 0; int count = 2; if(diff2%f!=0 && diff2!=1) { while(f!=0 && diff2%f!=0) { f--; } a = diff2/f; count = count + (diff2/a)-1; while(init-a>0 && count!=n) { init = init - a; count++; } } else if(diff2==1 && diff2%f!=0) { if(n-2<diff1) { init = x - n + 2; a =1; } else { init = 1; a=1; } } else { a = diff2/f; init = x; } for(int i=0;i<n;i++) { sb.append(init+" "); init = init+a; } sb.append("\n"); } System.out.print(sb.toString()); } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
4e6c7c3fcc99b4c77d28bfc9b9299247
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class C { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for(int tt= 0 ; tt < t ;tt++) { int n = sc.nextInt() , x = sc.nextInt() , y = sc.nextInt(); //generating all possible answers int ans[] = null; int max = Integer.MAX_VALUE; for(int i = 1 ; i <= 50 ; i++) { for(int j = 1 ; j < 50 ; j++) { int a[] = get(n , i , j); boolean hasx = false , hasy = false; for(int item : a) { if(item == x)hasx = true; if(item == y)hasy = true; } if(hasx && hasy) { if(a[n-1] < max) { ans = a; max = ans[n-1]; } } } } for(int i : ans) { System.out.print(i + " "); }System.out.println(); } } static int[] get(int n , int a , int b) { int arr[] = new int[n]; arr[0] = a; for(int i = 1; i < n ;i++) { arr[i] = arr[i-1] + b; } return arr; } static boolean valid(int i , int j) { return i>= 0 && i <j ; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
4dca35e7c4478c15eae0b3edccca4d3a
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { static final int INF = (int) (1e9 + 10); static final int MOD = (int) (1e9 + 7); // static final int N = (int) (4e5 + 5); // static ArrayList<Integer>[] graph; // static boolean visited[]; // static long size[]; public static void main(String[] args) throws NumberFormatException, IOException { FastReader sc = new FastReader(); PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int df = y-x; n = n-1; int d=0; for(int i = n ; i >=1 ;i--){ if(df%i == 0){ d =i; break; } } d = df/d; // pr.println(d + "asdfg"); ArrayList<Integer>list = new ArrayList<>(); n--; list.add(x); list.add(y); int in = x; while(n > 0 && in + d != y ){ list.add(in+d); in = in+d; n--; } in = x; while(n > 0 && in-d > 0){ list.add(in-d); in = in-d; n--; } in = y; while(n > 0){ list.add(in+d); in = in+d; n--; } for(int num : list){ pr.print(num + " "); } pr.println(); } pr.flush(); pr.close(); } public static boolean pos(long mid, long s){ long num = 0; while(mid > 0){ num += mid%10; mid = mid/10; } if(num <= s) return true; return false; } /* * Template From Here */ private static boolean possible(long[] arr, int[] f, long mid, long k) { long c = 0; for (int i = 0; i < arr.length; i++) { if ((arr[i] * f[f.length - i - 1]) > mid) { c += (arr[i] - (mid / f[f.length - i - 1])); } } // System.out.println(mid+" "+c); if (c <= k) return true; return false; } public static int lowerBound(ArrayList<Integer> array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value <= array.get(mid)) { high = mid; } else { low = mid + 1; } } return low; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static void sortbyColumn(int[][] att, int col) { // Using built-in sort function Arrays.sort Arrays.sort(att, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator // if (entry1[col] > entry2[col]) // return 1; // else //(entry1[col] >= entry2[col]) // return -1; return new Integer(entry1[col]).compareTo(entry2[col]); // return entry1[col]. } }); // End of function call sort(). } static class pair { int V, I; pair(int v, int i) { V = v; I = i; } } public static int[] swap(int data[], int left, int right) { int temp = data[left]; data[left] = data[right]; data[right] = temp; return data; } public static int[] reverse(int data[], int left, int right) { while (left < right) { int temp = data[left]; data[left++] = data[right]; data[right--] = temp; } return data; } public static boolean findNextPermutation(int data[]) { if (data.length <= 1) return false; int last = data.length - 2; while (last >= 0) { if (data[last] < data[last + 1]) { break; } last--; } if (last < 0) return false; int nextGreater = data.length - 1; for (int i = data.length - 1; i > last; i--) { if (data[i] > data[last]) { nextGreater = i; break; } } data = swap(data, nextGreater, last); data = reverse(data, last + 1, data.length - 1); return true; } static long nCr(long n, long r) { if (n == r) return 1; return fact(n) / (fact(r) * fact(n - r)); } static long fact(long n) { long res = 1; for (long i = 2; i <= n; i++) res = res * i; return res; } /* * static boolean prime[] = new boolean[1000007]; * * public static void sieveOfEratosthenes(int n) { * * for (int i = 0; i < n; i++) prime[i] = true; * for (int p = 2; p * p <= * n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] * == true) { // Update all multiples of p for (int i = p * p; i <= n; i += * p) prime[i] = false; } } * * // Print all prime numbers // for(int i = 2; i <= n; i++) // { // * if(prime[i] == true) // System.out.print(i + " "); // } } */ static long power(long fac2, int y, int p) { long res = 1; fac2 = fac2 % p; while (y > 0) { if (y % 2 == 1) res = (res * fac2) % p; fac2 = (fac2 * fac2) % p; } return res; } static long modInverse(long fac2, int p) { return power(fac2, p - 2, p); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static BigInteger lcmm(String a, String b) { BigInteger s = new BigInteger(a); BigInteger s1 = new BigInteger(b); BigInteger mul = s.multiply(s1); BigInteger gcd = s.gcd(s1); BigInteger lcm = mul.divide(gcd); return lcm; } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
4605c414428b261be00e2834a3ee60ab
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); for(int tt=0;tt<t;tt++) { int n = fs.nextInt();int x = fs.nextInt();int y = fs.nextInt(); int good = Integer.MAX_VALUE; int[] arr=new int[n]; a: for(int i=1;i<=50;i++) { for(int d= 1;d<=50;d++) { int [] a=gen(n,i,d); boolean aIS=false;boolean bIS=false; for(int l:a) { if(l==x)aIS=true; if(l==y)bIS=true; } if(aIS==true&&bIS==true&&a[n-1]<good) { arr=a; good=arr[n-1]; } } } for(int m:arr) { System.out.print(m+" "); } System.out.println(); } } static int[] gen(int n, int i, int d) { int [] a= new int[n]; a[0]=i; for(int m=1;m<n;m++) { a[m]=a[m-1]+d; } return a; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
68f8fc7386778e92db7ad7cbfc1f86d3
train_002.jsonl
1599230100
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$. If you sort the array in increasing order (such that $$$a_1 &lt; a_2 &lt; \ldots &lt; a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class c667 { public static void main (String[] args){ Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T --> 0){ int N = sc.nextInt(); int X = sc.nextInt(); int Y = sc.nextInt(); for (int i = 1; i <= Y-X; i++){ boolean yes = false; for (int j = 1; j <= X; j++){ int[] arr = new int[N]; arr[0] = j; for (int a = 1; a < N; a++){ arr[a] = arr[a-1] + i; } boolean xwork = false; boolean ywork = false; for (int a = 0; a < N; a++){ if (arr[a] == X) xwork=true; if (arr[a] == Y) ywork=true; } if (xwork && ywork){ yes = true; for (int a = 0; a < N; a++){ System.out.print(arr[a] + " "); } break; } } if (yes) break; } System.out.println(); } } }
Java
["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"]
1 second
["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"]
null
Java 11
standard input
[ "number theory", "brute force", "math" ]
ca9d97e731e86cf8223520f39ef5d945
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x &lt; y \le 50$$$) β€” the length of the array and two elements that are present in the array, respectively.
1,200
For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.
standard output
PASSED
5d49bbc58c3a953b68dc8c511f5bfe41
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class E279 { static class Unknown { private char[] digits; private int finalvalue; private int maxvalue; public Unknown(String in) { this.digits = in.toCharArray(); char[] max = new char[digits.length]; boolean free = false; for (int x = 0; x < digits.length; x++) { if (digits[x] == '?') { max[x] = '9'; free = true; } else { max[x] = digits[x]; } } maxvalue = Integer.parseInt(new String(max)); if (free) { finalvalue = -1; } else { finalvalue = maxvalue; } } public boolean setValue(int min) { if (min >= maxvalue) { return false; } if (finalvalue != -1) { return true; } char[] target = String.valueOf(min).toCharArray(); if (target.length < digits.length) { for (int x = 0; x < digits.length; x++) { if (x == 0 && digits[x] == '?') { digits[x] = '1'; } else if (digits[x] == '?') { digits[x] = '0'; } } finalvalue = Integer.parseInt(new String(digits)); } else { for (int x = 0; x < digits.length; x++) { if (digits[x] != target[x]) { if (digits[x] != '?' && digits[x] > target[x]) { for (int y = x + 1; y < digits.length; y++) { if (digits[y] == '?') { digits[y] = '0'; } } finalvalue = Integer.parseInt(new String(digits)); return true; } else if (digits[x] == '?') { boolean free = search(digits, target, x + 1); if (!free) { digits[x] = (char) (target[x] + 1); for (int y = x + 1; y < digits.length; y++) { if (digits[y] == '?') { digits[y] = '0'; } } finalvalue = Integer.parseInt(new String(digits)); return true; } else { digits[x] = target[x]; } } } } } finalvalue = Integer.parseInt(new String(digits)); return true; } private boolean search(char[] digits, char[] target, int pos) { for (int x = pos; x < digits.length; x++) { if (digits[x] == '?') { if (target[x] == '9') { return search(digits, target, pos + 1); } else { return true; } } else if (digits[x] > target[x]) { return true; } else if (target[x] > digits[x]) { return false; } } return false; } public int getValue() { return finalvalue; } } public void solve() throws IOException { int n = nextInt(); Unknown[] values = new Unknown[n]; for (int x = 0; x < n; x++) { values[x] = new Unknown(nextLine()); } int min = 0; for (int x = 0; x < n; x++) { boolean possible = values[x].setValue(min); min = values[x].getValue(); if (!possible) { System.out.println("NO"); return; } } for (int x = 1; x < n; x++) { if (values[x].getValue() <= values[x - 1].getValue()) { System.out.println("NO"); return; } } System.out.println("YES"); for (int x = 0; x < n; x++) { System.out.println(values[x].getValue()); } } public BufferedReader br; public StringTokenizer st; public PrintWriter out; public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void run() throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; oj = true; br = new BufferedReader( new InputStreamReader(oj ? System.in : new FileInputStream("input.txt"))); out = new PrintWriter(oj ? System.out : new FileOutputStream("output.txt")); solve(); out.close(); } public static void main(String[] args) throws IOException { new E279().run(); } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
865cbc0de6aaa4a837cce318de131a2c
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
//package round279; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); char[][] s = new char[n][]; for(int i = 0;i < n;i++){ s[i] = ns().toCharArray(); } int x = 0; int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i] = guess(x, s[i]); if(a[i] == -1){ out.println("NO"); return; } x = a[i]; } out.println("YES"); for(int i = 0;i < n;i++){ out.println(a[i]); } } int guess(int x, char[] s) { String xs = Integer.toString(x); if(xs.length() > s.length)return -1; int n = s.length; char[] r = new char[n]; Arrays.fill(r, '0'); for(int i = xs.length()-1, j = n-1;i >= 0 && j >= 0;i--,j--){ r[j] = xs.charAt(i); } for(int i = 0;i < n;i++){ if(s[i] != '?'){ if(r[i] > s[i]){ return -1; }else if(r[i] < s[i]){ for(int j = i;j < n;j++){ if(s[j] == '?')s[j] = '0'; } return Integer.parseInt(new String(s)); } }else{ // assume same boolean hasgr = false; if(!(i == 0 && r[i] == '0')){ for(int j = i+1;j < n;j++){ int v = s[j] == '?' ? '9' : s[j]; if(r[j] < v){ hasgr = true; break; }else if(r[j] > v){ hasgr = false; break; } } } if(hasgr){ s[i] = r[i]; }else{ if(r[i] == '9'){ return -1; } s[i] = (char)(r[i]+1); for(int j = i+1;j < n;j++){ if(s[j] == '?')s[j] = '0'; } return Integer.parseInt(new String(s)); } } } return -1; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
3919d8e9060d85ed4ccfc1112e4728da
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class E { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()), count[] = new int[n], len[] = new int[n]; int[][] elements = new int[n][8]; for (int i = 0; i < n; i++) { String x = br.readLine(); len[i] = x.length(); for (int j = 0, k = 8 - len[i]; j < len[i]; j++, k++) { char e = x.charAt(j); if (e == '?') { elements[i][k] = -1; count[i]++; } else { elements[i][k] = e - '0'; } } } if (count[0] > 0) { for (int j = 7, k = 0; k < len[0]; j--, k++) { if (k == len[0] - 1 && elements[0][j] == -1) elements[0][j] = 1; if (elements[0][j] == -1) elements[0][j] = 0; } } int prev = 0; for (int j = 8 - len[0]; j < 8; j++) prev = prev * 10 + elements[0][j]; boolean flag = n == 1 ? true : false; for (int i = 1; i < n; i++) { flag = false; if (len[i - 1] > len[i]) break; if (len[i] > len[i - 1]) { for (int j = 7, k = 0; k < len[i]; j--, k++) { if (k == len[i] - 1 && elements[i][j] == -1) elements[i][j] = 1; else if (elements[i][j] == -1) elements[i][j] = 0; } } else if (count[i] > 0) { flag = false; boolean[] mayChange = new boolean[8]; int lastChange = 7; for (int j = 7, k = 0; k < len[i]; j--, k++) { if (elements[i][j] == -1) { mayChange[j] = true; if (flag) elements[i][j] = elements[i - 1][j]; else { elements[i][j] = Math.min(elements[i - 1][j] + 1, 9); flag = elements[i][j] > elements[i - 1][j]; lastChange = flag ? j : lastChange; } } else { if (flag) { flag = elements[i][j] >= elements[i - 1][j]; lastChange = elements[i][j] > elements[i - 1][j] ? j : lastChange; } else { flag = elements[i][j] > elements[i - 1][j]; lastChange = flag ? j : lastChange; } } } if (flag) { for (int j = lastChange + 1; j < 8; j++) if (mayChange[j]) elements[i][j] = 0; } } int curr = 0; for (int j = 8 - len[i]; j < 8; j++) curr = curr * 10 + elements[i][j]; flag = curr > prev; //System.out.println(prev + " " + curr + " " + (curr > prev)); if (!flag) break; prev = curr; } if (flag) { out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 8 - len[i]; j < 8; j++) out.print(elements[i][j]); out.println(); } } else out.println("NO"); out.close(); } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
a60f7d6b9dec18e39a18e9eb5ed82517
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author thnkndblv */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[ n ]; for (int i = 0; i < n; i++) { char[] num = in.next().toCharArray(); a[i] = f(i == 0 ? 0 : a[i-1], num); } for (int i = 1; i < n; i++) if (a[i-1] >= a[i]) { out.println("NO"); return; } out.println("YES"); for (int x : a) out.println(x); } private int f(int n, char[] a) { int cc = 0; for (int c : a) if (c == '?') cc++; if (cc == 0) return Integer.parseInt(new String(a)); int le = 0; int ri = 0; for (int i = 0; i < cc; i++) ri = ri*10 + 9; while (le < ri) { int mid = (le + ri) / 2; if (getNewNumber(a, mid, cc) > n) ri = mid; else le = mid + 1; } return getNewNumber(a, le, cc); } private int getNewNumber(char[] a, int le, int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[(n-1) - i] = le % 10; le /= 10; } if (a[0] == '?' && arr[0]==0) return -1; int ret = 0; for (int i = 0, j = 0; i < a.length; i++) { ret *= 10; if (a[i] == '?') ret += arr[j++]; else ret += a[i] - '0'; } return ret; } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
437e5803393cd4e6b4a70a6c2d2395a7
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ESolver { private static InputReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { InputStream is = System.in; PrintWriter pw = new PrintWriter(System.out); init(is, pw); solve(); is.close(); out.close(); } public static void init(InputStream is, PrintWriter pw) { in = new InputReader(is); out = pw; } public static void solve() { int n = in.ni(); StringBuilder sb = new StringBuilder(); String cur; String prev = replaceWithZeros(in.next()); sb.append(prev + '\n'); for (int i = 1; i < n; i++, prev = cur) { cur = in.next(); if (cur.length() < prev.length()) { out.println("NO"); return; } else if (cur.length() > prev.length()) { cur = replaceWithZeros(cur); sb.append(cur + '\n'); } else { cur = getChangedCur(cur, prev); if (cur == null) { out.println("NO"); return; } sb.append(cur + '\n'); } } out.println("YES" + '\n' + sb); } static String getChangedCur(String cur, String prev) { int min = Integer.MAX_VALUE; int prevN = Integer.parseInt(prev); for (int i = 0; i < cur.length(); i++) { int num = getNumber(cur, prev, i); if (num > prevN) { min = Math.min(min, num); } } return min < Integer.MAX_VALUE ? String.valueOf(min) : null; } static int getNumber(String cur, String prev, int p) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < p; i++) { char c = cur.charAt(i); if (cur.charAt(i) == '?') { sb.append("" + getDigit(prev, i)); } else { sb.append(c); } } char c = cur.charAt(p); if (cur.charAt(p) == '?') { sb.append(Math.min(9, getDigit(prev, p) + 1)); } else { sb.append(c); } for (int i = p + 1; i < cur.length(); i++) { c = cur.charAt(i); if (c == '?') { sb.append('0'); } else { sb.append(c); } } return Integer.parseInt(sb.toString()); } static int getDigit(String str, int p) { return Integer.parseInt("" + str.charAt(p)); } static String replaceWithZeros(String str) { if (str.charAt(0) == '?') { str = "1" + str.substring(1); } return str.replaceAll("\\?", "0"); } private static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } int[] na(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
0723930a35ab71ae7efc0197dd9ba8ec
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.*; import java.util.*; public class restincseq { private static BufferedReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out, true); int N = Integer.parseInt(in.readLine()); char[][] s = new char[N][]; for (int i = 0; i < N; i++) s[i] = in.readLine().toCharArray(); for (int i = 0; i < s[0].length; i++) if (s[0][i] == '?') s[0][i] = i == 0 ? '1' : '0'; boolean ok = true; for (int i = 1; ok && i < N; i++) { ok &= fix(s[i-1], s[i]); } if (ok) { out.println("YES"); for (int i = 0; i < N; i++) out.println(new String(s[i])); } else { out.println("NO"); } out.close(); System.exit(0); } public static boolean fix(char[] a, char[] b) { if (a.length > b.length) return false; if (b.length > a.length) { if (b[0] == '?') b[0] = '1'; for (int i = 1; i < b.length; i++) { if (b[i] == '?') b[i] = '0'; } return true; } if (!canPlace(a, b)) return false; for (int i = 0; i < b.length; i++) { if (b[i] == '?') { for (int j = i == 0 ? 1 : 0; j <= 9; j++) { b[i] = (char)(j + '0'); if (canPlace(a, b)) { break; } } } } return true; } public static boolean canPlace(char[] a, char[] b) { char[] c = new char[b.length]; System.arraycopy(b, 0, c, 0, b.length); for (int i = 0; i < c.length; i++) if (c[i] == '?') c[i] = '9'; for (int i = 0; i < a.length; i++) { if (a[i] < c[i]) return true; if (a[i] > c[i]) return false; } return false; } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
2686098b8e80bca9319219bc99f72aab
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.*; import java.util.*; public class Main { // main public static void main(String [] args) throws IOException { // makes the reader and writer BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // read in int n = Integer.parseInt(f.readLine()); int[] prev = new int[]{0,0,0,0,0,0,0,0}; boolean ok = true; StringBuffer sb = new StringBuffer(); for (int w=0;w<n;w++) { char[] nextC = f.readLine().toCharArray(); int[] next = new int[8]; for (int i=8-nextC.length;i<8;i++) next[i] = nextC[nextC.length+i-8]-48; boolean leading = true; outer: for (int i=0;i<9;i++) { if (i==8) { ok = false; break; } if (next[i]!=15 && next[i]!=0) leading = false; if (next[i]!=15 && next[i]>prev[i]) { for (int j=0;j<8;j++) if (next[j]==15) next[j] = 0; break; } if (next[i]!=15 && next[i]<prev[i]) ok = false; if (next[i]!=15 && next[i]==prev[i]) continue; if (next[i]==15 && prev[i]!=9) { boolean lesser = false; for (int j=i+1;j<8;j++) { if (next[j]<prev[j]) { next[i] = prev[i]+1; for (int k=0;k<8;k++) if (next[k]==15) next[k] = 0; break outer; } else if (next[j]>prev[j] && (next[j]!=15 || (next[j]==15 && prev[j]!=9)) && (leading && prev[i]==0)) { next[i] = 1; for (int k=0;k<8;k++) if (next[k]==15) next[k] = 0; break outer; } else if (next[j]>prev[j] && (next[j]!=15 || (next[j]==15 && prev[j]!=9))) { next[i] = prev[i]; leading = false; continue outer; } } next[i] = prev[i]+1; for (int k=0;k<8;k++) if (next[k]==15) next[k] = 0; break outer; } else { next[i] = 9; leading = false; } } boolean need = true; for (int i=0;i<8;i++) { if (need && next[i]==0) continue; need = false; sb.append(next[i]); } sb.append("\n"); prev = next; } // print if (ok) { out.println("YES"); out.print(sb.toString()); } else out.println("NO"); // cleanup out.close(); System.exit(0); } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
fe6e35b581107ad16cfbfe0fd10b93b3
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.util.Scanner; public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); int[][] ans = new int[n][]; String[] v = new String[n]; int[][] vi = new int[n][]; for (int i = 0; i < v.length; i++) { v[i] = sc.nextLine(); vi[i] = new int[v[i].length()]; for (int j = 0; j < v[i].length(); j++) { vi[i][j] = -1; if (v[i].charAt(j) != '?') { vi[i][j] = v[i].charAt(j) - '0'; } } } ans[0] = getLowest(vi[0]); for (int i = 1; i < vi.length; i++) { ans[i] = solve(ans[i-1], vi[i]); if (ans[i] == null) { System.out.println("NO"); return; } } System.out.println("YES"); for (int i = 0; i < ans.length; i++) { System.out.println(convert(ans[i])); } } public static int[] getLowest(int[] v) { int[] ans = new int[v.length]; for (int i = 0; i < ans.length; i++) { if (v[i] == -1) { ans[i] = i == 0 ? 1 : 0; } else { ans[i] = v[i]; } } return ans; } public static int[] solve(int[] p, int[] n) { int[] ans = null; if (n.length < p.length) return null; if (n.length > p.length) { return getLowest(n); } int min = Integer.MAX_VALUE; for (int i = n.length - 1; i >= 0; i--) { int[] t = new int[n.length]; for (int j = 0; j < i; j++) { if (n[j] == -1) { t[j] = p[j]; } else { t[j] = n[j]; } } if (n[i] == -1) { if (p[i] == 9) continue; else t[i] = p[i] + 1; } else { t[i] = n[i]; } for (int j = i + 1; j < n.length; j++) { if (n[j] == -1) { t[j] = 0; } else { t[j] = n[j]; } } int current = convert(t); if (current > convert(p) && current < min) { min = current; ans = t; } } return ans; } static int convert(int[] v) { int ans = v[0]; for (int i = 1; i < v.length; i++) { ans *= 10; ans += v[i]; } return ans; } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
18e4e9dab1350d3d092c21bdbf2211b6
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.util.*; import java.io.*; public class Main { int MAXN=100*1000+100; int []numbers = new int[MAXN]; public String getStringNumber(String str, int number) { char []prev=Integer.toString(number).toCharArray(); char []cur=str.toCharArray(); if (prev.length>cur.length) { //ΠΊΠΎΠ»-Π²ΠΎ разрядов ΠΏΡ€Π΅Π΄ΠΈΠ΄ΡƒΡ‰Π΅Π³ΠΎ числа большС Π½Ρ‹Π½Π΅ΡˆΠ½Π΅Π³ΠΎ => Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ большСС число return null; } if (prev.length<cur.length) {//ΠΊΠΎΠ»-Π²ΠΎ разрядов Π½ΠΎΠ²ΠΎΠ³ΠΎ числа большС Π½Ρ‹Π½Π΅ΡˆΠ½Π΅Π³ΠΎ => ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ Π½ΠΎΠ²ΠΎΠ΅ число, Ρ‚.ΠΊ. ΠΎΠ½ΠΎ всС Ρ€Π°Π²Π½ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ большС for (int i=0; i<cur.length; i++) { if (cur[i]=='?') { if (i==0) { cur[i]='1'; } else { cur[i]='0'; } } } return getString(cur); } int isMore=-1; boolean ok=false; for (int i=0; i<cur.length; i++) { if (cur[i]!='?') { if (cur[i]>prev[i]) { for (int j=0; j<i; j++) { cur[j]=prev[j]; } for (int j=i+1; j<cur.length; j++) { if (cur[j]=='?') { cur[j]='0'; } } return getString(cur); } else if (cur[i]<prev[i]) { isMore=i; for (int j=i+1; j<cur.length; j++) { if (cur[j]=='?') { cur[j]='0'; } } break; } } } for (int i=(isMore==-1? cur.length-1: isMore); i>=0; i--) { if (cur[i]=='?') { if (prev[i]<'9') { cur[i]=(char)(prev[i]+1); for (int j=i-1; j>=0; j--) { cur[j]=prev[j]; } for (int j=i+1; j<cur.length; j++) { if (cur[j]=='?') { cur[j]='0'; } } isMore=-1; ok=true; break; } } } if (isMore>=0 || !ok) { return null; } return getString(cur); } public String getString(char[]mas) { String str=""; for (int i=0; i<mas.length; i++) { str+=mas[i]; } return str; } public void solve() throws IOException { int n=nextInt(); int curNumber=0; for (int i=0; i<n; i++) { String number=nextToken(); if (number.indexOf('?')>-1) { number=getStringNumber(number, curNumber); } int a; if (number==null || (a=Integer.parseInt(number))<=curNumber) { out.println("NO"); return; } numbers[i]=a; curNumber=a; } out.println("YES"); for (int i=0; i<n; i++) { out.println(numbers[i]); } } public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); // стандартный Π²Π²ΠΎΠ΄ out = new PrintWriter(System.out); // стандартный Π²Ρ‹Π²ΠΎΠ΄ solve(); in.close(); out.close(); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
caa6e433cf76a354eb012a6a68eaed8a
train_002.jsonl
1416733800
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
256 megabytes
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Iterator; import java.util.LinkedList; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.Stack; import java.util.TreeSet; public class Main { public String getMinStr(String s) { StringBuilder str = new StringBuilder(); for(int i = 0;i < s.length();++i) { if('?' == s.charAt(i)) { str.append(0 == i ? '1' : '0'); } else { str.append(s.charAt(i)); } } return str.toString(); } public String getMinStr(String s1, String s2) { int n = s1.length(); int index = n; for(int i = 0;i < n;++i) { if(s1.charAt(i) != '?' && s1.charAt(i) != s2.charAt(i)) { index = i; break; } } char[] a = s1.toCharArray(); if(index != n && s1.charAt(index) > s2.charAt(index)) { for(int i = 0;i < index;++i) { a[i] = s2.charAt(i); } for(int i = index + 1;i < n;++i) { if('?' == a[i]) { a[i] = '0'; } } } else { boolean isBigger = false; for(int i = index - 1;i >= 0;--i) { if('?' == a[i]) { if(!isBigger && s2.charAt(i) != '9') { a[i] = (char)(s2.charAt(i) + 1); isBigger = true; index = i; break; } } } if(!isBigger) { return null; } for(int i = 0;i < index;++i) { a[i] = s2.charAt(i); } for(int i = index + 1;i < n;++i) { if('?' == a[i]) { a[i] = '0'; } } } return new String(a); } public void foo() { MyScanner scan = new MyScanner(); int n = scan.nextInt(); String[] ss = new String[n + 1]; boolean isNo = false; ss[0] = "0"; for(int i = 1;i <= n;++i) { String s = scan.next(); if(isNo) { continue; } if(s.length() < ss[i - 1].length()) { isNo = true; } else if(s.length() > ss[i - 1].length()) { ss[i] = getMinStr(s); } else { ss[i] = getMinStr(s, ss[i - 1]); if(null == ss[i]) { isNo = true; } } } PrintWriter out = new PrintWriter(System.out); if(isNo) { out.println("NO"); } else { out.println("YES"); for(int i = 1;i <= n;++i) { out.println(ss[i]); } } out.close(); } public static void main(String[] args) { new Main().foo(); } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
Java
["3\n?\n18\n1?", "2\n??\n?", "5\n12224\n12??5\n12226\n?0000\n?00000"]
1 second
["YES\n1\n18\n19", "NO", "YES\n12224\n12225\n12226\n20000\n100000"]
null
Java 7
standard input
[ "binary search", "implementation", "greedy", "brute force" ]
7e0ba313c557cb6b61ba57559e73a2bb
The first line of the input contains integer n (1 ≀ n ≀ 105) β€” the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
2,000
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
standard output
PASSED
f8e7da65def2ed9ebce53b9c58de593f
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class PashmakAndGraph { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Edge implements Comparable<Edge> { int v1; int v2; int weight; public Edge(int v1, int v2, int w) { this.v1 = v1; this.v2 = v2; this.weight = w; } @Override public int compareTo(Edge arg0) { int a = weight - arg0.weight; if(a == 0) a = v2 - arg0.v2; if(a == 0) a = v1 - arg0.v1; return a; } } static int n, m; static Edge[] edge; public static void main(String[] args) { FastReader sc = new FastReader(); n = sc.nextInt(); m = sc.nextInt(); edge = new Edge[m]; for(int i = 0; i < m; i++) { int v1 = sc.nextInt()-1; int v2 = sc.nextInt()-1; int w = sc.nextInt(); edge[i] = new Edge(v1,v2,w); } Arrays.sort(edge); int[] score = new int[n]; int[] secondscore = new int[n]; int[] lastweight = new int[n]; // int[] curpar = new int[n]; int r = 0; int b = -1; for(int i = 0; i < m; i++) { Edge e = edge[i]; if(lastweight[e.v2] > e.weight) { System.out.println("ERROR!"); } if(lastweight[e.v1] < e.weight) { if(score[e.v2] < score[e.v1] + 1) { if(lastweight[e.v2] < e.weight)secondscore[e.v2] = score[e.v2]; score[e.v2] = score[e.v1]+1; lastweight[e.v2] = e.weight; if(score[e.v2] > r) { r = score[e.v2]; b = e.v2; } // r = Math.max(r, score[e.v2]); } } else { if(score[e.v2] < 1) { score[e.v2] = 1; lastweight[e.v2] = e.weight; if(score[e.v2] > r) { r = score[e.v2]; b = e.v2; } // r = Math.max(r, score[e.v2]); } } if(lastweight[e.v1] == e.weight) { if(score[e.v2] < secondscore[e.v1] + 1) { score[e.v2] = secondscore[e.v1]+1; lastweight[e.v2] = e.weight; if(score[e.v2] > r) { r = score[e.v2]; b = e.v2; } } } } System.out.println(r); // System.out.println(Arrays.toString(score)); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 8
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
719f9bb5781c4ddd6bdff144ab9475e0
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class PashmakAndGraph { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Edge implements Comparable<Edge> { int v1; int v2; int weight; public Edge(int v1, int v2, int w) { this.v1 = v1; this.v2 = v2; this.weight = w; } @Override public int compareTo(Edge arg0) { int a = weight - arg0.weight; if(a == 0) a = v2 - arg0.v2; if(a == 0) a = v1 - arg0.v1; return a; } } static int n, m; static Edge[] edge; public static void main(String[] args) { FastReader sc = new FastReader(); n = sc.nextInt(); m = sc.nextInt(); edge = new Edge[m]; for(int i = 0; i < m; i++) { int v1 = sc.nextInt()-1; int v2 = sc.nextInt()-1; int w = sc.nextInt(); edge[i] = new Edge(v1,v2,w); } Arrays.sort(edge); int[] score = new int[n]; int[] temp = new int[n]; int r = 0; int b = -1; for(int i = 0; i < m; i++) { int j = i+1; while(j < m && edge[j].weight == edge[i].weight) { j++; } for(int k = i; k < j; k++) { Edge e = edge[k]; if(temp[e.v2] < score[e.v1] + 1) { temp[e.v2] = score[e.v1]+1; } } for(int k = i; k < j; k++) { Edge e = edge[k]; score[e.v2] = temp[e.v2]; r = Math.max(r, score[e.v2]); } i=j-1; } System.out.println(r); // System.out.println(Arrays.toString(score)); // System.out.println(Arrays.toString(temp)); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 8
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
1cdfbc9e6f1d5ba8334a93e904afeec7
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.lang.*; import java.math.*; import java.util.*; import java.io.*; public class Main { class Node implements Comparable<Node>{ int u; int v; int w; public Node(int u,int v,int w){ this.u=u; this.v=v; this.w=w; } public int compareTo(Node c){ int t=Integer.compare(c.w,this.w); return t; } } void solve() { int n=ni(),m=ni(); Node p[]=new Node[m]; for(int i=0;i<m;i++){ int u=ni(),v=ni(),w=ni(); p[i]=new Node(u,v,w); } Arrays.sort(p); int dp[]=new int[n+1]; Queue<Node> q=new LinkedList<>(); for(int i=0;i<m;i++){ int j=i; while(j<m && p[j].w==p[i].w){ if(1+dp[p[j].v]>dp[p[j].u]) q.offer(new Node(p[j].u,-1,1+dp[p[j].v])); j++; } while(!q.isEmpty()){ Node tmp=q.poll(); dp[tmp.u]=Math.max(dp[tmp.u],tmp.w); } i=j-1; } int ans=0; for(int i=1;i<=n;i++) ans=Math.max(ans,dp[i]); pw.println(ans); } long M= (long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 8
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
9d6c0d5ffb0610794fbdce137102a59a
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class PashmakAndGraph { private static InputReader in; private static PrintWriter out; public static void main(String[] args) { new PashmakAndGraph().run(); } private static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(nextString()); } public char nextChar() { return (char) skip(); } public String nextString() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextString(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void debug(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } /*==================================================================================================================*/ static class Edge { int u, v; Edge(int u, int v) { this.u = u; this.v = v; } } final int INF = (int) 1e9; final int N = (int) 3e5 + 5; final int M = (int) 3e5 + 5; final int W = (int) 1e5 + 5; int n, m; ArrayList<Edge>[] edges = new ArrayList[W + 1]; int[] dp = new int[N]; int[] newDp = new int[N]; void solve() { n = in.nextInt(); m = in.nextInt(); Arrays.setAll(edges, i -> new ArrayList<>()); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); int w = in.nextInt(); u--; v--; edges[w].add(new Edge(u, v)); } for (int w = 1; w <= W; w++) { for (Edge e : edges[w]) { newDp[e.v] = max(newDp[e.v], dp[e.u] + 1); } for (Edge e : edges[w]) { dp[e.v] = max(dp[e.v], newDp[e.v]); } } int ans = -1; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } out.println(ans); } public void run() { in = new InputReader(System.in); out = new PrintWriter(System.out); int tt = 1; // int tt = in.nextInt(); for (int i = 1; i <= tt; i++) { solve(); } out.flush(); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 8
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
9616a0cf495253ed57cadfb0836a328c
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class PashmakAndGraph { private static InputReader in; private static PrintWriter out; public static void main(String[] args) { new PashmakAndGraph().run(); } private static class InputReader { private final InputStream is; private final byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { is = stream; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(nextString()); } public char nextChar() { return (char) skip(); } public String nextString() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextString(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void debug(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } /*==================================================================================================================*/ static class Edge { int u, v; Edge(int u, int v) { this.u = u; this.v = v; } } final int W = (int) 1e5 + 5; int n, m; ArrayList<Edge>[] edges; int[] dp; int[] newDp; void solve() { n = in.nextInt(); m = in.nextInt(); edges = new ArrayList[W + 1]; Arrays.setAll(edges, i -> new ArrayList<>()); for (int i = 0; i < m; i++) { int u = in.nextInt(); int v = in.nextInt(); int w = in.nextInt(); u--; v--; edges[w].add(new Edge(u, v)); } dp = new int[n]; newDp = new int[n]; for (int w = 1; w <= W; w++) { for (Edge e : edges[w]) { newDp[e.v] = max(newDp[e.v], dp[e.u] + 1); } for (Edge e : edges[w]) { dp[e.v] = max(dp[e.v], newDp[e.v]); } } int ans = -1; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } out.println(ans); } public void run() { in = new InputReader(System.in); out = new PrintWriter(System.out); int tt = 1; // int tt = in.nextInt(); for (int i = 1; i <= tt; i++) { solve(); } out.flush(); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 8
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
984c52a1ee31ef29e6212027cc093194
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import static java.lang.Math.min; import static java.lang.Math.max; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int qq = 1; //int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); int m = readInt(); Edge[] list = new Edge[m]; for(int i = 0; i < m; i++) { list[i] = new Edge(readInt()-1, readInt()-1, readInt()); } Arrays.sort(list); int[] dp = new int[n]; int[] next = new int[n]; int last = -1; ArrayList<Integer> updated = new ArrayList<Integer>(); for(Edge out: list) { if(out.w != last) { for(int out2: updated) { dp[out2] = next[out2]; } updated = new ArrayList<Integer>(); } next[out.b] = Math.max(next[out.b], dp[out.a]+1); updated.add(out.b); last = out.w; } int ret = 0; for(int out: next) { ret = Math.max(ret, out); } pw.println(ret); } pw.close(); } static class Edge implements Comparable<Edge> { public int a,b,w; public Edge(int x, int y, int z) { a=x; b=y; w=z; } public int compareTo(Edge e) { return w - e.w; } } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } return st.nextToken(); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 6
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
f821cdf21c45375bc7fa72e66840f268
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.awt.image.ImageConsumer; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class Solver { public static void main(String[] Args) throws NumberFormatException, IOException { new Solver().Run(); } PrintWriter pw; StringTokenizer Stok; BufferedReader br; public String nextToken() throws IOException { while (Stok == null || !Stok.hasMoreTokens()) { Stok = new StringTokenizer(br.readLine()); } return Stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } class TVertex { int num; int kolCur = 0; int kolPrev = 0; int curValue = Integer.MAX_VALUE; } class TEdge implements Comparable<TEdge>{ TVertex v1, v2; int weight; @Override public int compareTo(TEdge arg0) { return this.weight-arg0.weight; } } TVertex[] verts; TEdge[] edges; int kolVerts, kolEdges; public void Run() throws NumberFormatException, IOException { //br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); kolVerts = nextInt(); kolEdges = nextInt(); verts = new TVertex[kolVerts]; edges = new TEdge[kolEdges]; for (int i=0; i<kolVerts; i++){ verts[i]=new TVertex(); } for (int i=0; i<kolEdges; i++){ int ind1=nextInt()-1; int ind2=nextInt()-1; int weight=nextInt(); edges[i]=new TEdge(); edges[i].v1=verts[ind1]; edges[i].v2=verts[ind2]; edges[i].weight=weight; } Arrays.sort(edges); int result = 0; for (int i=kolEdges-1; i>=0; i--){ int curRes=edges[i].v2.kolPrev; if (edges[i].v2.curValue>edges[i].weight) curRes=edges[i].v2.kolCur; curRes++; if (edges[i].weight==edges[i].v1.curValue) edges[i].v1.kolCur=Math.max(edges[i].v1.kolCur, curRes); else { if (edges[i].v1.kolCur<curRes){ edges[i].v1.kolPrev=edges[i].v1.kolCur; edges[i].v1.kolCur=curRes; edges[i].v1.curValue=edges[i].weight; } } result=Math.max(result, edges[i].v1.kolPrev); result=Math.max(result, edges[i].v1.kolCur); } pw.println(result); pw.flush(); pw.close(); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 6
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
576497f822f4c704509f759933494a76
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
//package current; import java.io.*; import java.util.Scanner; import java.util.Arrays; import java.util.StringTokenizer; import java.util.Vector; /** * Built using CHelper plug-in * Actual solution is at the top * @author zxybazh */ public class Main { public static void main(String[] args) { OutputStream outputStream = System.out; FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } class Triple implements Comparable<Triple>{ int x, y, w; public Triple() { } public Triple(int xx, int yy, int zz) { x = xx; y = yy; w = zz; } @Override public int compareTo(Triple a) { if (w < a.w) return -1; if (w > a.w) return 1; return 0; } boolean equals(Triple obj) { return x == obj.x && y == obj.y && w == obj.w; } } class TaskE { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); Triple e[] = new Triple[1001010]; int f[] = new int[1001010]; int g[] = new int[1001010]; for (int i = 1; i <= n; i++) { f[i] = 0; g[i] = -1; } for (int i = 0; i < m; i++) { e[i] = new Triple(); e[i].x = in.nextInt(); e[i].y = in.nextInt(); e[i].w = in.nextInt(); } //out.println(e[0].compareTo(e[1])); Arrays.sort(e, 0, m); int ans = 0; Vector<Triple> a = new Vector(); int j = 0; for (int i = 0; i < m;) { a.clear(); for (j = i; j < m && e[j].w == e[i].w; j++) { if (g[e[j].x] < e[i].w && f[e[j].y] < f[e[j].x]+1) { a.add(new Triple(e[j].y, f[e[j].x] + 1, e[j].w)); } } for (int tj = 0; tj < a.size(); tj++) { if (a.get(tj).y > f[a.get(tj).x]) { f[a.get(tj).x] = a.get(tj).y; g[a.get(tj).x] = a.get(tj).w; if (a.get(tj).y > ans) ans = a.get(tj).y; } } i = j; } out.println(ans); } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 6
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
62bbc0fc0a4cf91a357df7de6868dfdb
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.util.Arrays; import java.util.ArrayList; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.io.BufferedReader; import java.math.BigInteger; import java.io.PrintStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Collections; import java.util.Comparator; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int m = in.nextInt(); V[] vs = new V[n]; for (int i = 0; i < vs.length; i++) { vs[i] = new V(); } for (int i = 0; i < m; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; int w = in.nextInt(); vs[u].add(new E(vs[v], w)); } for (V v : vs) Collections.sort(v); for (V v : vs) v.iter = v.size(); int ans = 0; for (V v : vs) { ans = Math.max(ans, dfs(v, 0)); } out.println(ans); } private int dfs(V v, int w) { int upper = Algo.upperBound(v, new E(null, w)); if (upper == v.size()) return 0; if (v.get(upper).step != -1) return v.get(upper).step; for (int i = v.iter - 1; i >= upper; i--) { v.get(i).step = 1 + dfs(v.get(i).to, v.get(i).cost); if (i + 1 < v.size()) v.get(i).step = Math.max(v.get(i).step, v.get(i + 1).step); v.iter = i; } return v.get(upper).step; } class V extends ArrayList<E> { int iter; } class E implements Comparable<E> { V to; int cost; int step = -1; E(V to, int cost) { this.to = to; this.cost = cost; } public int compareTo(E o) { return cost - o.cost; } } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } class Algo { public static <T extends Comparable<T>> int upperBound(List<T> tl, T v) { return upperBound(tl, 0, tl.size(), v); } public static<T extends Comparable<T>> int upperBound(List<T> tl, int l, int r, T v) { while (l < r) { int m = (l + r) >> 1; if (tl.get(m).compareTo(v) > 0) r = m; else l = m + 1; } return l; } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 6
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
e19f3ffb9986ab758ba4676d225dc493
train_002.jsonl
1408116600
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.Help Pashmak, print the number of edges in the required path.
256 megabytes
import java.util.Arrays; import java.util.ArrayList; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.io.BufferedReader; import java.math.BigInteger; import java.io.PrintStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Collections; import java.util.Comparator; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int m = in.nextInt(); V[] vs = new V[n]; for (int i = 0; i < vs.length; i++) { vs[i] = new V(); } for (int i = 0; i < m; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; int w = in.nextInt(); vs[u].add(new E(vs[v], w)); } for (V v : vs) Collections.sort(v); for (V v : vs) v.iter = v.size(); int ans = 0; for (V v : vs) { ans = Math.max(ans, dfs(v, 0)); } out.println(ans); } private int dfs(V v, int w) { int upper = Algo.upperBound(v, new E(null, w)); if (upper == v.size()) return 0; if (v.get(upper).step != -1) return v.get(upper).step; for (int i = v.iter - 1; i >= upper; i--) { v.get(i).step = 1 + dfs(v.get(i).to, v.get(i).cost); if (i + 1 < v.size()) v.get(i).step = Math.max(v.get(i).step, v.get(i + 1).step); } v.iter = upper; return v.get(upper).step; } class V extends ArrayList<E> { int iter; } class E implements Comparable<E> { V to; int cost; int step = -1; E(V to, int cost) { this.to = to; this.cost = cost; } public int compareTo(E o) { return cost - o.cost; } } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } class Algo { public static <T extends Comparable<T>> int upperBound(List<T> tl, T v) { return upperBound(tl, 0, tl.size(), v); } public static<T extends Comparable<T>> int upperBound(List<T> tl, int l, int r, T v) { while (l < r) { int m = (l + r) >> 1; if (tl.get(m).compareTo(v) > 0) r = m; else l = m + 1; } return l; } }
Java
["3 3\n1 2 1\n2 3 1\n3 1 1", "3 3\n1 2 1\n2 3 2\n3 1 3", "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4"]
1 second
["1", "3", "6"]
NoteIn the first sample the maximum trail can be any of this trails: .In the second sample the maximum trail is .In the third sample the maximum trail is .
Java 6
standard input
[ "dp", "sortings" ]
bac276604d67fa573b61075c1024865a
The first line contains two integers n, m (2 ≀ n ≀ 3Β·105;Β 1 ≀ m ≀ min(nΒ·(n - 1), 3Β·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≀ ui, vi ≀ n;Β 1 ≀ wi ≀ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges.
1,900
Print a single integer β€” the answer to the problem.
standard output
PASSED
93a4e44d6c1a99e678ff2b6f2a1ee46c
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.*; import java.util.*; public class Main { BufferedReader br; StringTokenizer in; PrintWriter out; public static void main(String[] args) { new Main().run(); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); run0(); out.close(); br.close(); } catch(IOException ex) { System.out.println(ex); System.exit(1); } } private void run0() throws IOException { int n = nextInt(); assert(n>=1 && n<=100); int m = nextInt(); assert(m>=0 && m<=n*(n-1)/2); byte[][] adj = new byte[n][n]; byte[] gilty = new byte[n]; for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u][v] = 1; adj[v][u] = 1; } int c = solve(adj, gilty); out.println(c); } private int solve(byte[][] adj, byte[] gilty) { int count = 0; while(solve0(adj, gilty)) count++; return count; } private boolean solve0(byte[][] adj, byte[] gilty) { for (int i = 0; i < gilty.length; i++) { int countKnuckles = 0; for (int j = 0; j < gilty.length; j++) { if (adj[i][j] > 0) { countKnuckles++; gilty[i] = (byte)j; } } if (countKnuckles != 1) { gilty[i] = -1; } } boolean removed = false; for (int i = 0; i < gilty.length; i++) { if (gilty[i] != -1) { adj[i][gilty[i]] = 0; adj[gilty[i]][i] = 0; gilty[i] = -1; removed = true; } } return removed; } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private String nextToken() throws IOException { while (in==null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
ccdc0ffe1778f9d400e887aa3e20ec3c
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.util.*; public class fence { // static {System.out.println("hello"); } public static void main(String args[]) { Scanner scn=new Scanner(System.in); int n=scn.nextInt(); ArrayList<Integer> ar[]=new ArrayList[n+1]; int deg[]=new int[n+1]; int lace=scn.nextInt(); for(int i=1;i<=lace;i++) { int x=scn.nextInt(); int y=scn.nextInt(); deg[x]++; deg[y]++; if (ar[x]==null ) ar[x]=new ArrayList<Integer>(); ar[x].add(y); if (ar[y]==null ) ar[y]=new ArrayList<Integer>();ar[y].add(x); } int cnt0=0; int cnt=0; int flag=0; while(true) { if (cnt0==n-1 ) break; flag=0; for(int i=1;i<=n;i++) { if (ar[i]!=null && deg[i]==1 && ar[i].size()==1) { int temp=ar[i].get(0); ar[temp].remove(new Integer(i)); deg[i]=0;// deg[temp]--; ar[i].remove(new Integer(temp)); ar[i]=null; flag=1; } } if (flag==0) break; cnt0=0; for(int i=1;i<=n;i++) { if (ar[i]==null ) {cnt0++; continue;} deg[i]=ar[i].size(); } cnt++; } System.out.println(cnt); // for(int i=1;i<=n;i++) System.out.println(ar[i]); } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
98967168835570d7b6c7be97c3670761
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedReader br; static PrintWriter out; static StringTokenizer st; static class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } } static void solve() throws Exception { int n = nextInt(); int m = nextInt(); int[][] g = new int[n][n]; for(int i = 0; i < m; i++){ int u = nextInt() - 1; int v = nextInt() - 1; g[u][v] = 1; g[v][u] = 1; } int cnt = 0; boolean canThrowAway = true; while(canThrowAway){ ArrayList<Pair> group = new ArrayList<Pair>(); for(int i = 0; i < n; i++){ int conn = 0; Pair condidate = null; for(int j = 0; j < n; j++){ if(i != j && g[i][j] == 1){ conn++; condidate = new Pair(i, j); } } if(conn == 1){ group.add(condidate); } } if(group.size() == 0){ canThrowAway = false; }else{ for(Pair p:group){ g[p.x][p.y] = 0; g[p.y][p.x] = 0; } cnt++; } } out.println(cnt); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public static void main(String[] args) { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new PrintStream(System.out)); solve(); out.close(); br.close(); } catch (Throwable t) { t.printStackTrace(); } } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
600dbb82fe2a75f00bf72d942ec0db52
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int n = nextInt(); int m = nextInt(); boolean[][] a = new boolean[n+1][n+1]; for (int k = 0; k < m; k++) { int i = nextInt(); int j = nextInt(); a[i][j] = true; a[j][i] = true; } for (int res = 0; true; res++) { int[] delete = new int[n+1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i][j]) { if (delete[i] == 0) { delete[i] = j; } else { delete[i] = 0; break; } } } } boolean stop = true; for (int i = 1; i <= n; i++) { if (delete[i] != 0) { a[i][delete[i]] = false; a[delete[i]][i] = false; stop = false; } } if (stop) { out.println(res); return; } } } static int sqr(int x) { return x*x; } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String nextLine() throws IOException { tok = new StringTokenizer(""); return in.readLine(); } static boolean hasNext() throws IOException { while (tok == null || !tok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return false; } tok = new StringTokenizer(s); } return true; } public static void main(String args[]) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); //in = new BufferedReader(new FileReader("1.in")); //out = new PrintWriter(new FileWriter("1.out")); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); java.lang.System.exit(1); } } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
fe6289fbf6a169034f1c925507992646
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package pkg129b; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Scanner; /** * * @author Admin */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int n, m, ans = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); Graf graf = new Graf(n + 1); for(int i = 0; i < m; i++){ int node1, node2; node1 = sc.nextInt(); node2 = sc.nextInt(); graf.addEdge(node1, node2); } boolean b = true; while (b) { b = false; ArrayList<Integer> v = new ArrayList<>(); for (int i = 0; i <= n; i++) { if (graf.getCountChild(i) == 1) { b = true; v.add(i); } } for (int i = 0; i < v.size(); i++) { graf.delEdge(v.get(i)); } if (b) { ans++; } } System.out.println(ans); } } class Graf{ private ArrayList< ArrayList<Integer> > v = new ArrayList<>(); private ArrayList <Integer> used = new ArrayList<>(); private ArrayDeque <Integer> Q = new ArrayDeque<>(); public Graf(int count){ for(int i = 0; i < count; i++){ v.add(new ArrayList<Integer>()); } used = new ArrayList<Integer>(); for(int i = 0; i < count; i++){ used.add(0); } } private void init(int k){ ArrayList<Integer> ls = v.get(k); for(int i = 0; i < ls.size(); i++){ if(used.get(ls.get(i)) == 0){ used.set(ls.get(i), used.get(k) + 1); Q.add(ls.get(i)); } } } int getCountChild(int k){ int ans = 0; return v.get(k).size(); } public void bsf(int start){ used.set(start, 1); init(start); while(!Q.isEmpty()){ init(Q.peek()); Q.poll(); } } public void addEdge(int first, int second){ v.get(first).add(second); v.get(second).add(first); } void delEdge(int k){ v.get(k).clear(); for(int i = 0; i < v.size(); i++){ ArrayList<Integer> ls = v.get(i); for(int j = 0; j < ls.size(); j++){ if(ls.get(j) == k){ ls.remove(j); break; } } } } ArrayList<Integer> getUsed(){ return used; } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
92cb882ac5e1eeb063b6f6694ebc752d
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.HashSet; import java.util.StringTokenizer; public class Main { private static BufferedReader in; private static BufferedWriter out; private static int n; private static int m; private static Node[] nodes; public static void main(String[] args) throws IOException { // stream boolean file = false; if (file) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer tok; // read input tok = new StringTokenizer(in.readLine()); n = Integer.parseInt(tok.nextToken()); m = Integer.parseInt(tok.nextToken()); nodes = new Node[n + 1]; for (int i = 0; i <= n; i++) nodes[i] = new Node(i); // read edges for (int i = 0; i < m; i++) { tok = new StringTokenizer(in.readLine()); int a = Integer.parseInt(tok.nextToken()); int b = Integer.parseInt(tok.nextToken()); nodes[a].next.add(b); nodes[b].next.add(a); } // simulate each round int ans = 0; boolean rem = true; while (rem) { // add all nodes with one neighbour HashSet<Integer> badNodes = new HashSet<>(); for (Node node : nodes) if (node.next.size() == 1) badNodes.add(node.id); // remove the edges of bad nodes for (Integer id : badNodes) { if (nodes[id].next.size() == 0) continue; int otherNode = nodes[id].next.iterator().next(); if (nodes[otherNode].next.contains(id)) nodes[otherNode].next.remove(id); if (nodes[id].next.contains(otherNode)) nodes[id].next.remove(otherNode); } // see if we couldn't find more students if (badNodes.size() == 0) rem = false; else ans++; } System.out.println(ans); } } class Node { int id; HashSet<Integer> next; public Node(int id) { this.id = id; this.next = new HashSet<>(); } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
2e635f3d2a6690cd4f593e55f99f8be1
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Collections; import java.util.StringTokenizer; public class Task { public static void main(String args[]){ InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); Solver s = new Solver(in,out); s.solve(); out.close(); } } class Solver{ InputReader in; PrintWriter out; public void solve(){ boolean isR[][]=new boolean[100][100]; int cnt[] = new int[100]; int n=in.nextInt(), m=in.nextInt(); for (int i=0;i<m;i++){ int a=in.nextInt(), b=in.nextInt(); a--;b--; isR[a][b]=true; isR[b][a]=true; cnt[a]++; cnt[b]++; } int ta =0; for (int i=0;i<n;i++){ boolean del[] = new boolean[100]; for (int j=0;j<n;j++) if (cnt[j]==1) del[j]=true; boolean ok = false; for (int j=0;j<n;j++) if (del[j]){ ok = true; cnt[j]=0; for (int k=0;k<n;k++) if (isR[j][k]){ isR[j][k]=false; isR[k][j]=false; cnt[k]--; } } ta += (ok)?1:0; } out.println(ta); } Solver(InputReader in, PrintWriter out){ this.in=in; this.out=out; } } class InputReader{ private BufferedReader buf; private StringTokenizer tok; InputReader(InputStream in){ tok = null; buf = new BufferedReader(new InputStreamReader(in)); } public String next(){ while (tok==null || !tok.hasMoreTokens()){ try{ tok = new StringTokenizer(buf.readLine()); }catch (IOException e){ throw new RuntimeException(e); } } return tok.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public float nextFloat(){ return Float.parseFloat(next()); } public String nextLine(){ try{ return buf.readLine(); }catch (IOException e){ throw new RuntimeException(e); } } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
f2442ad246320ee8dda559254c27bf14
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main { public static void main(String[] args) throws IOException { (new Main()).solve(); } public Main() { } MyReader in = new MyReader(); PrintWriter out = new PrintWriter(System.out); void solve() throws IOException { //BufferedReader in = new BufferedReader(new //InputStreamReader(System.in)); //Scanner in = new Scanner(System.in); //Scanner in = new Scanner(new FileReader("input.txt")); //PrintWriter out = new PrintWriter("output.txt"); List<Set<Integer>> a = new ArrayList<Set<Integer>>(); int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { a.add(new HashSet<Integer>()); } for (int i = 0; i < m; i++) { int i1 = in.nextInt() - 1; int i2 = in.nextInt() - 1; a.get(i1).add(i2); a.get(i2).add(i1); } boolean flag = true; int count = 0; while (flag) { Set<Integer> del = new HashSet<Integer>(); for (int i = 0; i < n; i++) { if (a.get(i).size() == 1) { del.add(i); } } if (del.size() > 0) { for (Integer t : del) { Set<Integer> set = a.get(t); for (Integer p : set) { a.get(p).remove(t); } a.set(t, new HashSet<Integer>()); } count++; } else { flag = false; } } out.println(count); out.close(); } class MyReader { private BufferedReader in; String[] parsed; int index = 0; public MyReader() { in = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Integer.parseInt(parsed[index++]); } public long nextLong() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Long.parseLong(parsed[index++]); } public double nextDouble() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Double.parseDouble(parsed[index++]); } public String nextString() throws IOException { if (parsed == null || parsed.length == index) { read(); } return parsed[index++]; } private void read() throws IOException { parsed = in.readLine().split(" "); index = 0; } public String readLine() throws IOException { return in.readLine(); } }; };
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
913c2ac37078ea8266b19ec6a02af6f6
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; public class Main { public static void main(String args[])throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); StringTokenizer datos = new StringTokenizer(in.readLine()); int n = Integer.parseInt(datos.nextToken()); int m = Integer.parseInt(datos.nextToken()); int[] degree = new int[n]; List<List<Integer>> aL = new ArrayList<List<Integer>>(); for(int i=0;i<n;i++) aL.add(new ArrayList<Integer>()); for(int i=0;i<m;i++){ datos = new StringTokenizer(in.readLine()); int u = Integer.parseInt(datos.nextToken())-1; int v = Integer.parseInt(datos.nextToken())-1; aL.get(u).add(v); aL.get(v).add(u); degree[u]++; degree[v]++; } int res = -1; int c; do{ res++; c = 0; List<Integer> kicked = new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(degree[i]==1){ kicked.add(i); c++; } } for(int i : kicked){ degree[i] = 0; for(int k : aL.get(i)){ degree[k]--; } } }while(c!=0); out.println(res); out.flush(); } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
6e69690dd2d8ddf8e987208ff0225ac0
train_002.jsonl
1321337400
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
256 megabytes
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main { /********************************************** a list of common variables **********************************************/ private MyScanner scan = new MyScanner(); private PrintWriter out = new PrintWriter(System.out); private final long INF = Long.MAX_VALUE; private final double PI = Math.acos(-1.0); private final int SIZEN = (int)(1e5); private final int MOD = (int)(1e9 + 7); private final long MODH = 10000000007L, BASE = 10007; private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0}; private ArrayList<Integer>[] edge; public void foo() { int n = scan.nextInt(); int m = scan.nextInt(); boolean[][] isTied = new boolean[n + 1][n + 1]; for(int i = 0;i < m;++i) { int u = scan.nextInt(); int v = scan.nextInt(); isTied[u][v] = true; isTied[v][u] = true; } int ans = 0; while(true) { ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i = 1;i <= n;++i) { int cnt = 0; for(int j = 1;j <= n;++j) { if(isTied[i][j]) { ++cnt; if(cnt > 1) { break; } } } if(1 == cnt) { arr.add(i); } } if(arr.isEmpty()) { break; } ++ans; for(int i : arr) { Arrays.fill(isTied[i], false); for(int j = 1;j <= n;++j) { isTied[j][i] = false; } } } out.println(ans); } public static void main(String[] args) { Main m = new Main(); m.foo(); m.out.close(); } /********************************************** a list of common algorithms **********************************************/ /** * 1---Get greatest common divisor * @param a : first number * @param b : second number * @return greatest common divisor */ public long gcd(long a, long b) { return 0 == b ? a : gcd(b, a % b); } /** * 2---Get the distance from a point to a line * @param x1 the x coordinate of one endpoint of the line * @param y1 the y coordinate of one endpoint of the line * @param x2 the x coordinate of the other endpoint of the line * @param y2 the y coordinate of the other endpoint of the line * @param x the x coordinate of the point * @param y the x coordinate of the point * @return the distance from a point to a line */ public double getDist(long x1, long y1, long x2, long y2, long x, long y) { long a = y2 - y1; long b = x1 - x2; long c = y1 * (x2 - x1) - x1 * (y2 - y1); return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b); } /** * 3---Get the distance from one point to a segment (not a line) * @param x1 the x coordinate of one endpoint of the segment * @param y1 the y coordinate of one endpoint of the segment * @param x2 the x coordinate of the other endpoint of the segment * @param y2 the y coordinate of the other endpoint of the segment * @param x the x coordinate of the point * @param y the y coordinate of the point * @return the distance from one point to a segment (not a line) */ public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y) { double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1); if(cross <= 0) { return (x - x1) * (x - x1) + (y - y1) * (y - y1); } double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); if(cross >= d) { return (x - x2) * (x - x2) + (y - y2) * (y - y2); } double r = cross / d; double px = x1 + (x2 - x1) * r; double py = y1 + (y2 - y1) * r; return (x - px) * (x - px) + (y - py) * (y - py); } /** * 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1. * @param t: String to match. * @param p: String to be matched. * @return if can match, first index; otherwise -1. */ public int kmpMatch(char[] t, char[] p) { int n = t.length; int m = p.length; int[] next = new int[m + 1]; next[0] = -1; int j = -1; for(int i = 1;i < m;++i) { while(j >= 0 && p[i] != p[j + 1]) { j = next[j]; } if(p[i] == p[j + 1]) { ++j; } next[i] = j; } j = -1; for(int i = 0;i < n;++i) { while(j >= 0 && t[i] != p[j + 1]) { j = next[j]; } if(t[i] == p[j + 1]) { ++j; } if(j == m - 1) { return i - m + 1; } } return -1; } /** * 5---Get the hash code of a String * @param s: input string * @return hash code */ public long hash(String s) { long key = 0, t = 1; for(int i = 0;i < s.length();++i) { key = (key + s.charAt(i) * t) % MODH; t = t * BASE % MODH; } return key; } /** * 6---Get x ^ n % MOD quickly. * @param x: base * @param n: times * @return x ^ n % MOD */ public long quickMod(long x, long n) { long ans = 1; while(n > 0) { if(1 == n % 2) { ans = ans * x % MOD; } x = x * x % MOD; n >>= 1; } return ans; } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c & 15; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c & 15) * m; c = read(); } } return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
Java
["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"]
2 seconds
["0", "2", "1"]
NoteIn the first sample Anna and Maria won't kick out any group of students β€” in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β€” two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
Java 7
standard input
[ "implementation", "dfs and similar", "brute force", "graphs" ]
f8315dc903b0542c453cab4577bcb20d
The first line contains two integers n and m β€” the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β€” the numbers of students tied by the i-th lace (1 ≀ a, b ≀ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
1,200
Print the single number β€” the number of groups of students that will be kicked out from the club.
standard output
PASSED
02a22844413751590e2db0aa39b8fbd2
train_002.jsonl
1295626200
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Contest52_1 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String []s = new String[]{"ABSINTH", "BEER","BRANDY", "CHAMPAGNE","GIN","RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"}; int c = 0; for (int i = 0; i < n; i++) { String a = in.readLine(); if(Character.isDigit(a.charAt(0))){ int aa = Integer.parseInt(a); if(aa<18) c++; }else{ if(Arrays.binarySearch(s,a)>=0) c++; } } System.out.println(c); } }
Java
["5\n18\nVODKA\nCOKE\n19\n17"]
2 seconds
["2"]
NoteIn the sample test the second and fifth clients should be checked.
Java 6
standard input
[ "implementation" ]
4b7b0fba7b0af78c3956c34c29785e7c
The first line contains an integer n (1 ≀ n ≀ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol.
1,000
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
standard output
PASSED
771bf350d02899bf975ba4f216a0e4f2
train_002.jsonl
1295626200
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class BetaRound52_A implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws IOException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } @Override public void run() { try { long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } public static void main(String[] args) { new Thread(new BetaRound52_A()).start(); } final String[] alc = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"}; void solve() throws IOException { HashSet<String> set = new HashSet<String>(); for (int i = 0; i < alc.length; i++) { set.add(alc[i]); } int n = readInt(); int ans = 0; for (int i = 0; i < n; i++) { String s = readString(); if (set.contains(s)) { ans++; continue; } try { int a = Integer.parseInt(s); if (a < 18) ans++; } catch (Exception e) { continue; } } out.print(ans); } }
Java
["5\n18\nVODKA\nCOKE\n19\n17"]
2 seconds
["2"]
NoteIn the sample test the second and fifth clients should be checked.
Java 6
standard input
[ "implementation" ]
4b7b0fba7b0af78c3956c34c29785e7c
The first line contains an integer n (1 ≀ n ≀ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol.
1,000
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
standard output
PASSED
4812beccb0fded69b16c0277c6d3e40b
train_002.jsonl
1295626200
According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class BetaRound52_A implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws IOException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } @Override public void run() { try { long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } public static void main(String[] args) { new Thread(new BetaRound52_A()).start(); } final String[] alc = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"}; void solve() throws IOException { HashSet<String> set = new HashSet<String>(); for (int i = 0; i < alc.length; i++) { set.add(alc[i]); } int n = readInt(); int ans = 0; for (int i = 0; i < n; i++) { String s = readString(); if (set.contains(s)) { ans++; continue; } try { int a = Integer.parseInt(s); if (a < 18) ans++; } catch (Exception e) { continue; } } out.print(ans); } }
Java
["5\n18\nVODKA\nCOKE\n19\n17"]
2 seconds
["2"]
NoteIn the sample test the second and fifth clients should be checked.
Java 6
standard input
[ "implementation" ]
4b7b0fba7b0af78c3956c34c29785e7c
The first line contains an integer n (1 ≀ n ≀ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol.
1,000
Print a single number which is the number of people Vasya should check to guarantee the law enforcement.
standard output