답안 #305689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
305689 2020-09-23T19:36:43 Z MatijaL 기지국 (IOI20_stations) C++14
100 / 100
1105 ms 1408 KB
/**
 * Author: MatijaL
 * Time: 2020-09-23 18:43:46
**/

#include <stations.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pll pair<ll, ll>
#define loop(i, n) for(ll i = 0; i < n; i++)
#define FOR(i,n,m) for(ll i = n; i <= m; i++)
#define isIn(vec, item) find(vec.begin(), vec.end(), item) != vec.end()
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define inf 1000000005
#define mod 1000000007
const int limit = 2000;
vector<int> sosedi[limit];
int in[limit];
int out[limit];
int depth[limit];
int cas = 1;


//cas se incrementa za vsako povezavo v vsako smer!!
void dfs(int node, int prev, int dpt){
    in[node] = cas;
    depth[node] = dpt;
    for(auto sosed : sosedi[node]){
        if(sosed != prev){
            cas++;
            dfs(sosed, node, dpt+1);
        }
    }
    
    cas++;
    out[node] = cas;
}

vector<int> label(int n, int k, vector<int> u, vector<int> v){
    //reset
    cas = 1;
    loop(i, limit) sosedi[i].clear();
    memset(in, 0, sizeof in);
    memset(out, 0, sizeof out);
    memset(depth, 0, sizeof depth);

    loop(i, n-1){
        int a = u[i];
        int b = v[i];
        sosedi[a].pb(b);
        sosedi[b].pb(a);
    }
    dfs(0, 0, 0);
    map<int, int> fi;
    vector<int> indeces;

    loop(i, n){
        if(depth[i] % 2 == 0){
            indeces.pb(in[i]);
            fi[in[i]] = i;
        }else{
            indeces.pb(out[i]);
            fi[out[i]] = i;
        }
    }
    //compression
    sort(all(indeces));
    vector<int> ans(n);
    
    int i = 0;
    for(auto index : indeces){
        int node = fi[index];
        ans[node] = i;
        i++;
    }
    return ans;
}
int find_next_station(int cur, int goal, vector<int> adjacent){

    if(cur == goal) return cur;
    for(auto e : adjacent) if(e == goal) return e;
    
    int mn = inf;
    int mx = -1;
    for(auto e : adjacent){
        mn = min(mn, e);
        mx = max(mx, e);
    }

    //ugotovi depth
    int depth;
    if(adjacent[0] > cur) depth = 0;
    else depth = 1;
    sort(all(adjacent));
    if(depth == 0){
        if(cur == 0){
            return *upper_bound(all(adjacent), goal); 
        }
        //cur je in poddrevesa
        //parent je mx
        int parent = mx;
        if(adjacent.size() == 1) return parent;

        reverse(all(adjacent));
        int maxChild = adjacent[1];

        reverse(all(adjacent));
        if(goal < cur or goal > maxChild) return parent;
        else return *upper_bound(all(adjacent), goal);  
    }
    else{
        //cur je out poddrevesa 
        int parent = mn;
        if(adjacent.size() == 1) return parent;

        int minChild = adjacent[1];
        if(goal < parent or goal > cur) return parent;
        return *(upper_bound(all(adjacent), goal) - 1);
    }

}

Compilation message

stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:121:13: warning: unused variable 'minChild' [-Wunused-variable]
  121 |         int minChild = adjacent[1];
      |             ^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 626 ms 1024 KB Output is correct
