Submission #920644

# Submission time Handle Problem Language Result Execution time Memory
920644 2024-02-02T20:07:22 Z BBart888 Railway (BOI17_railway) C++14
100 / 100
285 ms 85016 KB
#include "bits/stdc++.h"
#define fast ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
#define endl '\n'
#define int long long
#define f first
#define mp make_pair
#define s second
using namespace std;

int n, m, k;
int fa[100005][21], par[100005], dep[100005], arr[150005];
set <int> vals[100005], ans;
vector <int> g[100005], rem[100005], add[100005];
unordered_map<int, unordered_map<int,int> > edge;

void dfs(int x, int d, int p){
    dep[x] = d;
    par[x] = p;
    for(int k : g[x]){
        if(k != p) dfs(k, d + 1, x);
    }
} 

int LCA(int a, int b){
    if(dep[a] > dep[b]) swap(a, b);
    int d = dep[b] - dep[a];
    for(int i = 0; i < 20; i++){
        if(d & (1 << i)) b = fa[b][i];
    }
    if(a == b) return a;
    for(int i = 20; i >= 0; i--){
        if(fa[a][i] != fa[b][i]){
            a = fa[a][i];
            b = fa[b][i];
        }
    }
    return par[a];
}

void solve(int node, int parent){
    int big = node;
    for(int i : g[node]){
        if(i == parent) continue;
        solve(i, node);
        if(vals[i].size() > vals[big].size()) big = i;
        if(vals[i].size() >= k) ans.insert(edge[node][i]);
    }
    swap(vals[node], vals[big]);
    for(int i : g[node]){
        if(i == parent) continue;
        for(int x : vals[i]) vals[node].insert(x);
    }
    for(int i : add[node]) vals[node].insert(i);
    for(int i : rem[node]) vals[node].erase(i);
}

signed main()
{
    fast;
    cin >> n >> m >> k;
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        edge[a][b] = edge[b][a] = i;
    }
    dfs(1, 1, -1);
    par[1] = 1;
    for(int i = 1; i <= n; i++) fa[i][0] = par[i];
    for(int k = 0; k < 20; k++){
        for(int i = 1; i <= n; i++){
            fa[i][k+1] = fa[fa[i][k]][k];
        }
    }
    for(int i = 0; i < m; i++){
        int t; cin >> t;
        int y = 0;
        for(int j = 0; j < t; j++){
            cin >> arr[j];
            y = (j == 0 ? arr[j] : LCA(y, arr[j]));
        }
        rem[y].push_back(i);
        for(int j = 0; j < t; j++) add[arr[j]].push_back(i);
    }
    solve(1, -1);
    cout << ans.size() << endl;
    for(int i : ans) cout << i + 1 << ' ';
    cout << endl;
}

Compilation message

railway.cpp: In function 'void solve(long long int, long long int)':
railway.cpp:46:27: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   46 |         if(vals[i].size() >= k) ans.insert(edge[node][i]);
      |            ~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 13 ms 20304 KB Output is correct
