답안 #552986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
552986 2022-04-24T11:55:46 Z beedle 경주 (Race) (IOI11_race) C++17
43 / 100
3000 ms 48712 KB
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#define pi 3.141592653589793238
#define ll long long
#define ld long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 998244353ll
#define INF 1000000000000000000
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast                                                                  \
    ios_base::sync_with_stdio (false);                                        \
    cin.tie (NULL);                                                           \
    cout.tie (NULL)
 
using namespace std;

vector <int> minlen(1000000+1,mod);
vector <int> minlenothers(1000000+1,mod);
vector <int> ch;
 
void setup(int v, int p, int width[], vector <pair<int,int>> adj[])
{
    width[v]=1;
    for(auto u:adj[v])
    if(u.ff!=p)
    {
        setup(u.ff,v,width,adj);
        width[v]+=width[u.ff];
    }
}
 
int traverse(int n, int v, int p, int width[],vector <pair<int,int>> adj[])
{
    int outlier=-1;
    for(auto u:adj[v])
    if(u.ff!=p)
    if(width[u.ff]>n/2)
    outlier=u.ff;
 
    if(outlier==-1)
    return v;
    else 
    return traverse(n,outlier,v,width,adj);
}
 
void dfs(int v, int p, int k, int &ans, vector <int> &mydist, int depth,vector <pair<int,int>> adj[])
{
    if(mydist[v]<=k)
    {
        int d=mydist[v];
        minlen[d]=min(minlen[d],depth);
 
        if(d==k)
        ans=min(ans,minlen[d]);
 
        ans=min(ans,depth+minlenothers[k-d]);
 
        ch.push_back(d);
    }
 
    for(auto u:adj[v])
    if(u.ff!=p)
    {
        mydist[u.ff]=mydist[v]+u.ss;
        dfs(u.ff,v,k,ans,mydist, depth+1,adj);
    }
}
 
void find_children(int v, int p, vector <int> &ch,vector <pair<int,int>> adj[])
{
    ch.push_back(v);
    for(auto u:adj[v])
    if(u.ff!=p)
    find_children(u.ff,v,ch,adj);
}
 
int solve(int n, int k, vector <pair<int,int>> adj[])
{
    if(n==1)
    return mod;
    

    int width[n+1];
    setup(1,-1,width,adj);
 
    int v=traverse(n, 1,-1,width,adj);
 
 
    vector <int> mydist(n+1,mod);
    int ans=mod;
    ch.clear();

    vector <int> revert;
 
    for(auto u:adj[v])
    {
        mydist[u.ff]=u.ss;
        dfs(u.ff,v,k,ans,mydist,1,adj);
 
        for(auto x:ch)
        {
            minlenothers[x]=min(minlenothers[x],minlen[x]);
            minlen[x]=mod;
            revert.pb(x);
        }
 
        ch.clear();
    }

    for(auto x:revert)
    minlenothers[x]=mod;

   
    for(auto u:adj[v])
    {
        find_children(u.ff,v,ch,adj);
 
        sort(all(ch));
        ll s=sz(ch);
        vector <int> m(n+1);
        rep(i,0,s-1)
        m[ch[i]]=i+1;
 
        vector <pair<int,int>> nadj[s+1];
 
        rep(i,0,s-1)
        {
            int vt=ch[i];
            for(auto u:adj[vt])
            if(u.ff!=v)
            {
                nadj[m[vt]].pb({m[u.ff],u.ss});
            }
        }

        ch.clear();

        ans=min(ans,solve(s,k,nadj));

    }

 
    return ans;
}
 
int best_path(int n, int k, int h[][2], int l[])
{
    vector <pair<int,int>> adj[n+1];
    rep(i,0,n-2)
    {
        adj[h[i][0]+1].pb({h[i][1]+1,l[i]});
        adj[h[i][1]+1].pb({h[i][0]+1,l[i]});
    }
 
    ll x=solve(n,k,adj);
    return (x==mod?-1:x);
}
 
// signed main()
// {
//     fast;

//     freopen("milkorder.in","r",stdin);
//     // freopen("milkorder.out","w",stdout);

//     ll n,k;
//     cin>>n>>k;

//     int h[n-1][2];
//     int l[n-1];

//     rep(i,0,n-2)
//     {
//         cin>>h[i][0]>>h[i][1]>>l[i];
//     }