2 Correct 492 ms 1060 KB Output is correct
3 Correct 895 ms 896 KB Output is correct
4 Correct 754 ms 896 KB Output is correct
5 Correct 694 ms 952 KB Output is correct
6 Correct 537 ms 1064 KB Output is correct
7 Correct 490 ms 1072 KB Output is correct
8 Correct 3 ms 768 KB Output is correct
9 Correct 4 ms 956 KB Output is correct
10 Correct 2 ms 964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 523 ms 1024 KB Output is correct
2 Correct 573 ms 1152 KB Output is correct
3 Correct 1045 ms 1160 KB Output is correct
4 Correct 848 ms 896 KB Output is correct
5 Correct 626 ms 968 KB Output is correct
6 Correct 446 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 555 ms 1024 KB Output is correct
2 Correct 469 ms 1052 KB Output is correct
3 Correct 1099 ms 896 KB Output is correct
4 Correct 724 ms 960 KB Output is correct
5 Correct 616 ms 1024 KB Output is correct
6 Correct 471 ms 1016 KB Output is correct
7 Correct 512 ms 1096 KB Output is correct
8 Correct 3 ms 880 KB Output is correct
9 Correct 5 ms 768 KB Output is correct
10 Correct 1 ms 960 KB Output is correct
11 Correct 713 ms 768 KB Output is correct
12 Correct 530 ms 1052 KB Output is correct
13 Correct 554 ms 1400 KB Output is correct
14 Correct 470 ms 1072 KB Output is correct
15 Correct 55 ms 896 KB Output is correct
16 Correct 85 ms 896 KB Output is correct
17 Correct 109 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 886 ms 848 KB Output is correct
2 Correct 852 ms 896 KB Output is correct
3 Correct 685 ms 768 KB Output is correct
4 Correct 3 ms 960 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 1 ms 896 KB Output is correct
7 Correct 594 ms 832 KB Output is correct
8 Correct 951 ms 960 KB Output is correct
9 Correct 706 ms 896 KB Output is correct
10 Correct 600 ms 960 KB Output is correct
11 Correct 5 ms 896 KB Output is correct
12 Correct 5 ms 960 KB Output is correct
13 Correct 6 ms 768 KB Output is correct
14 Correct 5 ms 960 KB Output is correct
15 Correct 2 ms 1088 KB Output is correct
16 Correct 506 ms 1168 KB Output is correct
17 Correct 581 ms 964 KB Output is correct
18 Correct 554 ms 960 KB Output is correct
19 Correct 551 ms 960 KB Output is correct
20 Correct 631 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 673 ms 1400 KB Output is correct
2 Correct 546 ms 1024 KB Output is correct
3 Correct 1093 ms 896 KB Output is correct
4 Correct 796 ms 960 KB Output is correct
5 Correct 723 ms 964 KB Output is correct
6 Correct 496 ms 1056 KB Output is correct
7 Correct 555 ms 1088 KB Output is correct
8 Correct 3 ms 984 KB Output is correct
9 Correct 4 ms 1152 KB Output is correct
10 Correct 2 ms 972 KB Output is correct
11 Correct 457 ms 1112 KB Output is correct
12 Correct 643 ms 1264 KB Output is correct
13 Correct 1105 ms 972 KB Output is correct
14 Correct 827 ms 896 KB Output is correct
15 Correct 789 ms 768 KB Output is correct
16 Correct 446 ms 1280 KB Output is correct
17 Correct 572 ms 896 KB Output is correct
18 Correct 520 ms 1080 KB Output is correct
19 Correct 560 ms 1144 KB Output is correct
20 Correct 487 ms 1120 KB Output is correct
21 Correct 60 ms 780 KB Output is correct
22 Correct 91 ms 896 KB Output is correct
23 Correct 124 ms 1112 KB Output is correct
24 Correct 6 ms 960 KB Output is correct
25 Correct 6 ms 768 KB Output is correct
26 Correct 5 ms 896 KB Output is correct
27 Correct 4 ms 1088 KB Output is correct
28 Correct 2 ms 776 KB Output is correct
29 Correct 561 ms 956 KB Output is correct
30 Correct 507 ms 956 KB Output is correct
31 Correct 556 ms 964 KB Output is correct
32 Correct 524 ms 956 KB Output is correct
33 Correct 615 ms 972 KB Output is correct
34 Correct 333 ms 1024 KB Output is correct
35 Correct 481 ms 1044 KB Output is correct
36 Correct 509 ms 1400 KB Output is correct
37 Correct 608 ms 1088 KB Output is correct
38 Correct 599 ms 1032 KB Output is correct
39 Correct 572 ms 1080 KB Output is correct
40 Correct 455 ms 1076 KB Output is correct
41 Correct 459 ms 1408 KB Output is correct
42 Correct 66 ms 1140 KB Output is correct
43 Correct 119 ms 1024 KB Output is correct
44 Correct 135 ms 1024 KB Output is correct
45 Correct 180 ms 1024 KB Output is correct
46 Correct 330 ms 1128 KB Output is correct
47 Correct 385 ms 1092 KB Output is correct
48 Correct 85 ms 1024 KB Output is correct
49 Correct 83 ms 1072 KB Output is correct