fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class R485PD {
  5.  
  6. static FastInput k = new FastInput(System.in);
  7. static FastOutput z = new FastOutput();
  8.  
  9. static int nodes, edges, kk, s;
  10.  
  11. static Graph graph;
  12. static ArrayList<Integer> [] types;
  13. static int [] nodeTypes;
  14. static int [][] data;
  15.  
  16. static void startAlgorithm() {
  17. for(int type = 1; type<=kk; type++) {
  18. graph.bfs(type);
  19. }
  20.  
  21. for(int i = 0; i<nodes; i++) {
  22. int sum = 0;
  23.  
  24. ArrayList<Integer> temp = new ArrayList<Integer>();
  25. for(int j = 0; j<data[i].length; j++) {
  26. if(data[i][j] != 0) {
  27. temp.add(data[i][j]);
  28. }
  29. }
  30.  
  31. Collections.sort(temp);
  32.  
  33. for(int j = 0; j<s-1; j++) {
  34. sum += temp.get(j);
  35. }
  36.  
  37. if (i != 0) z.print(" ");
  38. z.print(sum);
  39. }
  40.  
  41. z.println();
  42. z.flush();
  43. }
  44.  
  45. @SuppressWarnings("unchecked")
  46. static void getInputsAndInitializes() {
  47. nodes = k.nextInt();
  48. edges = k.nextInt();
  49.  
  50. kk = k.nextInt();
  51. s = k.nextInt();
  52.  
  53. graph = new Graph(nodes, edges);
  54. data = new int[nodes][kk+1];
  55. types = new ArrayList[kk+1];
  56. nodeTypes = new int[nodes];
  57.  
  58. for(int i = 0; i<kk+1; i++) {
  59. types[i] = new ArrayList<Integer>();
  60. }
  61.  
  62. for(int i = 0; i<nodes; i++) {
  63. int type = k.nextInt();
  64. types[type].add(i);
  65. nodeTypes[i] = type;
  66. }
  67.  
  68. //i.inputArray(goodType, nodes, InputType.INTEGER);
  69. k.inputUndirectedGraph(graph, edges);
  70. }
  71.  
  72. public static void main(String [] args) throws FileNotFoundException {
  73. while(k.hasNext()) {
  74. getInputsAndInitializes();
  75. startAlgorithm();
  76. }
  77. }
  78.  
  79.  
  80. /* MARK: InputType enum */
  81.  
  82. public enum InputType {
  83. INTEGER, LONG, WORD, LINE;
  84. }
  85.  
  86.  
  87. /* MARK: PrintType enum */
  88.  
  89. public enum PrintType {
  90. SIDEBYSIDE, LINEBYLINE;
  91. }
  92.  
  93.  
  94. /* MARK: Graph implementation */
  95.  
  96. public static class Graph {
  97. int nodes, edges;
  98. ArrayList<Integer> [] neighbors;
  99.  
  100. @SuppressWarnings("unchecked")
  101. public Graph(int nodes) {
  102. this.nodes = nodes;
  103. neighbors = new ArrayList[nodes];
  104.  
  105. initializeGraph();
  106. }
  107.  
  108. @SuppressWarnings("unchecked")
  109. public Graph(int nodes, int edges) {
  110. this.nodes = nodes;
  111. this.edges = edges;
  112. neighbors = new ArrayList[nodes];
  113.  
  114. initializeGraph();
  115. }
  116.  
  117. public void initializeGraph() {
  118. for(int i = 0; i<nodes; i++) {
  119. neighbors[i] = new ArrayList<Integer>();
  120. }
  121. }
  122.  
  123. public void addDirectedEdge(int u, int v) {
  124. neighbors[u].add(v);
  125. }
  126.  
  127. public void addUndirectedEdge(int u, int v) {
  128. neighbors[u].add(v);
  129. neighbors[v].add(u);
  130. }
  131.  
  132. public void bfs(int type) {
  133.  
  134. int [] d = new int[nodes];
  135. Arrays.fill(d, Integer.MAX_VALUE);
  136.  
  137. MyArrayDeque q = new MyArrayDeque();
  138. for(int i = 0; i<types[type].size(); i++) {
  139. int node = types[type].get(i);
  140. q.enqueue(node);
  141. d[node] = 0;
  142. }
  143.  
  144. while(!q.isEmpty()) {
  145. int u = q.dequeue();
  146. for(int i = 0; i<neighbors[u].size(); i++) {
  147. int v = neighbors[u].get(i);
  148. if(d[v] > d[u] + 1 && nodeTypes[v] != type) {
  149. d[v] = d[u] + 1;
  150. q.enqueue(v);
  151. data[v][type] = d[v];
  152. }
  153. }
  154. }
  155. }
  156. }
  157.  
  158.  
  159. /* MARK: MyArrayDeque implementation */
  160.  
  161. public static class MyArrayDeque {
  162. int [] queue;
  163. int frontCursor;
  164. int backCursor;
  165.  
  166. public MyArrayDeque() {
  167. queue = new int[nodes+1];
  168. frontCursor = 0;
  169. backCursor = 0;
  170. }
  171.  
  172. public boolean isEmpty() {
  173. return backCursor > frontCursor;
  174. }
  175.  
  176. public void enqueue(int value) {
  177. queue[frontCursor] = value;
  178. frontCursor++;
  179. }
  180.  
  181. public int dequeue() {
  182. int returnValue = queue[backCursor];
  183. backCursor++;
  184.  
  185. return returnValue;
  186. }
  187. }
  188.  
  189. /* MARK: FastInput and FastOutput implementation */
  190.  
  191. public static class FastInput {
  192. StringTokenizer tokenizer;
  193.  
  194. public FastInput(InputStream stream){
  195. reader = new BufferedReader(new InputStreamReader(stream));
  196. }
  197.  
  198. public FastInput(String path){
  199. try {
  200. reader = new BufferedReader(new FileReader(path));
  201. } catch (FileNotFoundException e) {
  202. // TODO Auto-generated catch block
  203. e.printStackTrace();
  204. }
  205. tokenizer = null;
  206. }
  207.  
  208. public String next() {
  209. return nextToken();
  210. }
  211.  
  212. public String nextLine() {
  213. try {
  214. return reader.readLine();
  215. } catch (IOException e) {
  216. // TODO Auto-generated catch block
  217. e.printStackTrace();
  218. }
  219.  
  220. return null;
  221. }
  222.  
  223. public boolean hasNext(){
  224. try {
  225. return reader.ready();
  226. } catch (IOException e) {
  227. // TODO Auto-generated catch block
  228. e.printStackTrace();
  229. }
  230. return false;
  231. }
  232.  
  233. public String nextToken() {
  234. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  235. String line = null;
  236. try {
  237. line = reader.readLine();
  238. } catch (IOException e) {
  239. throw new RuntimeException(e);
  240. }
  241. if (line == null) {
  242. return null;
  243. }
  244. tokenizer = new StringTokenizer(line);
  245. }
  246. return tokenizer.nextToken();
  247. }
  248.  
  249. public Integer nextInt() {
  250. return Integer.parseInt(nextToken());
  251. }
  252.  
  253. public Long nextLong() {
  254. return Long.valueOf(nextToken());
  255. }
  256.  
  257. public void inputDirectedGraph(Graph graph, int edges) {
  258. while(edges--> 0) {
  259. int u = k.nextInt(); u--;
  260. int v = k.nextInt(); v--;
  261.  
  262. graph.addDirectedEdge(u, v);
  263. }
  264. }
  265.  
  266. public void inputUndirectedGraph(Graph graph, int edges) {
  267. while(edges--> 0) {
  268. int u = k.nextInt(); u--;
  269. int v = k.nextInt(); v--;
  270.  
  271. graph.addUndirectedEdge(u, v);
  272. }
  273. }
  274.  
  275. public <T> void inputList(ArrayList<T> list, int size, InputType inputType) {
  276. switch (inputType) {
  277. case INTEGER:
  278. inputIntegerList(list, size); break;
  279. case LONG:
  280. inputLongList(list, size); break;
  281. case WORD:
  282. inputWordList(list, size); break;
  283. case LINE:
  284. inputLineList(list, size); break;
  285. default: break;
  286. }
  287. }
  288.  
  289. @SuppressWarnings("unchecked")
  290. public <T> void inputIntegerList(ArrayList<T> list, int size) {
  291. for(int i = 0; i<size; i++) {
  292. list.add((T)nextInt());
  293. }
  294. }
  295.  
  296. @SuppressWarnings("unchecked")
  297. public <T> void inputLongList(ArrayList<T> list, int size) {
  298. for(int i = 0; i<size; i++) {
  299. list.add((T)nextLong());
  300. }
  301. }
  302.  
  303. @SuppressWarnings("unchecked")
  304. public <T> void inputWordList(ArrayList<T> list, int size) {
  305. for(int i = 0; i<size; i++) {
  306. list.add((T)next());
  307. }
  308. }
  309.  
  310. @SuppressWarnings("unchecked")
  311. public <T> void inputLineList(ArrayList<T> list, int size) {
  312. for(int i = 0; i<size; i++) {
  313. list.add((T)nextLine());
  314. }
  315. }
  316.  
  317. public <T> void inputArray(T [] array, int size, InputType inputType) {
  318. switch (inputType) {
  319. case INTEGER:
  320. inputIntegerArray(array, size); break;
  321. case LONG:
  322. inputLongArray(array, size); break;
  323. case WORD:
  324. inputWordArray(array, size); break;
  325. case LINE:
  326. inputLineArray(array, size); break;
  327. default: break;
  328. }
  329. }
  330.  
  331. @SuppressWarnings("unchecked")
  332. public <T> void inputIntegerArray(T [] array, int size) {
  333. for(int i = 0; i<size; i++) {
  334. array[i] = (T)nextInt();
  335. }
  336. }
  337.  
  338.  
  339. @SuppressWarnings("unchecked")
  340. public <T> void inputLongArray(T [] array, int size) {
  341. for(int i = 0; i<size; i++) {
  342. array[i] = (T)nextLong();
  343. }
  344. }
  345.  
  346. @SuppressWarnings("unchecked")
  347. public <T> void inputWordArray(T [] array, int size) {
  348. for(int i = 0; i<size; i++) {
  349. array[i] = (T)next();
  350. }
  351. }
  352.  
  353. @SuppressWarnings("unchecked")
  354. public <T> void inputLineArray(T [] array, int size) {
  355. for(int i = 0; i<size; i++) {
  356. array[i] = (T)nextLine();
  357. }
  358. }
  359. }
  360.  
  361. public static class FastOutput extends PrintWriter {
  362. public FastOutput() {
  363. super(new BufferedOutputStream(System.out));
  364. }
  365.  
  366. public <T> void printSet(Set<T> set, int size, PrintType printType) {
  367. switch (printType) {
  368. case SIDEBYSIDE:
  369. printSideBySideSet(set, size); break;
  370. case LINEBYLINE:
  371. printLineByLineSet(set, size); break;
  372. default: break;
  373. }
  374. }
  375.  
  376. public <T> void printSideBySideSet(Set<T> set, int size) {
  377. boolean isFirstElementPrinted = false;
  378. for(T element : set) {
  379. if(!isFirstElementPrinted) {
  380. print(element);
  381. isFirstElementPrinted = true;
  382.  
  383. return;
  384. }
  385. print(" "+element);
  386. }
  387. println();
  388. }
  389.  
  390. public <T> void printLineByLineSet(Set<T> set, int size) {
  391. for(T element : set) {
  392. println(element);
  393. }
  394. }
  395.  
  396. public <T> void printList(ArrayList<T> list, int size, PrintType printType) {
  397. switch (printType) {
  398. case SIDEBYSIDE:
  399. printSideBySideList(list, size); break;
  400. case LINEBYLINE:
  401. printLineByLineList(list, size); break;
  402. default: break;
  403. }
  404. }
  405.  
  406. public <T> void printSideBySideList(ArrayList<T> list, int size) {
  407. print(list.get(0));
  408. for(int i = 1; i<size; i++) {
  409. print(" "+list.get(i));
  410. }
  411. println();
  412. }
  413.  
  414. public <T> void printLineByLineList(ArrayList<T> list, int size) {
  415. for(int i = 0; i<size; i++) {
  416. println(list.get(i));
  417. }
  418. }
  419.  
  420. public <T> void printArray(T [] array, int size, PrintType printType) {
  421. switch (printType) {
  422. case SIDEBYSIDE:
  423. printSideBySideArray(array, size); break;
  424. case LINEBYLINE:
  425. printLineByLineArray(array, size); break;
  426. default: break;
  427. }
  428. }
  429.  
  430. public <T> void printSideBySideArray(T [] array, int size) {
  431. print(array[0]);
  432. for(int i = 1; i<size; i++) {
  433. print(" "+array[i]);
  434. }
  435. println();
  436. }
  437.  
  438. public <T> void printLineByLineArray(T [] array, int size) {
  439. for(int i = 0; i<size; i++) {
  440. println(array[i]);
  441. }
  442. }
  443. }
  444. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class R485PD is public, should be declared in a file named R485PD.java
public class R485PD {
       ^
1 error
stdout
Standard output is empty