import java.util.*;
class BFS extends Graph {
Queue<Integer> s = new LinkedList<Integer>();

int BFSTree(int root) {
 int Search = new int [nodes.size()];
 int tree = new int [nodes.size()];
 s.offer(root);
 for (int i = 0; i < nodes.size(); i++) {
 Search[i] = 1;
 }
while (s.peek() != null) {
 int x = s.poll();
 Search[root] = -1;
 for (int k = 0; k < nodes.get(x).getNum(); k++) {
 if (Search[list.get(nodes.get(x).getPointer() + k).To()] == 1 ){
 s.offer(list.get(nodes.get(x).getPointer() + k).To());
 Search[list.get(nodes.get(x).getPointer() + k).To()] = -1 ;
 tree[list.get(nodes.get(x).getPointer() + k).To()] = x;}
   }
  }
 return tree;
 }
}
class BFSTester {
public static void main (String args){
 int[] Tree;
 BFS b = new BFS();
 b.LoadGraph("graph001.txt");
 Tree = b.BFSTree(0);
 System.out.print(Tree[0]);
 for (int m = 1; m < Tree.length; m++)
 System.out.print("," + Tree[m]);
  }
 }

import java.util.*;
import java.io.*;
public class Graph {
 int num = 0;
  ArrayList<Edge> list = new ArrayList<Edge>();  //隣接ノード//
  ArrayList<Node> nodes = new ArrayList<Node>();  //ノード番号、隣接数、先頭オフセット//
 class Edge {
  private int to;
  Edge(int to) {this.to = to;}
  int To() { return to;}
 }
 class Node {
  int id; int Num; int pointer;
  Node (int id,int Num, int pointer) {this.id = id; this.Num = Num; this.pointer = pointer;}
  int getNum() {return this.Num;}
  int getId(){ return this.id;}
  int getPointer() { return this.pointer;}
  }

 void LoadGraph(String filename){
 try{
  Scanner scan = new Scanner(new File(filename));
  int pointer = 0;
 while(true){
 if( !scan.hasNextLine()) {
   break;
 }
 String line = scan.nextLine();
 String s = line.split(":",2);
 String
t = s[1].split(",",0);
 for (int i = 0; i < t.length; i++) {
 Edge e = new Edge(Integer.parseInt(t[i]));
 list.add(e);
 }
 Node n = new Node(Integer.parseInt(s[0]), t.length, pointer);
 nodes.add(n);
 num ++;
 pointer = pointer + t.length;
  }
 } catch (java.io.FileNotFoundException e){
  System.out.println(e);
  System.exit(0);
  }
 }

 void printGraph(){
 for (int j = 0; j < num ; j++) {
 System.out.print(nodes.get(j).getId() + ":");
 System.out.print(list.get(nodes.get(j).getPointer()).To()); 
 for (int k = 1; k < nodes.get(j).getNum(); k++) {
 System.out.print(", " + list.get(nodes.get(j).getPointer() + k).To());
   }
 System.out.println();
  }

}
}
 

kp

public class Solver {
 int n, capacity, answer;
 int weight, value;
 int
D;
 public Solver(int n, int weight, int value, int capacity) {
 this.n = n; this.weight = weight; this.value = value; this.capacity = capacity;
 }
 int solve(){
 int D = new int [n + 1][capacity + 1];
 for(int s = 0; s <= n; s++){ D[s][0] = 0;}
 for(int t = 1; t <= capacity; t++){ D[0][t] = 0;}
 for(int i = 1; i <= n; i++){
  for(int j = 1; j <= capacity; j++){
   if(j < weight[i]) D[i][j] = D[i-1][j];
   else {
 int v1 = D[i-1][j - weight[i]] + value[i];
 int v2 = D[i-1][j];
   if (v1 > v2)
       D[i][j] = v1;
   else   D[i][j] = v2;
  }
 }
 }
 return D[n][capacity];
 }
 }
 
 
 
 
 
 
 
 
import java.util.Scanner;
import java.io.File;
public class KP {
 public static void main(String args) {
 int num =0;
 int capacity = 0;
 int answer = 0;
 int weight = {0};
 int
value  = {0};
 try{
 File file = new File("1.txt");
 Scanner scan = new Scanner(file);
 String Num = scan.nextLine();
 String T = Num.split(" ",2);
 num = Integer.parseInt(T[0]);
 capacity = Integer.parseInt(T[1]);
 weight = new int[num + 1];
 value = new int[num + 1];
 Scanner Scan = new Scanner(file);
 String Num2 = scan.nextLine();
 for (int i = 0; i <= num ; i++){
  if (!Scan.hasNextLine()){
   break;
  }
  String line = Scan.nextLine();
 String date = line.split(" ",2);
 weight[i] = Integer.parseInt(date[0]);
 value[i] = Integer.parseInt(date[1]); 
 System.out.println(weight[i]+" "+ value[i]); 
   }
   }catch(java.io.FileNotFoundException e){
  System.out.println(e);
  System.exit(0);
 }

 Solver s = new Solver(num, weight, value, capacity);
 answer = s.solve();
 System.out.println(answer);
 }
}

fibo

class FiboRec {
public int FibR(int n) {
 if (n <= 2) return 1;
 else
 return FibR(n - 1) + FibR(n - 2);
 }
 }