//     cout<<best_path(n,k,h,l)<<endl;

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 6 ms 8148 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 6 ms 8160 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8104 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
18 Correct 5 ms 8148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 6 ms 8148 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 6 ms 8160 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8104 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
18 Correct 5 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 6 ms 8140 KB Output is correct
21 Correct 6 ms 8276 KB Output is correct
22 Correct 5 ms 8236 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 6 ms 8276 KB Output is correct
26 Correct 6 ms 8160 KB Output is correct
27 Correct 5 ms 8276 KB Output is correct
28 Correct 6 ms 8276 KB Output is correct
29 Correct 7 ms 8240 KB Output is correct
30 Correct 6 ms 8276 KB Output is correct
31 Correct 5 ms 8228 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8276 KB Output is correct
34 Correct 6 ms 8276 KB Output is correct
35 Correct 6 ms 8292 KB Output is correct
36 Correct 6 ms 8180 KB Output is correct
37 Correct 7 ms 8240 KB Output is correct
38 Correct 5 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 6 ms 8148 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 6 ms 8160 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8104 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
18 Correct 5 ms 8148 KB Output is correct
19 Correct 287 ms 22552 KB Output is correct
20 Correct 276 ms 22876 KB Output is correct
21 Correct 286 ms 23352 KB Output is correct
22 Correct 250 ms 23264 KB Output is correct
23 Correct 309 ms 21928 KB Output is correct
24 Correct 130 ms 18532 KB Output is correct
25 Correct 402 ms 26412 KB Output is correct
26 Correct 266 ms 28760 KB Output is correct
27 Correct 326 ms 25252 KB Output is correct
28 Correct 893 ms 47604 KB Output is correct
29 Correct 904 ms 46748 KB Output is correct
30 Correct 328 ms 25272 KB Output is correct
31 Correct 329 ms 25252 KB Output is correct
32 Correct 383 ms 25216 KB Output is correct
33 Correct 706 ms 36208 KB Output is correct
34 Correct 644 ms 34212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 6 ms 8148 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 6 ms 8160 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8104 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
18 Correct 5 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 6 ms 8140 KB Output is correct
21 Correct 6 ms 8276 KB Output is correct
22 Correct 5 ms 8236 KB Output is correct
23 Correct 5 ms 8276 KB Output is correct
24 Correct 6 ms 8276 KB Output is correct
25 Correct 6 ms 8276 KB Output is correct
26 Correct 6 ms 8160 KB Output is correct
27 Correct 5 ms 8276 KB Output is correct
28 Correct 6 ms 8276 KB Output is correct
29 Correct 7 ms 8240 KB Output is correct
30 Correct 6 ms 8276 KB Output is correct
31 Correct 5 ms 8228 KB Output is correct
32 Correct 6 ms 8276 KB Output is correct
33 Correct 6 ms 8276 KB Output is correct
34 Correct 6 ms 8276 KB Output is correct
35 Correct 6 ms 8292 KB Output is correct
36 Correct 6 ms 8180 KB Output is correct
37 Correct 7 ms 8240 KB Output is correct
38 Correct 5 ms 8276 KB Output is correct
39 Correct 287 ms 22552 KB Output is correct
40 Correct 276 ms 22876 KB Output is correct
41 Correct 286 ms 23352 KB Output is correct
42 Correct 250 ms 23264 KB Output is correct
43 Correct 309 ms 21928 KB Output is correct
44 Correct 130 ms 18532 KB Output is correct
45 Correct 402 ms 26412 KB Output is correct
46 Correct 266 ms 28760 KB Output is correct
47 Correct 326 ms 25252 KB Output is correct
48 Correct 893 ms 47604 KB Output is correct
49 Correct 904 ms 46748 KB Output is correct
50 Correct 328 ms 25272 KB Output is correct
51 Correct 329 ms 25252 KB Output is correct
52 Correct 383 ms 25216 KB Output is correct
53 Correct 706 ms 36208 KB Output is correct
54 Correct 644 ms 34212 KB Output is correct
55 Correct 22 ms 9688 KB Output is correct
56 Correct 25 ms 9680 KB Output is correct
57 Correct 267 ms 22648 KB Output is correct
58 Correct 959 ms 17268 KB Output is correct
59 Correct 276 ms 28776 KB Output is correct
60 Correct 941 ms 48712 KB Output is correct
61 Correct 329 ms 25408 KB Output is correct
62 Correct 353 ms 25864 KB Output is correct
63 Correct 403 ms 25904 KB Output is correct
64 Correct 718 ms 39524 KB Output is correct
65 Correct 540 ms 36524 KB Output is correct
66 Correct 969 ms 44852 KB Output is correct
67 Execution timed out 3058 ms 26300 KB Time limit exceeded
68 Halted 0 ms 0 KB -