| Task: | Bus lines |
| Sender: | Anonyymit Algoritmistit |
| Submission time: | 2015-09-30 19:29:21 +0300 |
| Language: | Java |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.41 s | details |
| #2 | ACCEPTED | 0.57 s | details |
| #3 | ACCEPTED | 0.49 s | details |
| #4 | ACCEPTED | 0.64 s | details |
| #5 | ACCEPTED | 0.59 s | details |
| #6 | ACCEPTED | 0.64 s | details |
| #7 | ACCEPTED | 0.68 s | details |
| #8 | ACCEPTED | 0.70 s | details |
| #9 | ACCEPTED | 0.45 s | details |
| #10 | ACCEPTED | 0.52 s | details |
| #11 | ACCEPTED | 0.53 s | details |
| #12 | ACCEPTED | 0.55 s | details |
| #13 | ACCEPTED | 0.86 s | details |
| #14 | ACCEPTED | 0.78 s | details |
| #15 | ACCEPTED | 0.74 s | details |
| #16 | ACCEPTED | 0.83 s | details |
| #17 | ACCEPTED | 0.78 s | details |
| #18 | ACCEPTED | 0.77 s | details |
| #19 | ACCEPTED | 0.78 s | details |
| #20 | ACCEPTED | 0.76 s | details |
| #21 | ACCEPTED | 0.78 s | details |
| #22 | ACCEPTED | 0.97 s | details |
| #23 | ACCEPTED | 0.80 s | details |
| #24 | ACCEPTED | 0.76 s | details |
| #25 | ACCEPTED | 0.78 s | details |
Code
//package kilo4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author asjuvone
*/
public class T3 {
public static int n;
public static int m;
public static int k;
public static Linja[] linjat;
public static Linja[] poistot;
public static int[] isa;
public static int[] koko;
public static int yhdistysCount;
public static void main(String[] args) {
IO io = new IO();
n = io.nextInt();
m = io.nextInt();
k = io.nextInt();
HashMap<Linja, Integer> ekaEsiintyma = new HashMap<>();
HashMap<Linja, Integer> tuplat = new HashMap<>();
linjat = new Linja[m+1];
for (int i=1; i<=m; i++) {
Linja uus = new Linja(io.nextInt(), io.nextInt());
if (tuplat.containsKey(uus)) {
tuplat.put(uus, tuplat.get(uus) + 1);
} else {
tuplat.put(uus, 1);
ekaEsiintyma.put(uus, i);
}
linjat[i] = (uus);
}
ArrayList<Linja> poistot = new ArrayList<>();
for (int i=1; i<=k; i++) {
int kek = io.nextInt();
Linja poisto = linjat[kek];
linjat[kek] = null;
if (tuplat.get(poisto) > 1) {
tuplat.put(poisto, tuplat.get(poisto)-1);
poistot.add(new Linja(-1,-1));
continue;
}
poistot.add(poisto);
}
yhdistysCount = 0;
int[] vastaukset = new int[k+1];
isa = new int[n+1];
koko = new int[n+1];
Arrays.fill(koko, 1);
for (int i=1; i<=n; i++) isa[i] = i;
for (int i=1; i<=m; i++) {
if (linjat[i] == null) continue;
yhdista(linjat[i]);
}
//System.out.println("Region count kaikkien rikkojen jälkeen: " + (n - yhdistysCount));
for (int i=k; i>=1; i--) {
//System.out.println("n: " + n + ", regincount: " + (n-yhdistysCount));
vastaukset[i] = n - yhdistysCount;
if (poistot.get(i-1).a > 0) yhdista(poistot.get(i-1));
}
//System.out.println(Arrays.toString(vastaukset));
for (int i=1; i<=k; i++) {
System.out.println(vastaukset[i]);
}
io.close();
}
public static void yhdista(Linja uus) {
//System.out.println("Yhdistetään " + uus.a + " ja " + uus.b);
//System.out.println("Isät: " + Arrays.toString(isa));
//System.out.println("moi" + " " + uus.a + " , " + uus.b);
int a = uus.a;
int b = uus.b;
while (a != isa[a]) {
a = isa[a];
}
while (b != isa[b]) b = isa[b];
if (a == b) {
return;
}
if (koko[a] > koko[b]) {
isa[b] = a;
koko[a] += koko[b];
} else {
isa[a] = b;
koko[b] += koko[a];
}
yhdistysCount++;
}
}
class Linja {
public int a;
public int b;
public Linja(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
Linja toinen = (Linja) obj;
if (toinen.a == this.b && toinen.b == this.a) return true;
return toinen.a == this.a && toinen.b == this.b;
}
@Override
public int hashCode() {
return a + 36*b;
}
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 42712 59669 3327 27393 5935 40762 40800 33005 34639 19131 2850 ... |
| correct output |
|---|
| 2937 2937 2937 2937 2937 2938 ... |
| user output |
|---|
| 2937 2937 2937 2937 2937 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 76287 96164 41787 58072 25022 46787 62110 30339 48282 73612 52159 ... |
| correct output |
|---|
| 6969 6969 6969 6969 6970 6970 ... |
| user output |
|---|
| 6969 6969 6969 6969 6970 ... |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 18336 40887 13506 13131 7525 2330 5108 1053 8852 2934 4194 ... |
| correct output |
|---|
| 208 208 208 208 208 208 208 20... |
| user output |
|---|
| 208 208 208 208 208 ... |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10251 91042 26842 3863 3691 4990 6580 3722 1896 1240 6558 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 ... |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 75339 65466 44858 37059 62665 14790 65885 12864 58973 70022 19675 ... |
| correct output |
|---|
| 16333 16334 16335 16336 16336 ... |
| user output |
|---|
| 16333 16334 16335 16336 16336 ... |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 2086 66519 52411 361 1822 723 1145 1914 914 2075 1759 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 ... |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 38126 95132 68427 15424 17170 2781 12706 33834 13802 13405 25848 ... |
| correct output |
|---|
| 246 246 246 246 246 246 246 24... |
| user output |
|---|
| 246 246 246 246 246 ... |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 13057 86973 49052 5610 4743 1062 7853 6391 10993 5814 10957 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 ... |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 88876 29498 13976 16610 72903 2735 47496 44512 709 3413 60896 ... |
| correct output |
|---|
| 59380 59381 59382 59383 59384 ... |
| user output |
|---|
| 59380 59381 59382 59383 59384 ... |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 54386 40792 30938 11372 35684 36569 35851 24804 11443 2433 7140 ... |
| correct output |
|---|
| 15500 15501 15502 15502 15503 ... |
| user output |
|---|
| 15500 15501 15502 15502 15503 ... |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 18637 33277 33188 6803 17956 14954 12901 7331 13934 15280 6584 ... |
| correct output |
|---|
| 560 560 560 561 561 561 561 56... |
| user output |
|---|
| 560 560 560 561 561 ... |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 2637 72943 18160 897 1538 720 703 1919 2314 1803 1644 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 ... |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 87033 99257 40607 9856 32664 94458 61822 19892 ... |
| correct output |
|---|
| 16212 16212 16212 16213 16213 ... |
| user output |
|---|
| 16212 16212 16212 16213 16213 ... |
Test 14
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 4621 68476 54853 81760 28147 87423 33351 48197 ... |
| correct output |
|---|
| 16294 16295 16295 16295 16296 ... |
| user output |
|---|
| 16294 16295 16295 16295 16296 ... |
Test 15
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 51596 84775 65582 47330 87748 35647 53679 16442 ... |
| correct output |
|---|
| 16120 16121 16122 16122 16123 ... |
| user output |
|---|
| 16120 16121 16122 16122 16123 ... |
Test 16
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 69292 26196 54314 63616 95 16072 19077 67875 ... |
| correct output |
|---|
| 16149 16149 16149 16149 16150 ... |
| user output |
|---|
| 16149 16149 16149 16149 16150 ... |
Test 17
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 97541 89807 88261 42118 68387 12007 50630 50 ... |
| correct output |
|---|
| 16105 16105 16105 16105 16106 ... |
| user output |
|---|
| 16105 16105 16105 16105 16106 ... |
Test 18
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 91853 52153 10866 98151 37463 6198 60814 65027 ... |
| correct output |
|---|
| 16154 16155 16155 16155 16156 ... |
| user output |
|---|
| 16154 16155 16155 16155 16156 ... |
Test 19
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 40682 32628 80240 43316 2162 93003 59820 88267 ... |
| correct output |
|---|
| 16398 16398 16398 16398 16398 ... |
| user output |
|---|
| 16398 16398 16398 16398 16398 ... |
Test 20
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 55437 43499 70984 56856 86259 35090 95150 56215 ... |
| correct output |
|---|
| 16044 16045 16045 16045 16046 ... |
| user output |
|---|
| 16044 16045 16045 16045 16046 ... |
Test 21
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 4134 98627 66480 41168 90133 75501 94062 11019 ... |
| correct output |
|---|
| 16251 16252 16252 16252 16253 ... |
| user output |
|---|
| 16251 16252 16252 16252 16253 ... |
Test 22
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 6352 451 19430 47543 18870 69498 10423 18161 ... |
| correct output |
|---|
| 16118 16118 16118 16119 16119 ... |
| user output |
|---|
| 16118 16118 16118 16119 16119 ... |
Test 23
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 47553 72232 19034 64055 24491 39922 98399 16192 ... |
| correct output |
|---|
| 16199 16200 16201 16202 16202 ... |
| user output |
|---|
| 16199 16200 16201 16202 16202 ... |
Test 24
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 69140 89073 95544 9460 45106 70799 50772 94371 ... |
| correct output |
|---|
| 16196 16196 16197 16198 16198 ... |
| user output |
|---|
| 16196 16196 16197 16198 16198 ... |
Test 25
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 100000 22639 28340 10882 29245 74076 54240 87341 51204 ... |
| correct output |
|---|
| 16129 16129 16129 16129 16129 ... |
| user output |
|---|
| 16129 16129 16129 16129 16129 ... |
