답안 #943507

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943507 2024-03-11T14:35:40 Z Zero_OP Railway (BOI17_railway) C++14
100 / 100
70 ms 22976 KB
#include<bits/stdc++.h>

using namespace std;
using int64 = int64_t;

#define     REP(i, n) for(int i = 0, _n = n; i < _n; ++i)
#define    REPD(i, n) for(int i = n - 1; i >= 0; --i)
#define  FOR(i, l, r) for(int i = l, _r = r; i <= _r; ++i)
#define FORD(i, r, l) for(int i = r, _l = l; i >= _l; --i)
#define          left __left
#define         right __right
#define          prev __prev
#define          next __next
#define           div __div
#define            pb push_back
#define            pf push_front
#define         sz(v) (int)v.size()
#define      range(v) begin(v), end(v)
#define    compact(v) v.erase(unique(range(v)), end(v))
#define      debug(v) "[" #v " = " << (v) << "]"

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            a = b;
            return true;
        }
        return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            a = b;
            return true;
        }
        return false;
    }

template<int dimension, class T>
struct vec : public vector<vec<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    vec(int n = 0, Args... args) : vector<vec<dimension - 1, T>>(n, vec<dimension - 1, T>(args...)) {}
};

template<class T>
struct vec<1, T> : public vector<T> {
    vec(int n = 0, T val = T()) : vector<T>(n, val) {}
};

void init(void);
void process(void);

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

    #define task "antuvu"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    int T = 1; //cin >> T;
    while(T--) {
        init();
        process();
    }

    return 0;
}

const int MAX = 1e5 + 5;

int n, m, k, timer_dfs;
int tour[MAX], cnt[MAX], eu[MAX], ev[MAX], depth[MAX], par[MAX], head[MAX], pos[MAX], heavy[MAX], count_children[MAX], diff[MAX], edge_indices[MAX];
vector<int> adj[MAX];

void init(){
    cin >> n >> m >> k;
    REP(i, n - 1){
        int u, v;
        cin >> u >> v;
        adj[u].pb(i);
        adj[v].pb(i);
        eu[i] = u, ev[i] = v;
    }
}

void pre_dfs(int u, int pre){
    count_children[u] = 1;
    for(int id : adj[u]) if(id != pre){
        int v = eu[id] ^ ev[id] ^ u;
        depth[v] = depth[u] + 1;
        par[v] = u;
        edge_indices[v] = id;
        pre_dfs(v, id);
        count_children[u] += count_children[v];
        if(count_children[heavy[u]] < count_children[v]){
            heavy[u] = v;
        }
    }
}

void decompose(int u, int c){
    head[u] = c;
    tour[pos[u] = ++timer_dfs] = u;

    if(heavy[u]){
        decompose(heavy[u], c);
        for(int id : adj[u]){
            int v = eu[id] ^ ev[id] ^ u;
            if(par[v] == u and v != heavy[u]){
                decompose(v, v);
            }
        }
    }
}

int getLCA(int u, int v){
    while(head[u] != head[v]){
        if(depth[head[u]] < depth[head[v]]){
            v = par[head[v]];
        }
        else{
            u = par[head[u]];
        }
    }
    if(depth[u] < depth[v]) return u;
    else return v;
}

void add_range_sum(int l, int r, int v){
    diff[l] += v;
    diff[r + 1] -= v;
}

void add_sum(int u, int v){
    while(head[u] != head[v]){
        if(depth[head[u]] < depth[head[v]]) swap(u, v);
        add_range_sum(pos[head[u]], pos[u], 1);
        u = par[head[u]];
    }

    if(u != v){
        if(depth[u] > depth[v]) swap(u, v);
        add_range_sum(pos[u] + 1, pos[v], 1);
    }
}

bool is_parent(int u, int v){
    return pos[u] <= pos[v] and pos[v] <= pos[u] + count_children[u] - 1;
}

void solve_difference(){
    FOR(i, 2, n){
        diff[i] += diff[i - 1];
        cnt[edge_indices[tour[i]]] = diff[i];
    }
}

void process(){
    pre_dfs(1, -1);
    decompose(1, 1);

    while(m--){
        int s;
        cin >> s;
        vector<int> ar(s);
        REP(i, s) cin >> ar[i];

        sort(range(ar), [&](int a, int b){
            return pos[a] < pos[b];
        });

        REP(i, s - 1){
            ar.pb(getLCA(ar[i], ar[i + 1]));
        }

        sort(range(ar), [&](int a, int b){
            return pos[a] < pos[b];
        });

        compact(ar);

        stack<int> stack_vertices;
        stack_vertices.push(ar[0]);
        FOR(i, 1, sz(ar) - 1){
            while(!stack_vertices.empty() and !is_parent(stack_vertices.top(), ar[i])){
                stack_vertices.pop();
            }

            add_sum(stack_vertices.top(), ar[i]);
            stack_vertices.push(ar[i]);
        }
    }

    solve_difference();

    vector<int> res;
    REP(i, n - 1){
        if(cnt[i] >= k){
            res.pb(i);
        }
    }

    cout << sz(res) << '\n';
    for(int i : res) cout << i + 1 << ' ';
}

