1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class mis {
	public static mis inst;
	public static List<Road> allRoads = new ArrayList<Road>();
	public static List<City> allCities = new ArrayList<City>();
	public static BajtocjaMap map;
	public static int minRoads;
	public static void main(String[] args) {
		inst = new mis();
		Scanner scn = new Scanner(System.in);
		int[] firstArgs = getArray(scn.nextLine());
		int cities = firstArgs[0];
		int roads = firstArgs[1];
		minRoads = firstArgs[2];
		List<int[]> roadsRaw = new ArrayList<int[]>();
		print("Adding roads...");
		for(int i = 0; i<roads; i++) {
			roadsRaw.add(getArray(scn.nextLine()));
		}
		print("Creating road objects...");
		for(int[] road : roadsRaw) {
			allRoads.add(inst.getRoad(road));
		}
		print("Creating cities...");
		for(int i = 1; i<=cities; i++) {
			allCities.add(inst.createCity(i));
		}
		print("Summary: " + allCities.size() + " cities created, "+allRoads.size() + " roads created.");
		print("Creating map...");
		map = inst.createMap(allCities.toArray(new City[allCities.size()]), allRoads.toArray(new Road[allRoads.size()]));
		print("Parsing city communications for map with " + map.cities.length + " cities and " + map.allRoads.length + " roads.");
		map.getWellCommCities();
		print("Cleaning up the arrays and printing...");
		StringBuilder sb = new StringBuilder();
		Arrays.sort(map.wellCommCityID);
		int[] sorted = map.wellCommCityID;
		for(int i : sorted) {
			print("int array2 has " + i);
		}
		if(sorted.length == 1 && sorted[0] == 0) {
			sorted = new int[0];
		}
		for(int i = 0; i < sorted.length; i++) {
			if(i+1 < sorted.length)
				sb.append(sorted[i] + " ");
			else
				sb.append(sorted[i]);
		}
		if(sb.toString().equals(""))
			System.out.println("NIE");
		else 
		{
			System.out.println(sorted.length);
			System.out.println(sb.toString());
		}
	}
	public static int[] sort(int[] source) {
		List<Integer> source_ = new ArrayList<Integer>();
		for(int i : source) {
			source_.add(i);
		}
		int[] ret = new int[source.length];
		for(int i = 0; i < source_.size(); i++) {
			int lowest = 199999;
			for(int k : source_) {
				if(k<lowest)
					lowest=k;
			}
			source_.remove(source_.indexOf(lowest));
			ret[i] = lowest;
		}
		return ret;
	}
	public static int[] cast(Integer[] source) {
		int[] ret = new int[source.length];
		for(int i = 0; i<source.length; i++) {
			ret[i] = source[i];
		}
		return ret;
	}
	public BajtocjaMap createMap(City[] cities, Road[] roads) {
		return new BajtocjaMap(cities, roads);
	}
	public City createCity(int id) {
		List<Road> cityRoads = new ArrayList<Road>();
		for(Road r : allRoads) {
			if(r.cityFrom == id || r.cityTo == id) {
				cityRoads.add(r);
			}
		}
		return new City(id, cityRoads.toArray(new Road[0]));
	}
	public static void print(String s) {
		//System.out.println(s);
	}
	public Road getRoad(int[] param) {
		return new Road(param[0],param[1]);
	}
	public boolean isCommWith(City cityone, City citytwo) {
		if(cityone.equals(citytwo))
			return false;
		for(Road r : cityone.roads) {
			if(r.cityTo == citytwo.id || r.cityFrom == citytwo.id)
				return true;
		}
		return false;
	}
	public static int[] getArray(String args) {
		String[] arr = args.split(" ");
		int[] arr_ = new int[arr.length];
		for(int i = 0; i< arr.length; i++) {
			arr_[i] = Integer.decode(arr[i]);
		}
		return arr_;
	}
	public boolean isWellComm(City c) {
		return (c.roads.length > (minRoads-1));
	}
	public class Road {
		public int cityFrom;
		public int cityTo;
		public Road(int from, int to) {
			this.cityFrom = from;
			this.cityTo = to;
		}
		@Override
		public boolean equals(Object o) {
			if(o instanceof Road)
				return equals((Road)o);
			else
				return false;
		}
		public boolean equals(Road r) {
			return ((this.cityTo == r.cityTo) && (this.cityFrom == r.cityFrom));
		}
		@Override
		public int hashCode() {
			return Objects.hash(this.cityTo, this.cityFrom);
		}
	}
	public class City {
		public Road[] roads;
		public int id;
		public int[] citiesConnected;
		public City(int id, Road[] roads) {
			this.roads = roads;
			this.id = id;
		}
	}
	public class BajtocjaMap {
		public City[] cities;
		public int[] wellCommCityID;
		public Road[] allRoads;
		public BajtocjaMap(City[] cities, Road[] roads) {
			this.cities = cities;
			this.allRoads = roads;
		}
		public City getCityByID(int id) {
			for(City c : cities) {
				if(c.id == id)
					return c;
			}
			return null;
		}
		public void getWellCommCities() {
			List<City> wellcomm = new ArrayList<City>();
			print("Declaring the hashmap...");
			HashMap<Integer, List<Integer>> commedCities = new HashMap<Integer, List<Integer>>();
			print("Iterating over all map's cities...");
			for(City c : this.cities) {
				print("City C has ID " + c.id + ", and " + c.roads.length + " roads. Minimum is " + minRoads);
				if(inst.isWellComm(c)) {
					wellcomm.add(c);
					print("WELL-COMMED CITY #"+c.id);
				}
			}
			for(City x : wellcomm) {
				List<Integer> commedWith = new ArrayList<Integer>();
				for(City y : wellcomm) {
					if(inst.isCommWith(x, y)) {
						commedWith.add(y.id);
					}
				}
				print("WELL-COMMED CITY #"+x.id + " IS COMMED WITH " + commedWith.size() + " OTHER CITIES");
				commedCities.put(x.id, commedWith);
			}
			int mostConnection = 0;
			int mostConnCityID = 0;
			for(Integer i : commedCities.keySet()) {
				List<Integer> ints = commedCities.get(i);
				if(ints.size() > mostConnection) {
					mostConnection = ints.size();
					mostConnCityID = i;
				}
			}
			print("Got a middle-point city (id:"+mostConnCityID+"), creating its area...");
			List<Integer> cityCommArea = new ArrayList<Integer>();
			//City start = getCityByID(mostConnCityID);
			cityCommArea.add(mostConnCityID);
			List<City> nonWellCommed = new ArrayList<City>();
			int loopsWithNoYield = 0;
			int iteration = 1;
			boolean yield = false;
			while(true) {
				yield=false;
				print("Iteration #"+iteration+", no-yield iterations so far: "+loopsWithNoYield);
				if(loopsWithNoYield >= 2) {
					break;
				}
				for(City c : map.cities) {
					print("Iterating over city #"+c.id);
					if(nonWellCommed.contains(c) || cityCommArea.contains(c.id))
						continue;
					if(!isWellComm(c)) {
						nonWellCommed.add(c);
						continue;
					}
					boolean isComm = false;
					for(int i : cityCommArea) {
						for(Road r : c.roads) {
							if(r.cityFrom == i || r.cityTo == i) {
								isComm = true;
							}
						}
					}
					if(isComm) {
						cityCommArea.add(c.id);
						yield=true;
					}
				}
				if(!yield)
					loopsWithNoYield++;
				iteration++;
			}
			Integer[] wellCommed = cityCommArea.toArray(new Integer[0]);
			for(int i : wellCommed) {
				print("int array has "+i);
			}
			this.wellCommCityID = cast(wellCommed);
		}
	}
}