답안 #1089052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089052 2024-09-15T20:53:49 Z MrPavlito 경주 (Race) (IOI11_race) C++17
0 / 100
3 ms 5164 KB
#include "race.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define pii pair<int,int>


using namespace std;

const int MAXN = 2e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
int n,k;
long long rez = inf;

vector<vector<pii>> graf(MAXN);
int nmnodes[MAXN];
bool wasCentroid[MAXN];
map<long long,long long> sviputevi;
vector<pair<long long,long long>> trsumsonpath;

int dfs(int nod, int p)
{
    nmnodes[nod] = 1;
    for(auto x: graf[nod])
    {
        if(x.fi==p || wasCentroid[x.fi])continue;
        nmnodes[nod] += dfs(x.fi,nod);
    }
    return nmnodes[nod];
}

void dfs2(int nod, int p, int d, long long s)
{
    if(s>k)return;
    trsumsonpath.pb({s,d});
    for(auto x:graf[nod])
    {
        if(x.fi==p || wasCentroid[x.fi] || s+x.sc > k)continue;
        dfs2(x.fi, nod, d+1, s+x.sc);
    }
}

int findCentroid(int nod, int treesize, int p)
{
    for(auto x: graf[nod])
    {
        if(x.fi == p || wasCentroid[x.fi])continue;
        if(nmnodes[x.fi]*2 > treesize)return findCentroid(x.fi,treesize,nod);
    }
    return nod;
}

void solve(int nod)
{
    int treesize = dfs(nod,nod);
    int centroid = findCentroid(nod, treesize, nod);

    wasCentroid[centroid] = 1;
    sviputevi.clear();
    sviputevi[0] = 0;
    for(auto x: graf[centroid])
    {
        if(wasCentroid[x.fi])continue;
        trsumsonpath.clear();
        dfs2(x.fi, x.fi, 1,x.sc);
        //for(auto y: trsumsonpath)cout << y.fi << " " << y.sc;cout << endl;
        for(auto y:trsumsonpath)
        {
            if(k-y.fi >= 0 && sviputevi.count(k-y.fi))
            {
                rez = min(rez, 0ll+y.sc + sviputevi[k-y.fi]);
            }
        }
        for(auto y:trsumsonpath)
        {
            if(k-y.fi >= 0)
            {
                if(sviputevi.count(y.fi))sviputevi[y.fi] = min(0ll+sviputevi[y.fi], 0ll+y.sc);
                else sviputevi[y.fi] =y.sc;
            }
        }
        //for(auto y:sviputevi)cout << y.fi << " " << y.sc << endl;
    }
    for(auto x: graf[centroid])
    {
        if(wasCentroid[x.fi])continue;
        solve(x.fi);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
    n = N;
    k = K;
    for(int i=0; i<n; i++)
    {
        graf[H[i][0]].pb(mp(H[i][1], L[i]));
        graf[H[i][1]].pb(mp(H[i][0], L[i]));
    }
    solve(0);
    if(rez == inf)return -1;
    return rez;

}

/*
4 7
0 1 1
1 2 2
1 3 4
1
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5164 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Incorrect 3 ms 4952 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5164 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Incorrect 3 ms 4952 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5164 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Incorrect 3 ms 4952 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5164 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Incorrect 3 ms 4952 KB Output isn't correct
14 Halted 0 ms 0 KB -