Submission #1043530

# Submission time Handle Problem Language Result Execution time Memory
1043530 2024-08-04T10:40:25 Z ssense Railway (BOI17_railway) C++14
100 / 100
238 ms 63572 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <limits.h>
#include <cassert>

#define fr first
#define sc second

using namespace std;

#define int long long

const int N = (1e5)+5;

int n, m, k;

vector<int> adj[N];

int par[N][22], depth[N];

void dfs(int u, int p)
{
    depth[u] = depth[p]+1;
    for(auto v : adj[u])
    {
        if(v != p)
        {
            par[v][0] = u;
            dfs(v, u);
        }
    }
}

int jump(int a, int len)
{
    // cout << a << " " << len << endl;
    for(int i = 0; i <= 20; i++)
    {
        if((len&(1<<i)) != 0)
        {
            a = par[a][i];
        }
    }
    // cout << a << endl;
    return a;
}

int lca(int a, int b)
{
    if(depth[a] < depth[b])
    {
        swap(a, b);
    }
    a = jump(a, depth[a]-depth[b]);
    if(a == b){return a;}
    for(int i = 20; i >= 0; i--)
    {
        if(par[a][i] != par[b][i])
        {
            a = par[a][i];
            b = par[b][i];
        }
    }
    return par[a][0];
}

set<pair<int, int> > tree[N];

map<pair<int, int>, int> mpe;

vector<int> ans;

void dfs2(int u, int p)
{
    for(auto v : adj[u])
    {
        if(v != p)
        {
            dfs2(v, u);
        }
    }
    for(auto v : adj[u])
    {
        if(v != p)
        {
            if(tree[v].size() > tree[u].size())
            {
                swap(tree[v], tree[u]);
            }
        }
    }
    for(auto v : adj[u])
    {
        if(v != p)
        {
            tree[u].insert(tree[v].begin(), tree[v].end());
        }
    }
    auto lower = tree[u].lower_bound(std::make_pair(u, INT_MIN));
    auto upper = tree[u].upper_bound(std::make_pair(u, INT_MAX));
    tree[u].erase(lower, upper);
    // cout << "NODE " << u << ": ";
    // for(auto x: tree[u])
    // {
    //     cout << " (" <<x.fr << ", " << x.sc << ") ";
    // }
    // cout << endl;
    if(tree[u].size() >= k)
    {
        ans.push_back(mpe[make_pair(min(u, p), max(u, p))]);
    }
}

void solve()
{
    cin >> n >> m >> k;
    for(int i = 0; i < n-1; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        mpe[make_pair(min(u, v), max(u, v))] = i+1;
    }
    dfs(1, 0);
    for(int i = 1; i <= 20; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            par[j][i] = par[par[j][i-1]][i-1];
        }
    }
    // for(int i = 1; i <= n; i++)
    // {
    //     cout << i << " " << depth[i] << endl;
    // }
    for(int i = 0; i < m; i++)
    {
        int cnt;
        cin >> cnt;
        vector<int> b(cnt);
        int lc;
        for(int j = 0; j < cnt; j++)
        {
            cin >> b[j];
            if(j == 0){lc = b[0];}
            else{lc = lca(lc, b[j]);}
        }
        assert(lc != 0);
        // cout << "PATANUL " << i << " LCA " << lc << endl;
        for(int j = 0; j < cnt; j++)
        {
            tree[b[j]].insert(make_pair(lc, i));
        }
    }
    dfs2(1, 0);
    //assert(tree[1].size() == 0);
    cout << ans.size() << endl;
    sort(ans.begin(), ans.end());
    for(auto x : ans)
    {
        cout << x << " ";
    }
}

int32_t main()
{
    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
}

/*
6 3 2
1 3
2 3
3 4
6 4
4 5
4 1 3 2 5
2 6 3
2 3 2
*/

/*
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <cassert>
*/

Compilation message

railway.cpp: In function 'void dfs2(long long int, long long int)':
railway.cpp:123:23: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  123 |     if(tree[u].size() >= k)
      |        ~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 9 ms 14284 KB Output is correct
