답안 #944583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944583 2024-03-13T01:15:00 Z VinhLuu Mergers (JOI19_mergers) C++17
10 / 100
148 ms 74032 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;

        sort(all(vr[i]), [&] (int x,int y){return in[x] < in[y];});
        int mi = vr[i][0];
        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:132:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |         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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 51548 KB Output is correct
2 Correct 11 ms 51548 KB Output is correct
3 Correct 12 ms 51780 KB Output is correct
4 Correct 10 ms 51548 KB Output is correct
5 Correct 10 ms 51688 KB Output is correct
6 Correct 10 ms 51548 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 11 ms 51772 KB Output is correct
9 Correct 10 ms 51704 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 12 ms 51548 KB Output is correct
12 Correct 10 ms 51548 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 11 ms 51804 KB Output is correct
16 Correct 10 ms 51548 KB Output is correct
17 Correct 10 ms 51544 KB Output is correct
18 Correct 10 ms 51548 KB Output is correct
19 Correct 10 ms 51776 KB Output is correct
20 Correct 10 ms 51780 KB Output is correct
21 Correct 11 ms 51548 KB Output is correct
22 Correct 10 ms 51784 KB Output is correct
23 Correct 11 ms 51548 KB Output is correct
24 Correct 10 ms 51548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 51548 KB Output is correct
2 Correct 11 ms 51548 KB Output is correct
3 Correct 12 ms 51780 KB Output is correct
4 Correct 10 ms 51548 KB Output is correct
5 Correct 10 ms 51688 KB Output is correct
6 Correct 10 ms 51548 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 11 ms 51772 KB Output is correct
9 Correct 10 ms 51704 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 12 ms 51548 KB Output is correct
12 Correct 10 ms 51548 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 11 ms 51804 KB Output is correct
16 Correct 10 ms 51548 KB Output is correct
17 Correct 10 ms 51544 KB Output is correct
18 Correct 10 ms 51548 KB Output is correct
19 Correct 10 ms 51776 KB Output is correct
20 Correct 10 ms 51780 KB Output is correct
21 Correct 11 ms 51548 KB Output is correct
22 Correct 10 ms 51784 KB Output is correct
23 Correct 11 ms 51548 KB Output is correct
24 Correct 10 ms 51548 KB Output is correct
25 Correct 10 ms 51548 KB Output is correct
26 Correct 12 ms 51800 KB Output is correct
27 Correct 12 ms 51804 KB Output is correct
28 Correct 12 ms 52060 KB Output is correct
29 Correct 12 ms 52056 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 11 ms 51548 KB Output is correct
34 Correct 12 ms 51800 KB Output is correct
35 Correct 13 ms 52060 KB Output is correct
36 Correct 14 ms 51800 KB Output is correct
37 Correct 12 ms 51804 KB Output is correct
38 Correct 11 ms 51692 KB Output is correct
39 Correct 12 ms 51800 KB Output is correct
40 Correct 12 ms 51896 KB Output is correct
41 Incorrect 12 ms 51804 KB Output isn't correct
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 51548 KB Output is correct
2 Correct 11 ms 51548 KB Output is correct
3 Correct 12 ms 51780 KB Output is correct
4 Correct 10 ms 51548 KB Output is correct
5 Correct 10 ms 51688 KB Output is correct
6 Correct 10 ms 51548 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 11 ms 51772 KB Output is correct
9 Correct 10 ms 51704 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 12 ms 51548 KB Output is correct
12 Correct 10 ms 51548 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 11 ms 51804 KB Output is correct
16 Correct 10 ms 51548 KB Output is correct
17 Correct 10 ms 51544 KB Output is correct
18 Correct 10 ms 51548 KB Output is correct
19 Correct 10 ms 51776 KB Output is correct
20 Correct 10 ms 51780 KB Output is correct
21 Correct 11 ms 51548 KB Output is correct
22 Correct 10 ms 51784 KB Output is correct
23 Correct 11 ms 51548 KB Output is correct
24 Correct 10 ms 51548 KB Output is correct
25 Correct 10 ms 51548 KB Output is correct
26 Correct 74 ms 64456 KB Output is correct
27 Correct 119 ms 64296 KB Output is correct
28 Correct 12 ms 51924 KB Output is correct
29 Correct 10 ms 51548 KB Output is correct
30 Correct 11 ms 51652 KB Output is correct
31 Correct 97 ms 64240 KB Output is correct
32 Correct 12 ms 51800 KB Output is correct
33 Correct 96 ms 71448 KB Output is correct
34 Correct 105 ms 64336 KB Output is correct
35 Correct 12 ms 51804 KB Output is correct
36 Correct 81 ms 65104 KB Output is correct
37 Correct 12 ms 51804 KB Output is correct
38 Incorrect 12 ms 51804 KB Output isn't correct
39 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 64632 KB Output is correct
2 Correct 115 ms 70520 KB Output is correct
3 Correct 12 ms 51804 KB Output is correct
4 Correct 12 ms 51924 KB Output is correct
5 Correct 10 ms 51548 KB Output is correct
6 Correct 10 ms 51544 KB Output is correct
7 Correct 13 ms 51804 KB Output is correct
8 Correct 95 ms 66208 KB Output is correct
9 Correct 12 ms 51804 KB Output is correct
10 Correct 88 ms 64224 KB Output is correct
11 Correct 12 ms 51548 KB Output is correct
12 Correct 77 ms 64284 KB Output is correct
13 Correct 94 ms 66384 KB Output is correct
14 Correct 148 ms 70100 KB Output is correct
15 Correct 86 ms 64588 KB Output is correct
16 Correct 13 ms 52056 KB Output is correct
17 Correct 10 ms 51544 KB Output is correct
18 Correct 87 ms 69404 KB Output is correct
19 Correct 138 ms 74032 KB Output is correct
20 Correct 12 ms 51804 KB Output is correct
21 Correct 11 ms 51548 KB Output is correct
22 Correct 100 ms 66268 KB Output is correct
23 Incorrect 12 ms 51804 KB Output isn't correct
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 51548 KB Output is correct
2 Correct 11 ms 51548 KB Output is correct
3 Correct 12 ms 51780 KB Output is correct
4 Correct 10 ms 51548 KB Output is correct
5 Correct 10 ms 51688 KB Output is correct
6 Correct 10 ms 51548 KB Output is correct
7 Correct 8 ms 39260 KB Output is correct
8 Correct 11 ms 51772 KB Output is correct
9 Correct 10 ms 51704 KB Output is correct
10 Correct 10 ms 51548 KB Output is correct
11 Correct 12 ms 51548 KB Output is correct
12 Correct 10 ms 51548 KB Output is correct
13 Correct 11 ms 51548 KB Output is correct
14 Correct 11 ms 51548 KB Output is correct
15 Correct 11 ms 51804 KB Output is correct
16 Correct 10 ms 51548 KB Output is correct
17 Correct 10 ms 51544 KB Output is correct
18 Correct 10 ms 51548 KB Output is correct
19 Correct 10 ms 51776 KB Output is correct
20 Correct 10 ms 51780 KB Output is correct
21 Correct 11 ms 51548 KB Output is correct
22 Correct 10 ms 51784 KB Output is correct
23 Correct 11 ms 51548 KB Output is correct
24 Correct 10 ms 51548 KB Output is correct
25 Correct 10 ms 51548 KB Output is correct
26 Correct 12 ms 51800 KB Output is correct
27 Correct 12 ms 51804 KB Output is correct
28 Correct 12 ms 52060 KB Output is correct
29 Correct 12 ms 52056 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 11 ms 51548 KB Output is correct
34 Correct 12 ms 51800 KB Output is correct
35 Correct 13 ms 52060 KB Output is correct
36 Correct 14 ms 51800 KB Output is correct
37 Correct 12 ms 51804 KB Output is correct
38 Correct 11 ms 51692 KB Output is correct
39 Correct 12 ms 51800 KB Output is correct
40 Correct 12 ms 51896 KB Output is correct
41 Incorrect 12 ms 51804 KB Output isn't correct
42 Halted 0 ms 0 KB -