Submission #799781

# Submission time Handle Problem Language Result Execution time Memory
799781 2023-08-01T02:20:38 Z Alish Mergers (JOI19_mergers) C++14
100 / 100
1116 ms 145464 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long int	ll;
typedef long double	ld;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;


#define F		        first
#define S		        second
#define pb		        push_back
#define endl            '\n'
#define Mp		        make_pair
#define all(x)          x.begin(), x.end()
#define debug(x)        cerr << #x << " = " << x << endl
#define set_dec(x)	    cout << fixed << setprecision(x);
#define fast_io         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io         freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);


ll mod = 998244353;

ll power(ll a, ll b)
{
    return (!b ? 1 : (b & 1 ? a * power(a * a % mod, b / 2) % mod : power(a * a % mod, b / 2) % mod));
}

const int N = 5e5+23, L =23 ;

vector<int>g[N], buc[N];
int st[N], h[N], par[N][L], sz[N], sum[N], is[N];
int ans;
int n , k ;
int a[N];

int tim;
void dfs(int v, int p=0){
    sz[v]=1;
    st[v]=++tim;
    for (int u: g[v]) {
        if(u==p)continue;
        h[u]=h[v]+1;
        par[u][0]=v;
        dfs(u,v);
        sz[v]+=sz[u];
    }
}

void DFS(int v, int p=0){

    int temp=0;
    for (int u: g[v]){
        if(u==p) continue;
        DFS(u,v);
        is[v]+=is[u];
        sum[v]+=sum[u];
    }

    if(sum[v]==sz[v]){
        if(is[v]==0 && v!=1)ans++;
        if(is[v]==1 && v==1) ans++;
        is[v]=1;
    }
}

int LCA(int v, int u){

    if(h[v]>h[u])swap(u,v);

    int d =h[u]-h[v];
    for (int i=L-1; i>=0; i--) {
        if(d>>i&1) u=par[u][i];
    }

    if(u==v) return v;

    for (int i=L-1; i>=0; i--){
        if(par[u][i]!=par[v][i]){
            v=par[v][i];
            u=par[u][i];
        }
    }

    return par[v][0];

}

int main()
{

    cin>>n>>k;
    for (int i=0; i<n-1; i++){
        int u , v; cin>>v>>u;
        g[v].pb(u);
        g[u].pb(v);
    }
    for (int i=1; i<=n; i++){
        cin>>a[i];
        buc[a[i]].pb(i);
    }

    dfs(1);

    for (int j=1; j<L; j++){
        for (int i=1; i<=n; i++){
            par[i][j]=par[par[i][j-1]][j-1];
        }
    }

    for (int w=1; w<=k; w++){
        vector<pii> vec;
        for (int i: buc[w]) vec.pb({st[i], i});
        sort(all(vec));
        int lca=LCA(vec[0].S, vec.back().S);
        sum[lca]+=buc[w].size();
    }

    DFS(1);

    //cout<<ans<<" ";
    cout<<(ans+1)/2<<endl;

}

Compilation message

mergers.cpp: In function 'void DFS(int, int)':
mergers.cpp:53:9: warning: unused variable 'temp' [-Wunused-variable]
   53 |     int temp=0;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23852 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23804 KB Output is correct
