Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Naloga9 {
  5.  
  6. public static int [] osebe;
  7. public static int [][] povezave;
  8. public static int N;
  9.  
  10. public static void main(String[] args) throws FileNotFoundException, IOException {
  11.  
  12. BufferedReader bufferedReader = new BufferedReader(new FileReader(args[0]));
  13. StringBuffer stringBuffer = new StringBuffer();
  14. String line = null;
  15.  
  16. line = bufferedReader.readLine();
  17.  
  18. Scanner sc = new Scanner(System.in);
  19. N = Integer.parseInt(line);
  20. line = bufferedReader.readLine();
  21. int M = Integer.parseInt(line);;
  22.  
  23. String result = "";
  24.  
  25. osebe = new int[N];
  26. povezave = new int[N][N];
  27. line = bufferedReader.readLine();
  28. for (int i = 0; i < M; i++) {
  29. String v = line;
  30. String [] vrstica = v.split(",");
  31. povezave[Integer.parseInt(vrstica[0]) - 1][Integer.parseInt(vrstica[1]) - 1] = 1;
  32. line = bufferedReader.readLine();
  33. }
  34.  
  35. for (int i = 0; i < N; i++) {
  36. boolean vsajEden = false;
  37. for (int j = 0; j < N; j++) {
  38. boolean poklici = true;
  39. if (osebe[j] == 1) continue;
  40. for (int k = 0; k < N; k++) {
  41. if (osebe[k] != 1) {
  42. if (povezave[j][k] == 1) {
  43. poklici = false;
  44. break;
  45. }
  46. }
  47. }
  48.  
  49. if (poklici) {
  50. vsajEden = true;
  51. osebe[j] = 1;
  52. result = result + (j+1) + ",";
  53. break;
  54. }
  55. }
  56. if(!vsajEden){
  57. result = "-1";
  58. break;
  59. }
  60.  
  61. }
  62.  
  63. if(!result.equals("-1"))
  64. result = result.substring(0, result.length() - 1);
  65.  
  66. BufferedWriter writer = new BufferedWriter(new FileWriter(args[1], true));
  67. writer.write(result);
  68.  
  69. writer.close();
  70.  
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement