답안 #944607

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944607 2024-03-13T01:44:42 Z VinhLuu Mergers (JOI19_mergers) C++17
10 / 100
135 ms 79800 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, pa[N], sz[N], g[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];

int fin(int u){
    return pa[u] == u ? u :  pa[u] = fin(pa[u]);
}

void dsu(int u,int v){
    u = fin(u);
    v = fin(v);
    if(u == v) return;
    if(sz[u] < sz[v]) swap(u, v);
    sz[u] += sz[v];
    pa[v] = u;
}

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(!fl[u] && v && get(in[u], en[u]) == (en[u] - in[u] + 1)){
        ans++;
        fl[u] = true;
    }
}

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 ++){
        pa[i] = i, sz[i] = 1;
        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;
        }
        open[mi].pb(i);
    }
    dfs(root, 0);
//    for(int i = 1; i <= n; i ++){
//        for(auto j : p[i]) if(fin(j) != fin(i)) g[fin(i)]++, g[fin(j)]++;
//    }
//    for(int i = 1; i <= n; i ++) if(g[i] == 2) ans++;
    cerr << (ans + 1) / 2 << "\n";
   cout << (ans + 1) / 2;
}


Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:146:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         for(int j = 0; j < vr[i].size() - 1; j ++){
      |                        ~~^~~~~~~~~~~~~~~~~~
mergers.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 57692 KB Output is correct
2 Correct 11 ms 57692 KB Output is correct
3 Correct 12 ms 57692 KB Output is correct
4 Correct 11 ms 57908 KB Output is correct
5 Correct 12 ms 57692 KB Output is correct
6 Correct 11 ms 57792 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57828 KB Output is correct
9 Correct 11 ms 57692 KB Output is correct
10 Correct 11 ms 57896 KB Output is correct
11 Correct 11 ms 57928 KB Output is correct
12 Correct 11 ms 57932 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57692 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 57692 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57820 KB Output is correct
19 Correct 10 ms 57692 KB Output is correct
20 Correct 11 ms 57692 KB Output is correct
21 Correct 11 ms 57916 KB Output is correct
22 Correct 11 ms 57692 KB Output is correct
23 Correct 10 ms 57688 KB Output is correct
24 Correct 11 ms 57692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 57692 KB Output is correct
2 Correct 11 ms 57692 KB Output is correct
3 Correct 12 ms 57692 KB Output is correct
4 Correct 11 ms 57908 KB Output is correct
5 Correct 12 ms 57692 KB Output is correct
6 Correct 11 ms 57792 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57828 KB Output is correct
9 Correct 11 ms 57692 KB Output is correct
10 Correct 11 ms 57896 KB Output is correct
11 Correct 11 ms 57928 KB Output is correct
12 Correct 11 ms 57932 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57692 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 57692 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57820 KB Output is correct
19 Correct 10 ms 57692 KB Output is correct
20 Correct 11 ms 57692 KB Output is correct
21 Correct 11 ms 57916 KB Output is correct
22 Correct 11 ms 57692 KB Output is correct
23 Correct 10 ms 57688 KB Output is correct
24 Correct 11 ms 57692 KB Output is correct
25 Correct 11 ms 57692 KB Output is correct
26 Correct 13 ms 57996 KB Output is correct
27 Correct 12 ms 57944 KB Output is correct
28 Correct 13 ms 58316 KB Output is correct
29 Correct 13 ms 58200 KB Output is correct
30 Correct 14 ms 57948 KB Output is correct
31 Correct 10 ms 57692 KB Output is correct
32 Correct 13 ms 58200 KB Output is correct
33 Correct 11 ms 57692 KB Output is correct
34 Correct 12 ms 57836 KB Output is correct
35 Correct 13 ms 58400 KB Output is correct
36 Correct 12 ms 57988 KB Output is correct
37 Correct 12 ms 57948 KB Output is correct
38 Correct 11 ms 57692 KB Output is correct
39 Correct 13 ms 57948 KB Output is correct
40 Correct 12 ms 57948 KB Output is correct
41 Incorrect 12 ms 57948 KB Output isn't correct
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 57692 KB Output is correct
2 Correct 11 ms 57692 KB Output is correct
3 Correct 12 ms 57692 KB Output is correct
4 Correct 11 ms 57908 KB Output is correct
5 Correct 12 ms 57692 KB Output is correct
6 Correct 11 ms 57792 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57828 KB Output is correct
9 Correct 11 ms 57692 KB Output is correct
10 Correct 11 ms 57896 KB Output is correct
11 Correct 11 ms 57928 KB Output is correct
12 Correct 11 ms 57932 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57692 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 57692 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57820 KB Output is correct
19 Correct 10 ms 57692 KB Output is correct
20 Correct 11 ms 57692 KB Output is correct
21 Correct 11 ms 57916 KB Output is correct
22 Correct 11 ms 57692 KB Output is correct
23 Correct 10 ms 57688 KB Output is correct
24 Correct 11 ms 57692 KB Output is correct
25 Correct 11 ms 57692 KB Output is correct
26 Correct 78 ms 70628 KB Output is correct
27 Correct 92 ms 70288 KB Output is correct
28 Correct 13 ms 57944 KB Output is correct
29 Correct 11 ms 57692 KB Output is correct
30 Correct 11 ms 57692 KB Output is correct
31 Correct 83 ms 70156 KB Output is correct
32 Correct 12 ms 57948 KB Output is correct
33 Correct 99 ms 77472 KB Output is correct
34 Correct 88 ms 70264 KB Output is correct
35 Correct 12 ms 57948 KB Output is correct
36 Correct 88 ms 70900 KB Output is correct
37 Correct 13 ms 57944 KB Output is correct
38 Incorrect 12 ms 57948 KB Output isn't correct
39 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 70636 KB Output is correct
2 Correct 97 ms 76232 KB Output is correct
3 Correct 13 ms 57948 KB Output is correct
4 Correct 12 ms 57948 KB Output is correct
5 Correct 11 ms 57692 KB Output is correct
6 Correct 11 ms 57692 KB Output is correct
7 Correct 12 ms 57948 KB Output is correct
8 Correct 95 ms 72276 KB Output is correct
9 Correct 12 ms 57948 KB Output is correct
10 Correct 84 ms 70192 KB Output is correct
11 Correct 11 ms 57692 KB Output is correct
12 Correct 88 ms 70252 KB Output is correct
13 Correct 107 ms 72316 KB Output is correct
14 Correct 109 ms 76104 KB Output is correct
15 Correct 75 ms 70416 KB Output is correct
16 Correct 12 ms 58056 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 95 ms 75472 KB Output is correct
19 Correct 135 ms 79800 KB Output is correct
20 Correct 13 ms 57944 KB Output is correct
21 Correct 11 ms 57692 KB Output is correct
22 Correct 82 ms 72416 KB Output is correct
23 Incorrect 13 ms 57948 KB Output isn't correct
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 57692 KB Output is correct
2 Correct 11 ms 57692 KB Output is correct
3 Correct 12 ms 57692 KB Output is correct
4 Correct 11 ms 57908 KB Output is correct
5 Correct 12 ms 57692 KB Output is correct
6 Correct 11 ms 57792 KB Output is correct
7 Correct 9 ms 45404 KB Output is correct
8 Correct 11 ms 57828 KB Output is correct
9 Correct 11 ms 57692 KB Output is correct
10 Correct 11 ms 57896 KB Output is correct
11 Correct 11 ms 57928 KB Output is correct
12 Correct 11 ms 57932 KB Output is correct
13 Correct 11 ms 57692 KB Output is correct
14 Correct 12 ms 57692 KB Output is correct
15 Correct 11 ms 57692 KB Output is correct
16 Correct 11 ms 57692 KB Output is correct
17 Correct 11 ms 57692 KB Output is correct
18 Correct 11 ms 57820 KB Output is correct
19 Correct 10 ms 57692 KB Output is correct
20 Correct 11 ms 57692 KB Output is correct
21 Correct 11 ms 57916 KB Output is correct
22 Correct 11 ms 57692 KB Output is correct
23 Correct 10 ms 57688 KB Output is correct
24 Correct 11 ms 57692 KB Output is correct
25 Correct 11 ms 57692 KB Output is correct
26 Correct 13 ms 57996 KB Output is correct
27 Correct 12 ms 57944 KB Output is correct
28 Correct 13 ms 58316 KB Output is correct
29 Correct 13 ms 58200 KB Output is correct
30 Correct 14 ms 57948 KB Output is correct
31 Correct 10 ms 57692 KB Output is correct
32 Correct 13 ms 58200 KB Output is correct
33 Correct 11 ms 57692 KB Output is correct
34 Correct 12 ms 57836 KB Output is correct
35 Correct 13 ms 58400 KB Output is correct
36 Correct 12 ms 57988 KB Output is correct
37 Correct 12 ms 57948 KB Output is correct
38 Correct 11 ms 57692 KB Output is correct
39 Correct 13 ms 57948 KB Output is correct
40 Correct 12 ms 57948 KB Output is correct
41 Incorrect 12 ms 57948 KB Output isn't correct
42 Halted 0 ms 0 KB -