Submission #29254

# Submission time Handle Problem Language Result Execution time Memory
29254 2017-07-18T20:14:34 Z ozaslan Race (IOI11_race) C++14
100 / 100
1363 ms 45144 KB
#include<bits/stdc++.h>
#include "race.h"
#define MAX_N 500000
#define oo (1<<28)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

using namespace std;

/*static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;

inline
void my_assert(int e) {if (!e) abort();}

void read_input()
{
  int i;
  my_assert(2==scanf("%d %d",&N,&K));
  for(i=0; i<N-1; i++)
    my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
  my_assert(1==scanf("%d",&solution));
}

int main()
{
    freopen("grader.in.3", "r", stdin);
  int ans;
  read_input();
  ans = best_path(N,K,H,L);
  if(ans==solution)
    printf("Correct.\n");
  else
    printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);

  return 0;
}*/

int enIyi = oo;
int kul[MAX_N], cocukSayisi[MAX_N], hedef;
map<int, int> uzak;
vector< pair<int, int> > E[MAX_N];
int uzaklik[MAX_N], derin[MAX_N];

int merkezDFS(int dugum, int ata) {
   cocukSayisi[dugum] = 0;

	int s = E[dugum].size();
	for (int i = 0; i < s; i++) {
		int v = E[dugum][i].first;

		if(v != ata && !kul[v])
			cocukSayisi[dugum] += merkezDFS(v, dugum);
	}
   return cocukSayisi[dugum] +1;
}

int merkezBul(int bas) {
	int uyeSayisi = merkezDFS(bas, -1);
    if (uyeSayisi == 1) return -1;

    int ata = -1;
    int dugum = bas;
 	while (1) {
 		bool bayrak = 1;

		int s = E[dugum].size();
		for (int i = 0; i < s; i++) {
            int v = E[dugum][i].first;
            int cocuk = cocukSayisi[v]+1;

            if(kul[v]) continue;
            if (v == ata) cocuk -= (cocukSayisi[dugum]+1);

			if (cocuk > uyeSayisi/2) {
				bayrak = 0;
				ata = dugum;
				dugum = v;
				break;
            }
		}
		if (bayrak) return dugum;
  }

}

int DFS(int dugum, int mesafe, int derinlik, int ata, int sayac) {
   // printf("Dugum: %d\n", dugum);
    if (uzak.find(hedef-mesafe) != uzak.end()) {
        enIyi = min(enIyi, derinlik + uzak[hedef-mesafe]);
     //   printf("derinlik: %d, mesafe:%d, dugum:%d\n", derinlik, mesafe, dugum);
    }
    uzaklik[sayac] = mesafe;
    derin[sayac] = derinlik;

    int s = E[dugum].size();
    for (int i = 0; i < s; i++) {
        int v = E[dugum][i].first, m = E[dugum][i].second;
        if (v == ata || kul[v]) continue;

        sayac = DFS(v, mesafe +m, derinlik+1, dugum, sayac+1);
    }

    return sayac;
}

void centeroid(int bas) {
    uzak.clear();
    uzak[0] = 0;

	int merkez = merkezBul(bas);
    if(merkez == -1) {kul[bas] = 1; return;}

//    printf("Merkez: %d \n", merkez);

    int s = E[merkez].size();
    for (int i = 0; i < s; i++) {
        int v = E[merkez][i].first, m = E[merkez][i].second;
  //      printf("M: %d, v: %d", merkez, v);
        if(kul[v]) continue;
        int sayac = DFS(v, m, 1, merkez, 0);

    //    printf("\n");
        for(int j=0; j<=sayac; j++) {
            if(uzak.find(uzaklik[j]) == uzak.end()) uzak[uzaklik[j]] = derin[j];
            else uzak[uzaklik[j]] = min(uzak[uzaklik[j]], derin[j]);
        }
    }
  //  printf("Kontrol\n");
    kul[merkez] = 1;

    for (int i = 0; i < s; i++)
        if(!kul[E[merkez][i].first])
            centeroid(E[merkez][i].first);

}

int best_path(int N, int K, int H[][2], int L[])
{
    hedef = K;

	for (int i = 0; i < N-1; i++) {
    //    cout << H[i][0] << " " << H[i][1] << endl;
		E[ H[i][0] ].push_back( make_pair( H[i][1], L[i]) );
		E[ H[i][1] ].push_back( make_pair( H[i][0], L[i]) );
	}

	centeroid(0);

    if(enIyi == oo) enIyi = -1;
	return enIyi;
}

# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 13 ms 12132 KB Output is correct
3 Correct 10 ms 12184 KB Output is correct
4 Correct 11 ms 12260 KB Output is correct
5 Correct 11 ms 12260 KB Output is correct
6 Correct 11 ms 12260 KB Output is correct
7 Correct 11 ms 12312 KB Output is correct
8 Correct 11 ms 12440 KB Output is correct
9 Correct 11 ms 12440 KB Output is correct
10 Correct 12 ms 12440 KB Output is correct
11 Correct 11 ms 12440 KB Output is correct
12 Correct 11 ms 12440 KB Output is correct
13 Correct 11 ms 12440 KB Output is correct
14 Correct 11 ms 12440 KB Output is correct
15 Correct 11 ms 12440 KB Output is correct
16 Correct 11 ms 12440 KB Output is correct
17 Correct 12 ms 12440 KB Output is correct
18 Correct 10 ms 12440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 13 ms 12132 KB Output is correct
3 Correct 10 ms 12184 KB Output is correct
4 Correct 11 ms 12260 KB Output is correct
5 Correct 11 ms 12260 KB Output is correct
6 Correct 11 ms 12260 KB Output is correct
7 Correct 11 ms 12312 KB Output is correct
8 Correct 11 ms 12440 KB Output is correct
9 Correct 11 ms 12440 KB Output is correct
10 Correct 12 ms 12440 KB Output is correct
11 Correct 11 ms 12440 KB Output is correct
12 Correct 11 ms 12440 KB Output is correct
13 Correct 11 ms 12440 KB Output is correct
14 Correct 11 ms 12440 KB Output is correct
15 Correct 11 ms 12440 KB Output is correct
16 Correct 11 ms 12440 KB Output is correct
17 Correct 12 ms 12440 KB Output is correct
18 Correct 10 ms 12440 KB Output is correct
19 Correct 11 ms 12440 KB Output is correct
20 Correct 13 ms 12440 KB Output is correct
21 Correct 12 ms 12440 KB Output is correct
22 Correct 13 ms 12440 KB Output is correct
23 Correct 13 ms 12440 KB Output is correct
24 Correct 12 ms 12440 KB Output is correct
25 Correct 13 ms 12440 KB Output is correct
26 Correct 15 ms 12440 KB Output is correct
27 Correct 11 ms 12440 KB Output is correct
28 Correct 12 ms 12440 KB Output is correct
29 Correct 14 ms 12440 KB Output is correct
30 Correct 13 ms 12484 KB Output is correct
31 Correct 12 ms 12484 KB Output is correct
32 Correct 13 ms 12484 KB Output is correct
33 Correct 12 ms 12484 KB Output is correct
34 Correct 13 ms 12484 KB Output is correct
35 Correct 12 ms 12484 KB Output is correct
36 Correct 14 ms 12484 KB Output is correct
37 Correct 12 ms 12484 KB Output is correct
38 Correct 13 ms 12484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 13 ms 12132 KB Output is correct
3 Correct 10 ms 12184 KB Output is correct
4 Correct 11 ms 12260 KB Output is correct
5 Correct 11 ms 12260 KB Output is correct
6 Correct 11 ms 12260 KB Output is correct
7 Correct 11 ms 12312 KB Output is correct
8 Correct 11 ms 12440 KB Output is correct
9 Correct 11 ms 12440 KB Output is correct
10 Correct 12 ms 12440 KB Output is correct
11 Correct 11 ms 12440 KB Output is correct
12 Correct 11 ms 12440 KB Output is correct
13 Correct 11 ms 12440 KB Output is correct
14 Correct 11 ms 12440 KB Output is correct
15 Correct 11 ms 12440 KB Output is correct
16 Correct 11 ms 12440 KB Output is correct
17 Correct 12 ms 12440 KB Output is correct
18 Correct 10 ms 12440 KB Output is correct
19 Correct 309 ms 18428 KB Output is correct
20 Correct 339 ms 18428 KB Output is correct
21 Correct 371 ms 18492 KB Output is correct
22 Correct 271 ms 18544 KB Output is correct
23 Correct 418 ms 18616 KB Output is correct
24 Correct 221 ms 18616 KB Output is correct
25 Correct 304 ms 23676 KB Output is correct
26 Correct 131 ms 24024 KB Output is correct
27 Correct 461 ms 24028 KB Output is correct
28 Correct 1264 ms 45144 KB Output is correct
29 Correct 1266 ms 45144 KB Output is correct
30 Correct 428 ms 45144 KB Output is correct
31 Correct 449 ms 45144 KB Output is correct
32 Correct 487 ms 45144 KB Output is correct
33 Correct 716 ms 45144 KB Output is correct
34 Correct 976 ms 45144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12024 KB Output is correct
2 Correct 13 ms 12132 KB Output is correct
3 Correct 10 ms 12184 KB Output is correct
4 Correct 11 ms 12260 KB Output is correct
5 Correct 11 ms 12260 KB Output is correct
6 Correct 11 ms 12260 KB Output is correct
7 Correct 11 ms 12312 KB Output is correct
8 Correct 11 ms 12440 KB Output is correct
9 Correct 11 ms 12440 KB Output is correct
10 Correct 12 ms 12440 KB Output is correct
11 Correct 11 ms 12440 KB Output is correct
12 Correct 11 ms 12440 KB Output is correct
13 Correct 11 ms 12440 KB Output is correct
14 Correct 11 ms 12440 KB Output is correct
15 Correct 11 ms 12440 KB Output is correct
16 Correct 11 ms 12440 KB Output is correct
17 Correct 12 ms 12440 KB Output is correct
18 Correct 10 ms 12440 KB Output is correct
19 Correct 11 ms 12440 KB Output is correct
20 Correct 13 ms 12440 KB Output is correct
21 Correct 12 ms 12440 KB Output is correct
22 Correct 13 ms 12440 KB Output is correct
23 Correct 13 ms 12440 KB Output is correct
24 Correct 12 ms 12440 KB Output is correct
25 Correct 13 ms 12440 KB Output is correct
26 Correct 15 ms 12440 KB Output is correct
27 Correct 11 ms 12440 KB Output is correct
28 Correct 12 ms 12440 KB Output is correct
29 Correct 14 ms 12440 KB Output is correct
30 Correct 13 ms 12484 KB Output is correct
31 Correct 12 ms 12484 KB Output is correct
32 Correct 13 ms 12484 KB Output is correct
33 Correct 12 ms 12484 KB Output is correct
34 Correct 13 ms 12484 KB Output is correct
35 Correct 12 ms 12484 KB Output is correct
36 Correct 14 ms 12484 KB Output is correct
37 Correct 12 ms 12484 KB Output is correct
38 Correct 13 ms 12484 KB Output is correct
39 Correct 309 ms 18428 KB Output is correct
40 Correct 339 ms 18428 KB Output is correct
41 Correct 371 ms 18492 KB Output is correct
42 Correct 271 ms 18544 KB Output is correct
43 Correct 418 ms 18616 KB Output is correct
44 Correct 221 ms 18616 KB Output is correct
45 Correct 304 ms 23676 KB Output is correct
46 Correct 131 ms 24024 KB Output is correct
47 Correct 461 ms 24028 KB Output is correct
48 Correct 1264 ms 45144 KB Output is correct
49 Correct 1266 ms 45144 KB Output is correct
50 Correct 428 ms 45144 KB Output is correct
51 Correct 449 ms 45144 KB Output is correct
52 Correct 487 ms 45144 KB Output is correct
53 Correct 716 ms 45144 KB Output is correct
54 Correct 976 ms 45144 KB Output is correct
55 Correct 36 ms 45144 KB Output is correct
56 Correct 26 ms 45144 KB Output is correct
57 Correct 164 ms 45144 KB Output is correct
58 Correct 67 ms 45144 KB Output is correct
59 Correct 388 ms 45144 KB Output is correct
60 Correct 1125 ms 45144 KB Output is correct
61 Correct 416 ms 45144 KB Output is correct
62 Correct 417 ms 45144 KB Output is correct
63 Correct 446 ms 45144 KB Output is correct
64 Correct 1109 ms 45144 KB Output is correct
65 Correct 1148 ms 45144 KB Output is correct
66 Correct 1363 ms 45144 KB Output is correct
67 Correct 252 ms 45144 KB Output is correct
68 Correct 811 ms 45144 KB Output is correct
69 Correct 724 ms 45144 KB Output is correct
70 Correct 683 ms 45144 KB Output is correct