Submission #944582

# Submission time Handle Problem Language Result Execution time Memory
944582 2024-03-13T01:13:23 Z VinhLuu Mergers (JOI19_mergers) C++17
10 / 100
144 ms 75640 KB
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 5e5 + 5;
const ll oo = 1e16;
const int mod = 1e9 + 7;
const int base = 23;

int n, k, ans, X[N], Y[N], col[N], a[N], f[N][22], dp[N], in[N], en[N], demin, b[N], st[N << 1];
vector<int> p[N], vr[N], open[N];
bool fl[N];

void update(int i){
    i += n - 1;
    st[i] ++;
    while(i > 1){
        i /= 2;
        st[i]++;
    }
}

int get(int l,int r){
    r++;
    int ret = 0;
    for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
        if(l & 1) ret += st[l ++];
        if(r & 1) ret += st[-- r];
    }
    return ret;
}

void pre(int u,int v){
    dp[u] = ((int)p[u].size() == 1);
    if(v == 0) for(int i = 0; i <= 20; i ++) f[u][i] = u;
    else{
        f[u][0] = v;
        for(int i = 1; i <= 20; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
    }
    in[u] = ++demin;
    b[demin] = u;
    for(auto j : p[u]){
        if(j == v) continue;
        pre(j, u);
        dp[u] += dp[j];
    }
    en[u] = demin;
}

bool kt(int u,int v){
    return in[u] <= in[v] && en[u] >= in[v];
}

int lca(int u,int v){
    int kq = u;
    if(kt(u, v)) return u;
    else{
        for(int i = 20; i >= 0; i --){
            if(kt(f[u][i], v)) kq = f[u][i];
            else u = f[u][i];
        }
        return kq;
    }
}

void dfs(int u,int v){
    for(auto j : p[u]){
        if(j == v) continue;
        dfs(j, u);
        fl[u] = max(fl[j], fl[u]);
    }
    for(auto j : open[u]){
        for(auto o : vr[j]) update(in[o]);
    }
    if(v != 0 && !fl[u] && get(in[u], en[u]) == (en[u] - in[u] + 1)){
        fl[u] = true;
        ans ++;
    }
}

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

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

    cin >> n >> k;

    for(int i = 1; i <= n - 1; i ++){
        cin >> X[i] >> Y[i];
        p[X[i]].pb(Y[i]);
        p[Y[i]].pb(X[i]);
    }
    int root = 1, tmp = 1;
    for(int i = 1; i <= n; i ++){
        if((int)p[i].size() > tmp) root = i, tmp = (int)p[i].size();
        int x; cin >> x;
        vr[x].pb(i);
        col[i] = x;
    }
    if(n == 1){
        cout << 0;
        return 0;
    }
    if(n == 2){
        if(col[1] == col[2]){
            cout << 0 << "\n";
        }else cout << 1 << "\n";
        return 0;
    }
    in[n + 1] = n + 1;
    pre(root, 0);
     for(int i = 1; i <= k; i ++){
        if(vr[i].empty()) continue;
        int mi = vr[i][0];
        sort(all(vr[i]), [&] (int x,int y){return in[x] < in[y];});
        for(int j = 0; j < vr[i].size() - 1; j ++){
            int u = lca(vr[i][j], vr[i][j + 1]);
            if(in[u] < in[mi]) mi = u;
        }
//        cout << i << " " << mi << "f\n";
        open[mi].pb(i);
    }
    dfs(root, 0);
    cerr << (ans + 1) / 2 << "\n";
   cout << (ans + 1) / 2;
}


Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:131:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         for(int j = 0; j < vr[i].size() - 1; j ++){
      |                        ~~^~~~~~~~~~~~~~~~~~
mergers.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 51708 KB Output is correct
2 Correct 10 ms 51548 KB Output is correct
3 Correct 10 ms 51684 KB Output is correct
4 Correct 11 ms 51548 KB Output is correct
5 Correct 10 ms 51776 KB Output is correct
6 Correct 11 ms 51548 KB Output is correct
7 Correct 9 ms 39272 KB Output is correct
8 Correct 12 ms 51544 KB Output is correct
9 Correct 11 ms 51548 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 11 ms 51548 KB Output is correct
12 Correct 10 ms 51784 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 10 ms 51548 KB Output is correct
16 Correct 11 ms 51708 KB Output is correct
17 Correct 10 ms 51548 KB Output is correct
18 Correct 10 ms 51664 KB Output is correct
19 Correct 12 ms 51544 KB Output is correct
20 Correct 10 ms 51752 KB Output is correct
21 Correct 10 ms 51548 KB Output is correct
22 Correct 10 ms 51548 KB Output is correct
23 Correct 10 ms 51548 KB Output is correct
24 Correct 11 ms 51548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 51708 KB Output is correct
2 Correct 10 ms 51548 KB Output is correct
3 Correct 10 ms 51684 KB Output is correct
4 Correct 11 ms 51548 KB Output is correct
5 Correct 10 ms 51776 KB Output is correct
6 Correct 11 ms 51548 KB Output is correct
7 Correct 9 ms 39272 KB Output is correct
8 Correct 12 ms 51544 KB Output is correct
9 Correct 11 ms 51548 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 11 ms 51548 KB Output is correct
12 Correct 10 ms 51784 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 10 ms 51548 KB Output is correct
16 Correct 11 ms 51708 KB Output is correct
17 Correct 10 ms 51548 KB Output is correct
18 Correct 10 ms 51664 KB Output is correct
19 Correct 12 ms 51544 KB Output is correct
20 Correct 10 ms 51752 KB Output is correct
21 Correct 10 ms 51548 KB Output is correct
22 Correct 10 ms 51548 KB Output is correct
23 Correct 10 ms 51548 KB Output is correct
24 Correct 11 ms 51548 KB Output is correct
25 Correct 11 ms 51548 KB Output is correct
26 Correct 12 ms 52060 KB Output is correct
27 Correct 12 ms 51912 KB Output is correct
28 Correct 13 ms 52060 KB Output is correct
29 Correct 12 ms 52060 KB Output is correct
30 Correct 12 ms 51804 KB Output is correct
31 Correct 10 ms 51548 KB Output is correct
32 Correct 12 ms 52056 KB Output is correct
33 Correct 10 ms 51548 KB Output is correct
34 Correct 12 ms 51800 KB Output is correct
35 Correct 12 ms 52120 KB Output is correct
36 Correct 12 ms 51804 KB Output is correct
37 Correct 12 ms 52000 KB Output is correct
38 Correct 10 ms 51548 KB Output is correct
39 Correct 12 ms 51804 KB Output is correct
40 Correct 12 ms 51752 KB Output is correct
41 Incorrect 12 ms 51804 KB Output isn't correct
42 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 51708 KB Output is correct
2 Correct 10 ms 51548 KB Output is correct
3 Correct 10 ms 51684 KB Output is correct
4 Correct 11 ms 51548 KB Output is correct
5 Correct 10 ms 51776 KB Output is correct
6 Correct 11 ms 51548 KB Output is correct
7 Correct 9 ms 39272 KB Output is correct
8 Correct 12 ms 51544 KB Output is correct
9 Correct 11 ms 51548 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 11 ms 51548 KB Output is correct
12 Correct 10 ms 51784 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 10 ms 51548 KB Output is correct
16 Correct 11 ms 51708 KB Output is correct
17 Correct 10 ms 51548 KB Output is correct
18 Correct 10 ms 51664 KB Output is correct
19 Correct 12 ms 51544 KB Output is correct
20 Correct 10 ms 51752 KB Output is correct
21 Correct 10 ms 51548 KB Output is correct
22 Correct 10 ms 51548 KB Output is correct
23 Correct 10 ms 51548 KB Output is correct
24 Correct 11 ms 51548 KB Output is correct
25 Correct 10 ms 51548 KB Output is correct
26 Correct 78 ms 65844 KB Output is correct
27 Correct 122 ms 65724 KB Output is correct
28 Correct 12 ms 51908 KB Output is correct
29 Correct 10 ms 51548 KB Output is correct
30 Correct 10 ms 51796 KB Output is correct
31 Correct 88 ms 65676 KB Output is correct
32 Correct 12 ms 51800 KB Output is correct
33 Correct 99 ms 72788 KB Output is correct
34 Correct 98 ms 65872 KB Output is correct
35 Correct 12 ms 51800 KB Output is correct
36 Correct 88 ms 66388 KB Output is correct
37 Correct 12 ms 51800 KB Output is correct
38 Incorrect 13 ms 51800 KB Output isn't correct
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 64468 KB Output is correct
2 Correct 107 ms 70276 KB Output is correct
3 Correct 12 ms 52060 KB Output is correct
4 Correct 13 ms 51804 KB Output is correct
5 Correct 11 ms 51548 KB Output is correct
6 Correct 11 ms 51548 KB Output is correct
7 Correct 12 ms 51888 KB Output is correct
8 Correct 107 ms 68008 KB Output is correct
9 Correct 13 ms 51804 KB Output is correct
10 Correct 96 ms 65876 KB Output is correct
11 Correct 10 ms 51548 KB Output is correct
12 Correct 77 ms 65784 KB Output is correct
13 Correct 98 ms 68268 KB Output is correct
14 Correct 137 ms 71696 KB Output is correct
15 Correct 84 ms 66120 KB Output is correct
16 Correct 12 ms 51800 KB Output is correct
17 Correct 12 ms 51548 KB Output is correct
18 Correct 93 ms 71100 KB Output is correct
19 Correct 144 ms 75640 KB Output is correct
20 Correct 13 ms 51800 KB Output is correct
21 Correct 12 ms 51548 KB Output is correct
22 Correct 88 ms 68024 KB Output is correct
23 Incorrect 13 ms 51804 KB Output isn't correct
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 51708 KB Output is correct
2 Correct 10 ms 51548 KB Output is correct
3 Correct 10 ms 51684 KB Output is correct
4 Correct 11 ms 51548 KB Output is correct
5 Correct 10 ms 51776 KB Output is correct
6 Correct 11 ms 51548 KB Output is correct
7 Correct 9 ms 39272 KB Output is correct
8 Correct 12 ms 51544 KB Output is correct
9 Correct 11 ms 51548 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 11 ms 51548 KB Output is correct
12 Correct 10 ms 51784 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 10 ms 51548 KB Output is correct
16 Correct 11 ms 51708 KB Output is correct
17 Correct 10 ms 51548 KB Output is correct
18 Correct 10 ms 51664 KB Output is correct
19 Correct 12 ms 51544 KB Output is correct
20 Correct 10 ms 51752 KB Output is correct
21 Correct 10 ms 51548 KB Output is correct
22 Correct 10 ms 51548 KB Output is correct
23 Correct 10 ms 51548 KB Output is correct
24 Correct 11 ms 51548 KB Output is correct
25 Correct 11 ms 51548 KB Output is correct
26 Correct 12 ms 52060 KB Output is correct
27 Correct 12 ms 51912 KB Output is correct
28 Correct 13 ms 52060 KB Output is correct
29 Correct 12 ms 52060 KB Output is correct
30 Correct 12 ms 51804 KB Output is correct
31 Correct 10 ms 51548 KB Output is correct
32 Correct 12 ms 52056 KB Output is correct
33 Correct 10 ms 51548 KB Output is correct
34 Correct 12 ms 51800 KB Output is correct
35 Correct 12 ms 52120 KB Output is correct
36 Correct 12 ms 51804 KB Output is correct
37 Correct 12 ms 52000 KB Output is correct
38 Correct 10 ms 51548 KB Output is correct
39 Correct 12 ms 51804 KB Output is correct
40 Correct 12 ms 51752 KB Output is correct
41 Incorrect 12 ms 51804 KB Output isn't correct
42 Halted 0 ms 0 KB -