Submission #519017

#TimeUsernameProblemLanguageResultExecution timeMemory
519017fabijan_cikacRace (IOI11_race)C++17
Compilation error
0 ms0 KiB
 #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, ll> pp;

const int MAXN = 2 * 1e2 + 100;
const int INF = 2 * 1e9;

struct mapa{
    int val = INF;
};

int n; int k;
vector<pp> v[MAXN];
int p[MAXN] = { 0 };
int vel[MAXN] = { 0 };
map<ll, mapa> m;
vector<int> s;
int sol = INF;
int cnt = 0;
int H[MAXN][2];
int L[MAXN];

int dfs(int x, int depth, ll sum, int val, int k){
    p[x] = 1; vel[x] = 0;
    if (val == 1 && sum <= k){
        int zb = depth + m[k - sum].val - 1;
        sol = min(sol, zb);
    }
    else if (val == 2 && sum <= k){
        s.push_back(sum);
        m[sum].val = min(m[sum].val, depth);
    }
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y]){
            int rt = dfs(y, depth + 1, sum + v[x][i].second, val, k);
            if (val == 0)
                vel[x] += rt;
        }
    }
    if (val == 0)
        ++vel[x];
    p[x] = 0;
    if (val == 0)
        return vel[x];
    return 0;
}

int centr(int x){
    p[x] = 1; pp maks = {-1, -1};
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y] && vel[y] > maks.second)
            maks = {y, vel[y]};
    }
    if (maks.second <= cnt / 2){
        p[x] = 0; return x;
    }
    else{
        int rjes = centr(maks.first);
        p[x] = 0; return rjes;
    }
}

void solve(int x, int K){
    s.clear();
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y]){
            dfs(y, 2, v[x][i].second, 1, K); dfs(y, 2, v[x][i].second, 2, K);
        }
    }
    while (!s.empty()){
    	m[s.back()].val = INF; s.pop_back();
	}
	return;
}

void decompose(int x, int K){
    cnt = 0; dfs(x, 1, 0, 0, K); cnt = vel[x];
    int root = centr(x); p[root] = 1;
    solve(root, K);
    for (int i = 0; i < v[root].size(); ++i){
        int y = v[root][i].first;
        if (!p[y]) decompose(y, K);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int N, K; cin >> N >> K;
	for (int i = 0; i < N - 1; ++i)
		cin >> H[i][0] >> H[i][1] >> L[i];
    for (int i = 0; i < N - 1; ++i){
        v[H[i][0]].push_back({H[i][1], L[i]});
        v[H[i][1]].push_back({H[i][0], L[i]});
    }
    decompose(0, K);
    if (sol == INF)
        cout << -1;
    else cout << sol - 1;

    return 0;
}
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, ll> pp;

const int MAXN = 2 * 1e2 + 100;
const int INF = 2 * 1e9;

struct mapa{
    int val = INF;
};

int n; int k;
vector<pp> v[MAXN];
int p[MAXN] = { 0 };
int vel[MAXN] = { 0 };
map<ll, mapa> m;
vector<int> s;
int sol = INF;
int cnt = 0;
int H[MAXN][2];
int L[MAXN];

int dfs(int x, int depth, ll sum, int val, int k){
    p[x] = 1; vel[x] = 0;
    if (val == 1 && sum <= k){
        int zb = depth + m[k - sum].val - 1;
        sol = min(sol, zb);
    }
    else if (val == 2 && sum <= k){
        s.push_back(sum);
        m[sum].val = min(m[sum].val, depth);
    }
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y]){
            int rt = dfs(y, depth + 1, sum + v[x][i].second, val, k);
            if (val == 0)
                vel[x] += rt;
        }
    }
    if (val == 0)
        ++vel[x];
    p[x] = 0;
    if (val == 0)
        return vel[x];
    return 0;
}

int centr(int x){
    p[x] = 1; pp maks = {-1, -1};
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y] && vel[y] > maks.second)
            maks = {y, vel[y]};
    }
    if (maks.second <= cnt / 2){
        p[x] = 0; return x;
    }
    else{
        int rjes = centr(maks.first);
        p[x] = 0; return rjes;
    }
}

void solve(int x, int K){
    s.clear();
    for (int i = 0; i < v[x].size(); ++i){
        int y = v[x][i].first;
        if (!p[y]){
            dfs(y, 2, v[x][i].second, 1, K); dfs(y, 2, v[x][i].second, 2, K);
        }
    }
    while (!s.empty()){
    	m[s.back()].val = INF; s.pop_back();
	}
	return;
}