3 Correct 6 ms 14172 KB Output is correct
4 Correct 1 ms 11100 KB Output is correct
5 Correct 1 ms 11100 KB Output is correct
6 Correct 8 ms 15196 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 6 ms 14192 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 1 ms 11096 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 1 ms 11100 KB Output is correct
14 Correct 1 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 9 ms 14284 KB Output is correct
3 Correct 6 ms 14172 KB Output is correct
4 Correct 1 ms 11100 KB Output is correct
5 Correct 1 ms 11100 KB Output is correct
6 Correct 8 ms 15196 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 6 ms 14192 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 1 ms 11096 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 1 ms 11100 KB Output is correct
14 Correct 1 ms 11100 KB Output is correct
15 Correct 38 ms 21196 KB Output is correct
16 Correct 39 ms 21340 KB Output is correct
17 Correct 35 ms 21328 KB Output is correct
18 Correct 11 ms 15196 KB Output is correct
19 Correct 7 ms 14504 KB Output is correct
20 Correct 37 ms 20352 KB Output is correct
21 Correct 41 ms 20316 KB Output is correct
22 Correct 1 ms 11100 KB Output is correct
23 Correct 7 ms 14168 KB Output is correct
24 Correct 8 ms 14312 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 1 ms 11100 KB Output is correct
27 Correct 9 ms 15196 KB Output is correct
28 Correct 7 ms 14428 KB Output is correct
29 Correct 6 ms 14428 KB Output is correct
30 Correct 7 ms 14428 KB Output is correct
31 Correct 1 ms 10940 KB Output is correct
32 Correct 1 ms 10940 KB Output is correct
33 Correct 1 ms 11100 KB Output is correct
34 Correct 2 ms 11100 KB Output is correct
35 Correct 2 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 60992 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 133 ms 60420 KB Output is correct
4 Correct 120 ms 59868 KB Output is correct
5 Correct 149 ms 59844 KB Output is correct
6 Correct 162 ms 62916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 53744 KB Output is correct
2 Correct 137 ms 47956 KB Output is correct
3 Correct 149 ms 54564 KB Output is correct
4 Correct 148 ms 54864 KB Output is correct
5 Correct 151 ms 55444 KB Output is correct
6 Correct 128 ms 53844 KB Output is correct
7 Correct 123 ms 53844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 53744 KB Output is correct
2 Correct 137 ms 47956 KB Output is correct
3 Correct 149 ms 54564 KB Output is correct
4 Correct 148 ms 54864 KB Output is correct
5 Correct 151 ms 55444 KB Output is correct
6 Correct 128 ms 53844 KB Output is correct
7 Correct 123 ms 53844 KB Output is correct
8 Correct 121 ms 52388 KB Output is correct
9 Correct 120 ms 52304 KB Output is correct
10 Correct 131 ms 59848 KB Output is correct
11 Correct 160 ms 59596 KB Output is correct
12 Correct 88 ms 41808 KB Output is correct
13 Correct 84 ms 41792 KB Output is correct
14 Correct 126 ms 48724 KB Output is correct
15 Correct 131 ms 48720 KB Output is correct
16 Correct 166 ms 56636 KB Output is correct
17 Correct 196 ms 56400 KB Output is correct
18 Correct 140 ms 54076 KB Output is correct
19 Correct 238 ms 48888 KB Output is correct
20 Correct 138 ms 54208 KB Output is correct
21 Correct 130 ms 54092 KB Output is correct
22 Correct 128 ms 54224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 9 ms 14284 KB Output is correct
3 Correct 6 ms 14172 KB Output is correct
4 Correct 1 ms 11100 KB Output is correct
5 Correct 1 ms 11100 KB Output is correct
6 Correct 8 ms 15196 KB Output is correct
7 Correct 7 ms 14428 KB Output is correct
8 Correct 6 ms 14192 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 1 ms 11096 KB Output is correct
11 Correct 2 ms 11100 KB Output is correct
12 Correct 2 ms 11100 KB Output is correct
13 Correct 1 ms 11100 KB Output is correct
14 Correct 1 ms 11100 KB Output is correct
15 Correct 38 ms 21196 KB Output is correct
16 Correct 39 ms 21340 KB Output is correct
17 Correct 35 ms 21328 KB Output is correct
18 Correct 11 ms 15196 KB Output is correct
19 Correct 7 ms 14504 KB Output is correct
20 Correct 37 ms 20352 KB Output is correct
21 Correct 41 ms 20316 KB Output is correct
22 Correct 1 ms 11100 KB Output is correct
23 Correct 7 ms 14168 KB Output is correct
24 Correct 8 ms 14312 KB Output is correct
25 Correct 2 ms 11100 KB Output is correct
26 Correct 1 ms 11100 KB Output is correct
27 Correct 9 ms 15196 KB Output is correct
28 Correct 7 ms 14428 KB Output is correct
29 Correct 6 ms 14428 KB Output is correct
30 Correct 7 ms 14428 KB Output is correct
31 Correct 1 ms 10940 KB Output is correct
32 Correct 1 ms 10940 KB Output is correct
33 Correct 1 ms 11100 KB Output is correct
34 Correct 2 ms 11100 KB Output is correct
35 Correct 2 ms 11100 KB Output is correct
36 Correct 130 ms 60992 KB Output is correct
37 Correct 2 ms 11096 KB Output is correct
38 Correct 133 ms 60420 KB Output is correct
39 Correct 120 ms 59868 KB Output is correct
40 Correct 149 ms 59844 KB Output is correct
41 Correct 162 ms 62916 KB Output is correct
42 Correct 129 ms 53744 KB Output is correct
43 Correct 137 ms 47956 KB Output is correct
44 Correct 149 ms 54564 KB Output is correct
45 Correct 148 ms 54864 KB Output is correct
46 Correct 151 ms 55444 KB Output is correct
47 Correct 128 ms 53844 KB Output is correct
48 Correct 123 ms 53844 KB Output is correct
49 Correct 121 ms 52388 KB Output is correct
50 Correct 120 ms 52304 KB Output is correct
51 Correct 131 ms 59848 KB Output is correct
52 Correct 160 ms 59596 KB Output is correct
53 Correct 88 ms 41808 KB Output is correct
54 Correct 84 ms 41792 KB Output is correct
55 Correct 126 ms 48724 KB Output is correct
56 Correct 131 ms 48720 KB Output is correct
57 Correct 166 ms 56636 KB Output is correct
58 Correct 196 ms 56400 KB Output is correct
59 Correct 140 ms 54076 KB Output is correct
60 Correct 238 ms 48888 KB Output is correct
61 Correct 138 ms 54208 KB Output is correct
62 Correct 130 ms 54092 KB Output is correct
63 Correct 128 ms 54224 KB Output is correct
64 Correct 139 ms 52260 KB Output is correct
65 Correct 163 ms 63572 KB Output is correct
66 Correct 148 ms 58636 KB Output is correct
67 Correct 139 ms 52916 KB Output is correct
68 Correct 69 ms 40016 KB Output is correct
69 Correct 76 ms 40020 KB Output is correct
70 Correct 128 ms 53964 KB Output is correct
71 Correct 130 ms 52052 KB Output is correct
72 Correct 2 ms 11096 KB Output is correct
73 Correct 7 ms 14172 KB Output is correct
74 Correct 7 ms 14244 KB Output is correct
75 Correct 1 ms 11100 KB Output is correct
76 Correct 1 ms 11100 KB Output is correct
77 Correct 8 ms 15260 KB Output is correct
78 Correct 8 ms 14428 KB Output is correct
79 Correct 6 ms 14428 KB Output is correct
80 Correct 7 ms 14428 KB Output is correct
81 Correct 1 ms 11100 KB Output is correct
82 Correct 1 ms 11100 KB Output is correct
83 Correct 2 ms 11100 KB Output is correct
84 Correct 1 ms 11100 KB Output is correct
85 Correct 1 ms 11100 KB Output is correct
86 Correct 37 ms 21348 KB Output is correct
87 Correct 35 ms 21336 KB Output is correct
88 Correct 36 ms 21436 KB Output is correct
89 Correct 8 ms 15196 KB Output is correct
90 Correct 7 ms 14428 KB Output is correct
91 Correct 37 ms 20524 KB Output is correct
92 Correct 43 ms 20564 KB Output is correct
93 Correct 2 ms 11100 KB Output is correct
94 Correct 149 ms 60796 KB Output is correct
95 Correct 133 ms 60368 KB Output is correct
96 Correct 123 ms 59728 KB Output is correct
97 Correct 135 ms 59844 KB Output is correct
98 Correct 167 ms 62916 KB Output is correct
99 Correct 171 ms 59732 KB Output is correct
100 Correct 157 ms 55632 KB Output is correct
101 Correct 155 ms 56528 KB Output is correct
102 Correct 142 ms 47956 KB Output is correct
103 Correct 140 ms 53824 KB Output is correct
104 Correct 141 ms 54228 KB Output is correct
105 Correct 122 ms 53588 KB Output is correct
106 Correct 122 ms 52508 KB Output is correct
107 Correct 122 ms 52512 KB Output is correct
108 Correct 126 ms 59596 KB Output is correct
109 Correct 123 ms 59620 KB Output is correct
110 Correct 87 ms 41940 KB Output is correct
111 Correct 82 ms 41812 KB Output is correct
112 Correct 132 ms 48464 KB Output is correct
113 Correct 119 ms 48472 KB Output is correct