class FiboDP {
public int FibD(int m) {
 int F = new int[m];
 F[0] = F[1] = 1;
 for (int i = 2; i < m ; i++)
 F[i] = F[i-1] + F[i-2];
 return F[m-1];
 }
}
public class FiboTime {
 public static void main (String args){
 int n = Integer.parseInt(args[0]);
 FiboRec rec = new FiboRec();
 FiboDP  dp  = new FiboDP();
 double t1 = System.nanoTime();
 System.out.println("FiboRec " + n + "\n" + rec.FibR(n));
 double t2 = System.nanoTime();
 System.out.println*1;
 double s2 = System.nanoTime();
 System.out.println((s2 - s1)/1000000 + " ms" );
 }
}

*1:t2 - t1)/1000000 + " ms\n" );

 double s1 = System.nanoTime();
 System.out.println("FiboDP " + n + "\n" + dp.FibD(n

import java.util.*;
public class  SignalSimulator {
 public static void main (String[] args) {
 if (args.length != 3) {
 System.out.println("使用方法: java SignalSimulator 車列への追加周期 車列の上限 信号の周期");
 System.out.println("使用例  : java SignalSimulator 1 100 30");
   }
 else {
 int period = 10000;
 int L = Integer.parseInt(args[0]);
 int M = Integer.parseInt(args[1]);
 int N = Integer.parseInt(args[2]);
 
 Queue<String> queue = new LinkedList<String>();
 for (int t = 0; t < period; t++) {
 int signal = t/N;
 if (t % N == 0) {
 if (signal % 2 == 0) System.out.println("\n信号が赤になりました。\n");
 if (signal % 2 == 1) System.out.println("\n信号が青になりました。\n");
  }

 if (t % L == 0) {
 Random rand = new Random();
 int car = rand.nextInt(4);
 if (car != 0) queue.offer("t");
 System.out.println("時刻" + t + ":車が車列に入りました。 車列:" + queue.size());
 }
 // 車の追加
 if (signal % 2 == 1) {
 System.out.println("時刻" + t + ":車が信号を通過しました。車列:" + queue.size()); queue.poll();
  }
 //信号青ならすすむ
 if (queue.size() == M) {
 System.out.println("車列が" + M + "台に到達しました。シュミレーションを終了します。"); break;
  }
     }
 }
    }
}

import java.io.*;
public class FindFile {
 public static void searchDir(String a,String b) {
 File A = new File(a); 
 String files = A.list();
 if ( A.list() == null){
 if (a.indexOf(b) != -1)
 System.out.println(A.getAbsolutePath());
 }
 else
 for (int i = 0; i < files.length ; i++) {
 searchDir(a + "/" + files[i],b);
  }
 }
 public static void main (String args) {
 String word = args[0].split("/",2);
 searchDir(word[0],word[1]);
    }
}
 
 
 
 
 
 
 
 
import java.io.File;
class MakeDir {
 public static void main(String args){
 String Dir = args[0];
 String [] dir = Dir.split("/",0);
 for(int i = 0; i <= Dir.length(); i++){
 File newfile = new File(dir[0]);
 if (newfile.mkdir()){
      System.out.println("ディレクトリ「" + dir[i] + "」は存在しません。ディレクトリの作成に成功しました");
    }else{
      System.out.println("ディレクトリ「" + dir[i] + "」は存在します。");
      }
 dir[0] = dir[0] + "/" + dir[i + 1];
    }
  }
}
 
 
 
 

import java.io.*;
public class FindFile1 {
 public static void main (String args) {
  if (args.length != 2) {
  System.out.println("使用例:java FindFile1 検索文字列 検索対象ファイル");
  System.out.println("例:java FindFile1 System FindFile1.java");
  System.exit(0);
 }
 String findstring = args[0];
 String filename = args[1];
 try {
  String line;
  int linenum = 1;
  BufferedReader reader = new BufferedReader(new FileReader(filename));
  while ((line = reader.readLine()) != null) {
   int n = line.indexOf(findstring);
   if (n >= 0) {
   System.out.println(linenum + ":" + line);
   }
   linenum++;
  }
  reader.close();
 }catch (FileNotFoundException e) {
  System.out.println(filename + "が見つかりません。");
 }catch (IOException e) {
  System.out.println(e);
  }
 }
}
 
 
 
 
 
 
 
 
 
 
 
import java.io.*;
public class Prime1 {
 static int MAX_PRIME = 1000;
 public static void main (String args) {
  if (args.length != 1) {
  System.out.println("使用法:java Prime1 作成ファイル");
  System.out.println("例:java Prime1 prime.txt");
  System.exit(0);
  }
  String filename = args[0];
  try {
   PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
   writePrime(writer);
   writer.close();
  } catch (IOException e) {
   System.out.println(e);
  }
 }
 public static void writePrime(PrintWriter writer) {
  boolean[] prime = new boolean[MAX_PRIME];
  for (int n = 0; n < MAX_PRIME; n++) {
   prime[n] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int n = 0; n < MAX_PRIME; n++) {
   if (prime[n]) {
    writer.println(n);
    for (int i = 2; i * n < MAX_PRIME; i++) {
     prime[i * n] = false;
    }
       }
  }
     }
}