void decompose(int x, int K){
    cnt = 0; dfs(x, 1, 0, 0, K); cnt = vel[x];
    int root = centr(x); p[root] = 1;
    solve(root, K);
    for (int i = 0; i < v[root].size(); ++i){
        int y = v[root][i].first;
        if (!p[y]) decompose(y, K);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int N, K; cin >> N >> K;
	for (int i = 0; i < N - 1; ++i)
		cin >> H[i][0] >> H[i][1] >> L[i];
    for (int i = 0; i < N - 1; ++i){
        v[H[i][0]].push_back({H[i][1], L[i]});
        v[H[i][1]].push_back({H[i][0], L[i]});
    }
    decompose(0, K);
    if (sol == INF)
        cout << -1;
    else cout << sol - 1;

    return 0;
}

Compilation message (stderr)

race.cpp: In function 'int dfs(int, int, ll, int, int)':
race.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: In function 'int centr(int)':
race.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int, int)':
race.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 0; i < v[root].size(); ++i){
      |                     ~~^~~~~~~~~~~~~~~~
race.cpp: At global scope:
race.cpp:117:11: error: redefinition of 'const int MAXN'
  117 | const int MAXN = 2 * 1e2 + 100;
      |           ^~~~
race.cpp:8:11: note: 'const int MAXN' previously defined here
    8 | const int MAXN = 2 * 1e2 + 100;
      |           ^~~~
race.cpp:118:11: error: redefinition of 'const int INF'
  118 | const int INF = 2 * 1e9;
      |           ^~~
race.cpp:9:11: note: 'const int INF' previously defined here
    9 | const int INF = 2 * 1e9;
      |           ^~~
race.cpp:120:8: error: redefinition of 'struct mapa'
  120 | struct mapa{
      |        ^~~~
race.cpp:11:8: note: previous definition of 'struct mapa'
   11 | struct mapa{
      |        ^~~~
race.cpp:124:5: error: redefinition of 'int n'
  124 | int n; int k;
      |     ^
race.cpp:15:5: note: 'int n' previously declared here
   15 | int n; int k;
      |     ^
race.cpp:124:12: error: redefinition of 'int k'
  124 | int n; int k;
      |            ^
race.cpp:15:12: note: 'int k' previously declared here
   15 | int n; int k;
      |            ^
race.cpp:125:12: error: redefinition of 'std::vector<std::pair<int, long long int> > v [300]'
  125 | vector<pp> v[MAXN];
      |            ^
race.cpp:16:12: note: 'std::vector<std::pair<int, long long int> > v [300]' previously declared here
   16 | vector<pp> v[MAXN];
      |            ^
race.cpp:126:5: error: redefinition of 'int p [300]'
  126 | int p[MAXN] = { 0 };
      |     ^
race.cpp:17:5: note: 'int p [300]' previously defined here
   17 | int p[MAXN] = { 0 };
      |     ^
race.cpp:127:5: error: redefinition of 'int vel [300]'
  127 | int vel[MAXN] = { 0 };
      |     ^~~
race.cpp:18:5: note: 'int vel [300]' previously defined here
   18 | int vel[MAXN] = { 0 };
      |     ^~~
race.cpp:128:15: error: redefinition of 'std::map<long long int, mapa> m'
  128 | map<ll, mapa> m;
      |               ^
race.cpp:19:15: note: 'std::map<long long int, mapa> m' previously declared here
   19 | map<ll, mapa> m;
      |               ^
race.cpp:129:13: error: redefinition of 'std::vector<int> s'
  129 | vector<int> s;
      |             ^
race.cpp:20:13: note: 'std::vector<int> s' previously declared here
   20 | vector<int> s;
      |             ^
race.cpp:130:5: error: redefinition of 'int sol'
  130 | int sol = INF;
      |     ^~~
race.cpp:21:5: note: 'int sol' previously defined here
   21 | int sol = INF;
      |     ^~~
race.cpp:131:5: error: redefinition of 'int cnt'
  131 | int cnt = 0;
      |     ^~~
race.cpp:22:5: note: 'int cnt' previously defined here
   22 | int cnt = 0;
      |     ^~~
race.cpp:132:5: error: redefinition of 'int H [300][2]'
  132 | int H[MAXN][2];
      |     ^
race.cpp:23:5: note: 'int H [300][2]' previously declared here
   23 | int H[MAXN][2];
      |     ^
race.cpp:133:5: error: redefinition of 'int L [300]'
  133 | int L[MAXN];
      |     ^
race.cpp:24:5: note: 'int L [300]' previously declared here
   24 | int L[MAXN];
      |     ^
race.cpp:135:5: error: redefinition of 'int dfs(int, int, ll, int, int)'
  135 | int dfs(int x, int depth, ll sum, int val, int k){
      |     ^~~
race.cpp:26:5: note: 'int dfs(int, int, ll, int, int)' previously defined here
   26 | int dfs(int x, int depth, ll sum, int val, int k){
      |     ^~~
race.cpp: In function 'int dfs(int, int, ll, int, int)':
race.cpp:145:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: At global scope:
race.cpp:161:5: error: redefinition of 'int centr(int)'
  161 | int centr(int x){
      |     ^~~~~
race.cpp:52:5: note: 'int centr(int)' previously defined here
   52 | int centr(int x){
      |     ^~~~~
race.cpp: In function 'int centr(int)':
race.cpp:163:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  163 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: At global scope:
race.cpp:177:6: error: redefinition of 'void solve(int, int)'
  177 | void solve(int x, int K){
      |      ^~~~~
race.cpp:68:6: note: 'void solve(int, int)' previously defined here
   68 | void solve(int x, int K){
      |      ^~~~~
race.cpp: In function 'void solve(int, int)':
race.cpp:179:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |     for (int i = 0; i < v[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~
race.cpp: At global scope:
race.cpp:191:6: error: redefinition of 'void decompose(int, int)'
  191 | void decompose(int x, int K){
      |      ^~~~~~~~~
race.cpp:82:6: note: 'void decompose(int, int)' previously defined here
   82 | void decompose(int x, int K){
      |      ^~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:195:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |     for (int i = 0; i < v[root].size(); ++i){
      |                     ~~^~~~~~~~~~~~~~~~
race.cpp: At global scope:
race.cpp:201:5: error: redefinition of 'int main()'
  201 | int main(){
      |     ^~~~
race.cpp:92:5: note: 'int main()' previously defined here
   92 | int main(){
      |     ^~~~