5 Correct 14 ms 23796 KB Output is correct
6 Correct 11 ms 23844 KB Output is correct
7 Correct 12 ms 23796 KB Output is correct
8 Correct 11 ms 23792 KB Output is correct
9 Correct 11 ms 23752 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23856 KB Output is correct
15 Correct 12 ms 23792 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23796 KB Output is correct
20 Correct 12 ms 23880 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 11 ms 23792 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23852 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23804 KB Output is correct
5 Correct 14 ms 23796 KB Output is correct
6 Correct 11 ms 23844 KB Output is correct
7 Correct 12 ms 23796 KB Output is correct
8 Correct 11 ms 23792 KB Output is correct
9 Correct 11 ms 23752 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23856 KB Output is correct
15 Correct 12 ms 23792 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23796 KB Output is correct
20 Correct 12 ms 23880 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 11 ms 23792 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23740 KB Output is correct
26 Correct 13 ms 24360 KB Output is correct
27 Correct 13 ms 24280 KB Output is correct
28 Correct 16 ms 24452 KB Output is correct
29 Correct 13 ms 24404 KB Output is correct
30 Correct 15 ms 24276 KB Output is correct
31 Correct 11 ms 23764 KB Output is correct
32 Correct 13 ms 24532 KB Output is correct
33 Correct 12 ms 23796 KB Output is correct
34 Correct 12 ms 24252 KB Output is correct
35 Correct 13 ms 24404 KB Output is correct
36 Correct 14 ms 24328 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 11 ms 23800 KB Output is correct
39 Correct 13 ms 24316 KB Output is correct
40 Correct 13 ms 24360 KB Output is correct
41 Correct 13 ms 24276 KB Output is correct
42 Correct 13 ms 24276 KB Output is correct
43 Correct 13 ms 24540 KB Output is correct
44 Correct 11 ms 23796 KB Output is correct
45 Correct 13 ms 24404 KB Output is correct
46 Correct 15 ms 24368 KB Output is correct
47 Correct 11 ms 23764 KB Output is correct
48 Correct 13 ms 24336 KB Output is correct
49 Correct 12 ms 24404 KB Output is correct
50 Correct 14 ms 24400 KB Output is correct
51 Correct 14 ms 24320 KB Output is correct
52 Correct 13 ms 24232 KB Output is correct
53 Correct 15 ms 24276 KB Output is correct
54 Correct 15 ms 24268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23852 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23804 KB Output is correct
5 Correct 14 ms 23796 KB Output is correct
6 Correct 11 ms 23844 KB Output is correct
7 Correct 12 ms 23796 KB Output is correct
8 Correct 11 ms 23792 KB Output is correct
9 Correct 11 ms 23752 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23856 KB Output is correct
15 Correct 12 ms 23792 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23796 KB Output is correct
20 Correct 12 ms 23880 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 11 ms 23792 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 10 ms 23820 KB Output is correct
26 Correct 89 ms 39880 KB Output is correct
27 Correct 99 ms 40340 KB Output is correct
28 Correct 13 ms 24276 KB Output is correct
29 Correct 11 ms 23832 KB Output is correct
30 Correct 11 ms 23800 KB Output is correct
31 Correct 129 ms 40320 KB Output is correct
32 Correct 13 ms 24276 KB Output is correct
33 Correct 117 ms 47564 KB Output is correct
34 Correct 100 ms 40432 KB Output is correct
35 Correct 13 ms 24220 KB Output is correct
36 Correct 106 ms 41096 KB Output is correct
37 Correct 13 ms 24276 KB Output is correct
38 Correct 13 ms 24276 KB Output is correct
39 Correct 95 ms 39876 KB Output is correct
40 Correct 14 ms 24528 KB Output is correct
41 Correct 102 ms 40500 KB Output is correct
42 Correct 127 ms 42700 KB Output is correct
43 Correct 11 ms 23800 KB Output is correct
44 Correct 138 ms 47996 KB Output is correct
45 Correct 124 ms 44228 KB Output is correct
46 Correct 13 ms 24276 KB Output is correct
47 Correct 14 ms 24344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 38424 KB Output is correct
2 Correct 113 ms 41724 KB Output is correct
3 Correct 13 ms 24276 KB Output is correct
4 Correct 14 ms 24316 KB Output is correct
5 Correct 10 ms 23752 KB Output is correct
6 Correct 11 ms 23764 KB Output is correct
7 Correct 13 ms 24404 KB Output is correct
8 Correct 132 ms 41728 KB Output is correct
9 Correct 13 ms 24276 KB Output is correct
10 Correct 100 ms 40536 KB Output is correct
11 Correct 11 ms 23860 KB Output is correct
12 Correct 105 ms 40380 KB Output is correct
13 Correct 120 ms 41584 KB Output is correct
14 Correct 122 ms 43176 KB Output is correct
15 Correct 92 ms 39876 KB Output is correct
16 Correct 17 ms 24324 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 113 ms 43204 KB Output is correct
19 Correct 138 ms 47160 KB Output is correct
20 Correct 13 ms 24276 KB Output is correct
21 Correct 14 ms 23872 KB Output is correct
22 Correct 107 ms 41888 KB Output is correct
23 Correct 13 ms 24276 KB Output is correct
24 Correct 111 ms 40844 KB Output is correct
25 Correct 133 ms 45772 KB Output is correct
26 Correct 14 ms 24332 KB Output is correct
27 Correct 14 ms 24404 KB Output is correct
28 Correct 14 ms 24276 KB Output is correct
29 Correct 13 ms 24276 KB Output is correct
30 Correct 13 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23852 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23804 KB Output is correct
5 Correct 14 ms 23796 KB Output is correct
6 Correct 11 ms 23844 KB Output is correct
7 Correct 12 ms 23796 KB Output is correct
8 Correct 11 ms 23792 KB Output is correct
9 Correct 11 ms 23752 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23856 KB Output is correct
15 Correct 12 ms 23792 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23796 KB Output is correct
20 Correct 12 ms 23880 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 11 ms 23792 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23740 KB Output is correct
26 Correct 13 ms 24360 KB Output is correct
27 Correct 13 ms 24280 KB Output is correct
28 Correct 16 ms 24452 KB Output is correct
29 Correct 13 ms 24404 KB Output is correct
30 Correct 15 ms 24276 KB Output is correct
31 Correct 11 ms 23764 KB Output is correct
32 Correct 13 ms 24532 KB Output is correct
33 Correct 12 ms 23796 KB Output is correct
34 Correct 12 ms 24252 KB Output is correct
35 Correct 13 ms 24404 KB Output is correct
36 Correct 14 ms 24328 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 11 ms 23800 KB Output is correct
39 Correct 13 ms 24316 KB Output is correct
40 Correct 13 ms 24360 KB Output is correct
41 Correct 13 ms 24276 KB Output is correct
42 Correct 13 ms 24276 KB Output is correct
43 Correct 13 ms 24540 KB Output is correct
44 Correct 11 ms 23796 KB Output is correct
45 Correct 13 ms 24404 KB Output is correct
46 Correct 15 ms 24368 KB Output is correct
47 Correct 11 ms 23764 KB Output is correct
48 Correct 13 ms 24336 KB Output is correct
49 Correct 12 ms 24404 KB Output is correct
50 Correct 14 ms 24400 KB Output is correct
51 Correct 14 ms 24320 KB Output is correct
52 Correct 13 ms 24232 KB Output is correct
53 Correct 15 ms 24276 KB Output is correct
54 Correct 15 ms 24268 KB Output is correct
55 Correct 10 ms 23820 KB Output is correct
56 Correct 89 ms 39880 KB Output is correct
57 Correct 99 ms 40340 KB Output is correct
58 Correct 13 ms 24276 KB Output is correct
59 Correct 11 ms 23832 KB Output is correct
60 Correct 11 ms 23800 KB Output is correct
61 Correct 129 ms 40320 KB Output is correct
62 Correct 13 ms 24276 KB Output is correct
63 Correct 117 ms 47564 KB Output is correct
64 Correct 100 ms 40432 KB Output is correct
65 Correct 13 ms 24220 KB Output is correct
66 Correct 106 ms 41096 KB Output is correct
67 Correct 13 ms 24276 KB Output is correct
68 Correct 13 ms 24276 KB Output is correct
69 Correct 95 ms 39876 KB Output is correct
70 Correct 14 ms 24528 KB Output is correct
71 Correct 102 ms 40500 KB Output is correct
72 Correct 127 ms 42700 KB Output is correct
73 Correct 11 ms 23800 KB Output is correct
74 Correct 138 ms 47996 KB Output is correct
75 Correct 124 ms 44228 KB Output is correct
76 Correct 13 ms 24276 KB Output is correct
77 Correct 14 ms 24344 KB Output is correct
78 Correct 90 ms 38424 KB Output is correct
79 Correct 113 ms 41724 KB Output is correct
80 Correct 13 ms 24276 KB Output is correct
81 Correct 14 ms 24316 KB Output is correct
82 Correct 10 ms 23752 KB Output is correct
83 Correct 11 ms 23764 KB Output is correct
84 Correct 13 ms 24404 KB Output is correct
85 Correct 132 ms 41728 KB Output is correct
86 Correct 13 ms 24276 KB Output is correct
87 Correct 100 ms 40536 KB Output is correct
88 Correct 11 ms 23860 KB Output is correct
89 Correct 105 ms 40380 KB Output is correct
90 Correct 120 ms 41584 KB Output is correct
91 Correct 122 ms 43176 KB Output is correct
92 Correct 92 ms 39876 KB Output is correct
93 Correct 17 ms 24324 KB Output is correct
94 Correct 12 ms 23764 KB Output is correct
95 Correct 113 ms 43204 KB Output is correct
96 Correct 138 ms 47160 KB Output is correct
97 Correct 13 ms 24276 KB Output is correct
98 Correct 14 ms 23872 KB Output is correct
99 Correct 107 ms 41888 KB Output is correct
100 Correct 13 ms 24276 KB Output is correct
101 Correct 111 ms 40844 KB Output is correct
102 Correct 133 ms 45772 KB Output is correct
103 Correct 14 ms 24332 KB Output is correct
104 Correct 14 ms 24404 KB Output is correct
105 Correct 14 ms 24276 KB Output is correct
106 Correct 13 ms 24276 KB Output is correct
107 Correct 13 ms 24276 KB Output is correct
108 Correct 670 ms 114412 KB Output is correct
109 Correct 936 ms 124432 KB Output is correct
110 Correct 939 ms 135852 KB Output is correct
111 Correct 1116 ms 145464 KB Output is correct
112 Correct 988 ms 135588 KB Output is correct
113 Correct 753 ms 122264 KB Output is correct
114 Correct 703 ms 106432 KB Output is correct
115 Correct 714 ms 106584 KB Output is correct
116 Correct 889 ms 111136 KB Output is correct
117 Correct 899 ms 122304 KB Output is correct
118 Correct 762 ms 106816 KB Output is correct
119 Correct 928 ms 122432 KB Output is correct
120 Correct 1026 ms 136284 KB Output is correct
121 Correct 895 ms 122336 KB Output is correct
122 Correct 939 ms 116044 KB Output is correct
123 Correct 764 ms 123828 KB Output is correct
124 Correct 1041 ms 120208 KB Output is correct