3 Correct 12 ms 20060 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14936 KB Output is correct
6 Correct 13 ms 20828 KB Output is correct
7 Correct 14 ms 20192 KB Output is correct
8 Correct 12 ms 20316 KB Output is correct
9 Correct 15 ms 20420 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 13 ms 20304 KB Output is correct
3 Correct 12 ms 20060 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14936 KB Output is correct
6 Correct 13 ms 20828 KB Output is correct
7 Correct 14 ms 20192 KB Output is correct
8 Correct 12 ms 20316 KB Output is correct
9 Correct 15 ms 20420 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 35 ms 24660 KB Output is correct
16 Correct 34 ms 24924 KB Output is correct
17 Correct 35 ms 25180 KB Output is correct
18 Correct 16 ms 21084 KB Output is correct
19 Correct 13 ms 20312 KB Output is correct
20 Correct 35 ms 26788 KB Output is correct
21 Correct 36 ms 26716 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 14 ms 20160 KB Output is correct
24 Correct 12 ms 20124 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14988 KB Output is correct
27 Correct 14 ms 21080 KB Output is correct
28 Correct 13 ms 20312 KB Output is correct
29 Correct 15 ms 20828 KB Output is correct
30 Correct 14 ms 20316 KB Output is correct
31 Correct 3 ms 14936 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14936 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 82888 KB Output is correct
2 Correct 3 ms 14936 KB Output is correct
3 Correct 197 ms 81840 KB Output is correct
4 Correct 157 ms 79560 KB Output is correct
5 Correct 179 ms 81860 KB Output is correct
6 Correct 171 ms 83004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 74176 KB Output is correct
2 Correct 214 ms 71296 KB Output is correct
3 Correct 228 ms 76484 KB Output is correct
4 Correct 232 ms 76608 KB Output is correct
5 Correct 233 ms 77220 KB Output is correct
6 Correct 199 ms 74944 KB Output is correct
7 Correct 203 ms 74688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 74176 KB Output is correct
2 Correct 214 ms 71296 KB Output is correct
3 Correct 228 ms 76484 KB Output is correct
4 Correct 232 ms 76608 KB Output is correct
5 Correct 233 ms 77220 KB Output is correct
6 Correct 199 ms 74944 KB Output is correct
7 Correct 203 ms 74688 KB Output is correct
8 Correct 212 ms 74696 KB Output is correct
9 Correct 211 ms 74440 KB Output is correct
10 Correct 169 ms 82128 KB Output is correct
11 Correct 173 ms 81972 KB Output is correct
12 Correct 159 ms 70600 KB Output is correct
13 Correct 163 ms 70544 KB Output is correct
14 Correct 224 ms 71580 KB Output is correct
15 Correct 206 ms 71872 KB Output is correct
16 Correct 242 ms 78020 KB Output is correct
17 Correct 237 ms 78020 KB Output is correct
18 Correct 230 ms 75992 KB Output is correct
19 Correct 212 ms 71904 KB Output is correct
20 Correct 221 ms 75932 KB Output is correct
21 Correct 218 ms 76268 KB Output is correct
22 Correct 211 ms 75820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 13 ms 20304 KB Output is correct
3 Correct 12 ms 20060 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 4 ms 14936 KB Output is correct
6 Correct 13 ms 20828 KB Output is correct
7 Correct 14 ms 20192 KB Output is correct
8 Correct 12 ms 20316 KB Output is correct
9 Correct 15 ms 20420 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 5 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 35 ms 24660 KB Output is correct
16 Correct 34 ms 24924 KB Output is correct
17 Correct 35 ms 25180 KB Output is correct
18 Correct 16 ms 21084 KB Output is correct
19 Correct 13 ms 20312 KB Output is correct
20 Correct 35 ms 26788 KB Output is correct
21 Correct 36 ms 26716 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 14 ms 20160 KB Output is correct
24 Correct 12 ms 20124 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 3 ms 14988 KB Output is correct
27 Correct 14 ms 21080 KB Output is correct
28 Correct 13 ms 20312 KB Output is correct
29 Correct 15 ms 20828 KB Output is correct
30 Correct 14 ms 20316 KB Output is correct
31 Correct 3 ms 14936 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 3 ms 14936 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 4 ms 14940 KB Output is correct
36 Correct 187 ms 82888 KB Output is correct
37 Correct 3 ms 14936 KB Output is correct
38 Correct 197 ms 81840 KB Output is correct
39 Correct 157 ms 79560 KB Output is correct
40 Correct 179 ms 81860 KB Output is correct
41 Correct 171 ms 83004 KB Output is correct
42 Correct 203 ms 74176 KB Output is correct
43 Correct 214 ms 71296 KB Output is correct
44 Correct 228 ms 76484 KB Output is correct
45 Correct 232 ms 76608 KB Output is correct
46 Correct 233 ms 77220 KB Output is correct
47 Correct 199 ms 74944 KB Output is correct
48 Correct 203 ms 74688 KB Output is correct
49 Correct 212 ms 74696 KB Output is correct
50 Correct 211 ms 74440 KB Output is correct
51 Correct 169 ms 82128 KB Output is correct
52 Correct 173 ms 81972 KB Output is correct
53 Correct 159 ms 70600 KB Output is correct
54 Correct 163 ms 70544 KB Output is correct
55 Correct 224 ms 71580 KB Output is correct
56 Correct 206 ms 71872 KB Output is correct
57 Correct 242 ms 78020 KB Output is correct
58 Correct 237 ms 78020 KB Output is correct
59 Correct 230 ms 75992 KB Output is correct
60 Correct 212 ms 71904 KB Output is correct
61 Correct 221 ms 75932 KB Output is correct
62 Correct 218 ms 76268 KB Output is correct
63 Correct 211 ms 75820 KB Output is correct
64 Correct 217 ms 75148 KB Output is correct
65 Correct 285 ms 85016 KB Output is correct
66 Correct 263 ms 80840 KB Output is correct
67 Correct 231 ms 76224 KB Output is correct
68 Correct 142 ms 68292 KB Output is correct
69 Correct 142 ms 68260 KB Output is correct
70 Correct 223 ms 77144 KB Output is correct
71 Correct 199 ms 74180 KB Output is correct
72 Correct 3 ms 14936 KB Output is correct
73 Correct 12 ms 20060 KB Output is correct
74 Correct 13 ms 20060 KB Output is correct
75 Correct 3 ms 14940 KB Output is correct
76 Correct 3 ms 14940 KB Output is correct
77 Correct 14 ms 20992 KB Output is correct
78 Correct 13 ms 20360 KB Output is correct
79 Correct 12 ms 20304 KB Output is correct
80 Correct 12 ms 20312 KB Output is correct
81 Correct 3 ms 15192 KB Output is correct
82 Correct 3 ms 14940 KB Output is correct
83 Correct 4 ms 14936 KB Output is correct
84 Correct 3 ms 14940 KB Output is correct
85 Correct 3 ms 14940 KB Output is correct
86 Correct 34 ms 24932 KB Output is correct
87 Correct 34 ms 24912 KB Output is correct
88 Correct 36 ms 25216 KB Output is correct
89 Correct 13 ms 21080 KB Output is correct
90 Correct 12 ms 20316 KB Output is correct
91 Correct 37 ms 26964 KB Output is correct
92 Correct 35 ms 26668 KB Output is correct
93 Correct 3 ms 14940 KB Output is correct
94 Correct 207 ms 83076 KB Output is correct
95 Correct 174 ms 81708 KB Output is correct
96 Correct 161 ms 79688 KB Output is correct
97 Correct 185 ms 82008 KB Output is correct
98 Correct 163 ms 82884 KB Output is correct
99 Correct 248 ms 80320 KB Output is correct
100 Correct 234 ms 77524 KB Output is correct
101 Correct 240 ms 77880 KB Output is correct
102 Correct 211 ms 71108 KB Output is correct
103 Correct 206 ms 74732 KB Output is correct
104 Correct 209 ms 76112 KB Output is correct
105 Correct 192 ms 74404 KB Output is correct
106 Correct 223 ms 75460 KB Output is correct
107 Correct 206 ms 74692 KB Output is correct
108 Correct 153 ms 82112 KB Output is correct
109 Correct 173 ms 82328 KB Output is correct
110 Correct 161 ms 70664 KB Output is correct
111 Correct 161 ms 70620 KB Output is correct
112 Correct 216 ms 71824 KB Output is correct
113 Correct 208 ms 71616 KB Output is correct