Compilation message

railway.cpp: In function 'int main()':
railway.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 4 ms 7260 KB Output is correct
3 Correct 4 ms 7260 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 6 ms 7768 KB Output is correct
7 Correct 4 ms 7260 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7256 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6780 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 4 ms 7260 KB Output is correct
3 Correct 4 ms 7260 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 6 ms 7768 KB Output is correct
7 Correct 4 ms 7260 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7256 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6780 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6792 KB Output is correct
15 Correct 19 ms 7512 KB Output is correct
16 Correct 19 ms 7768 KB Output is correct
17 Correct 19 ms 7480 KB Output is correct
18 Correct 4 ms 7772 KB Output is correct
19 Correct 4 ms 7260 KB Output is correct
20 Correct 17 ms 7816 KB Output is correct
21 Correct 14 ms 7516 KB Output is correct
22 Correct 1 ms 6748 KB Output is correct
23 Correct 4 ms 7236 KB Output is correct
24 Correct 4 ms 7256 KB Output is correct
25 Correct 1 ms 6748 KB Output is correct
26 Correct 1 ms 6744 KB Output is correct
27 Correct 4 ms 7772 KB Output is correct
28 Correct 4 ms 7256 KB Output is correct
29 Correct 3 ms 7260 KB Output is correct
30 Correct 3 ms 7052 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 1 ms 6748 KB Output is correct
33 Correct 2 ms 6748 KB Output is correct
34 Correct 1 ms 6748 KB Output is correct
35 Correct 1 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 20780 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 45 ms 20576 KB Output is correct
4 Correct 41 ms 20004 KB Output is correct
5 Correct 46 ms 21448 KB Output is correct
6 Correct 55 ms 21292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 15184 KB Output is correct
2 Correct 46 ms 12884 KB Output is correct
3 Correct 47 ms 12368 KB Output is correct
4 Correct 47 ms 12372 KB Output is correct
5 Correct 53 ms 12576 KB Output is correct
6 Correct 43 ms 17148 KB Output is correct
7 Correct 44 ms 17188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 15184 KB Output is correct
2 Correct 46 ms 12884 KB Output is correct
3 Correct 47 ms 12368 KB Output is correct
4 Correct 47 ms 12372 KB Output is correct
5 Correct 53 ms 12576 KB Output is correct
6 Correct 43 ms 17148 KB Output is correct
7 Correct 44 ms 17188 KB Output is correct
8 Correct 49 ms 17236 KB Output is correct
9 Correct 46 ms 17088 KB Output is correct
10 Correct 51 ms 22740 KB Output is correct
11 Correct 44 ms 22664 KB Output is correct
12 Correct 33 ms 12368 KB Output is correct
13 Correct 35 ms 12600 KB Output is correct
14 Correct 46 ms 12320 KB Output is correct
15 Correct 44 ms 12372 KB Output is correct
16 Correct 67 ms 12308 KB Output is correct
17 Correct 48 ms 12372 KB Output is correct
18 Correct 46 ms 12368 KB Output is correct
19 Correct 47 ms 12768 KB Output is correct
20 Correct 58 ms 17692 KB Output is correct
21 Correct 54 ms 17620 KB Output is correct
22 Correct 50 ms 17416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 4 ms 7260 KB Output is correct
3 Correct 4 ms 7260 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 6 ms 7768 KB Output is correct
7 Correct 4 ms 7260 KB Output is correct
8 Correct 3 ms 7260 KB Output is correct
9 Correct 3 ms 7256 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 1 ms 6780 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6792 KB Output is correct
15 Correct 19 ms 7512 KB Output is correct
16 Correct 19 ms 7768 KB Output is correct
17 Correct 19 ms 7480 KB Output is correct
18 Correct 4 ms 7772 KB Output is correct
19 Correct 4 ms 7260 KB Output is correct
20 Correct 17 ms 7816 KB Output is correct
21 Correct 14 ms 7516 KB Output is correct
22 Correct 1 ms 6748 KB Output is correct
23 Correct 4 ms 7236 KB Output is correct
24 Correct 4 ms 7256 KB Output is correct
25 Correct 1 ms 6748 KB Output is correct
26 Correct 1 ms 6744 KB Output is correct
27 Correct 4 ms 7772 KB Output is correct
28 Correct 4 ms 7256 KB Output is correct
29 Correct 3 ms 7260 KB Output is correct
30 Correct 3 ms 7052 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 1 ms 6748 KB Output is correct
33 Correct 2 ms 6748 KB Output is correct
34 Correct 1 ms 6748 KB Output is correct
35 Correct 1 ms 6748 KB Output is correct
36 Correct 48 ms 20780 KB Output is correct
37 Correct 2 ms 6744 KB Output is correct
38 Correct 45 ms 20576 KB Output is correct
39 Correct 41 ms 20004 KB Output is correct
40 Correct 46 ms 21448 KB Output is correct
41 Correct 55 ms 21292 KB Output is correct
42 Correct 45 ms 15184 KB Output is correct
43 Correct 46 ms 12884 KB Output is correct
44 Correct 47 ms 12368 KB Output is correct
45 Correct 47 ms 12372 KB Output is correct
46 Correct 53 ms 12576 KB Output is correct
47 Correct 43 ms 17148 KB Output is correct
48 Correct 44 ms 17188 KB Output is correct
49 Correct 49 ms 17236 KB Output is correct
50 Correct 46 ms 17088 KB Output is correct
51 Correct 51 ms 22740 KB Output is correct
52 Correct 44 ms 22664 KB Output is correct
53 Correct 33 ms 12368 KB Output is correct
54 Correct 35 ms 12600 KB Output is correct
55 Correct 46 ms 12320 KB Output is correct
56 Correct 44 ms 12372 KB Output is correct
57 Correct 67 ms 12308 KB Output is correct
58 Correct 48 ms 12372 KB Output is correct
59 Correct 46 ms 12368 KB Output is correct
60 Correct 47 ms 12768 KB Output is correct
61 Correct 58 ms 17692 KB Output is correct
62 Correct 54 ms 17620 KB Output is correct
63 Correct 50 ms 17416 KB Output is correct
64 Correct 46 ms 17236 KB Output is correct
65 Correct 52 ms 12364 KB Output is correct
66 Correct 70 ms 12432 KB Output is correct
67 Correct 58 ms 12296 KB Output is correct
68 Correct 33 ms 12124 KB Output is correct
69 Correct 31 ms 12172 KB Output is correct
70 Correct 47 ms 17624 KB Output is correct
71 Correct 48 ms 17040 KB Output is correct
72 Correct 1 ms 6744 KB Output is correct
73 Correct 4 ms 7260 KB Output is correct
74 Correct 4 ms 7260 KB Output is correct
75 Correct 1 ms 6748 KB Output is correct
76 Correct 1 ms 6748 KB Output is correct
77 Correct 5 ms 7768 KB Output is correct
78 Correct 4 ms 7260 KB Output is correct
79 Correct 3 ms 7260 KB Output is correct
80 Correct 5 ms 7260 KB Output is correct
81 Correct 1 ms 6748 KB Output is correct
82 Correct 1 ms 7256 KB Output is correct
83 Correct 1 ms 6744 KB Output is correct
84 Correct 1 ms 6748 KB Output is correct
85 Correct 1 ms 6748 KB Output is correct
86 Correct 18 ms 7516 KB Output is correct
87 Correct 19 ms 7528 KB Output is correct
88 Correct 18 ms 7512 KB Output is correct
89 Correct 5 ms 7768 KB Output is correct
90 Correct 4 ms 7416 KB Output is correct
91 Correct 12 ms 7772 KB Output is correct
92 Correct 15 ms 7580 KB Output is correct
93 Correct 1 ms 6748 KB Output is correct
94 Correct 46 ms 22488 KB Output is correct
95 Correct 44 ms 22280 KB Output is correct
96 Correct 45 ms 22064 KB Output is correct
97 Correct 43 ms 22472 KB Output is correct
98 Correct 51 ms 22976 KB Output is correct
99 Correct 47 ms 12372 KB Output is correct
100 Correct 45 ms 12372 KB Output is correct
101 Correct 63 ms 12552 KB Output is correct
102 Correct 47 ms 12772 KB Output is correct
103 Correct 55 ms 17232 KB Output is correct
104 Correct 48 ms 17500 KB Output is correct
105 Correct 45 ms 16980 KB Output is correct
106 Correct 46 ms 17232 KB Output is correct
107 Correct 59 ms 17236 KB Output is correct
108 Correct 46 ms 22728 KB Output is correct
109 Correct 44 ms 22580 KB Output is correct
110 Correct 37 ms 12392 KB Output is correct
111 Correct 33 ms 12348 KB Output is correct
112 Correct 46 ms 12180 KB Output is correct
113 Correct 63 ms 12316 KB Output is correct