Submission #486580

# Submission time Handle Problem Language Result Execution time Memory
486580 2021-11-12T04:44:21 Z i_am_noob Mergers (JOI19_mergers) C++17
100 / 100
1056 ms 129992 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,tune=native")

#define ll long long
//#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define chkmin(a,b) (a=min(a,b))
#define chkmax(a,b) (a=max(a,b))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define inf 1010000000
//#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;
const int maxn=500005,maxm=7777714,maxk=7777714;
    
//i_am_noob
//#define wiwihorz  
int n,k,id[maxn],dep[maxn],par[maxn][20],siz[maxn],val[maxn],stamp,comp[maxn],deg[maxn];
vector<int> adj[maxn],vec[maxn];

void dfs(int u, int fa){
    id[u]=stamp++;
    siz[u]=1;
    par[u][0]=fa;
    dep[u]=fa==-1?0:dep[fa]+1;
    rep2(i,1,20) par[u][i]=par[u][i-1]==-1?-1:par[par[u][i-1]][i-1];
    for(auto v: adj[u]) if(v!=fa){
        dfs(v,u);
        siz[u]+=siz[v];
    }
}

int lca(int u, int v){
    if(dep[u]>dep[v]) swap(u,v);
    rep(20) if((dep[v]-dep[u])>>i&1) v=par[v][i];
    if(u==v) return u;
    rep3(i,19,0) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
    return par[u][0];
}

void dfs2(int u, int fa){
    for(auto v: adj[u]) if(v!=fa){
        dfs2(v,u);
        val[u]+=val[v];
    }
}

void dfs3(int u, int fa){
    if(fa!=-1&&siz[u]==val[u]){
        comp[u]=++stamp;
        deg[comp[u]]++;
        deg[comp[fa]]++;
    }
    else comp[u]=fa==-1?0:comp[fa];
    for(auto v: adj[u]) if(v!=fa){
        dfs3(v,u);
    }
}

void orzck(){
    cin >> n >> k;
    rep(n-1){
        int u,v;
        cin >> u >> v;
        u--,v--;
        adj[u].pb(v),adj[v].pb(u);
    }
    rep(n){
        int x;
        cin >> x;
        x--;
        vec[x].pb(i);
    }
    dfs(0,-1);
    rep(k) if(sz(vec[i])){
        int x=vec[i][0];
        for(auto u: vec[i]) x=lca(x,u);
        val[x]+=sz(vec[i]);
        /*
        sort(all(vec[i]),[](int i1, int i2){return id[i1]<id[i2];});
        vector<int> stk;
        stk.pb(0);
        val[0]+=sz(vec[i]);
        for(auto u: vec[i]){
            int x=lca(stk.back(),u);
            val[u]+=sz(vec[i]);
            val[x]-=sz(vec[i]);
            while(dep[stk.back()]>dep[x]) stk.pop_back();
            if(stk.back()!=x) stk.pb(x);
            stk.pb(u);
        }
        */
    }
    dfs2(0,-1);
    stamp=0;
    dfs3(0,-1);
    int res=0;
    rep(stamp+1) if(deg[i]==1) res++;
    print((res+1)/2);
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    #ifdef i_am_noob
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    freopen("output2.txt","w",stderr);
    #endif
    cout << fixed << setprecision(15);
    int t;
    #ifdef wiwihorz
    cin >> t;
    #else
    t=1;
    #endif
    while(t--) orzck();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 11 ms 23800 KB Output is correct
4 Correct 11 ms 23768 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 14 ms 23844 KB Output is correct
7 Correct 13 ms 23756 KB Output is correct
8 Correct 15 ms 23748 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 12 ms 23808 KB Output is correct
13 Correct 11 ms 23804 KB Output is correct
14 Correct 11 ms 23848 KB Output is correct
15 Correct 11 ms 23756 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 12 ms 23800 KB Output is correct
18 Correct 12 ms 23804 KB Output is correct
19 Correct 12 ms 23832 KB Output is correct
20 Correct 12 ms 23800 KB Output is correct
21 Correct 11 ms 23756 KB Output is correct
22 Correct 11 ms 23756 KB Output is correct
23 Correct 12 ms 23828 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 11 ms 23800 KB Output is correct
4 Correct 11 ms 23768 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 14 ms 23844 KB Output is correct
7 Correct 13 ms 23756 KB Output is correct
8 Correct 15 ms 23748 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 12 ms 23808 KB Output is correct
13 Correct 11 ms 23804 KB Output is correct
14 Correct 11 ms 23848 KB Output is correct
15 Correct 11 ms 23756 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 12 ms 23800 KB Output is correct
18 Correct 12 ms 23804 KB Output is correct
19 Correct 12 ms 23832 KB Output is correct
20 Correct 12 ms 23800 KB Output is correct
21 Correct 11 ms 23756 KB Output is correct
22 Correct 11 ms 23756 KB Output is correct
23 Correct 12 ms 23828 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 12 ms 23828 KB Output is correct
26 Correct 13 ms 24268 KB Output is correct
27 Correct 13 ms 24308 KB Output is correct
28 Correct 13 ms 24452 KB Output is correct
29 Correct 13 ms 24396 KB Output is correct
30 Correct 13 ms 24316 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 13 ms 24528 KB Output is correct
33 Correct 12 ms 23852 KB Output is correct
34 Correct 14 ms 24236 KB Output is correct
35 Correct 17 ms 24284 KB Output is correct
36 Correct 12 ms 24268 KB Output is correct
37 Correct 13 ms 24328 KB Output is correct
38 Correct 11 ms 23756 KB Output is correct
39 Correct 12 ms 24268 KB Output is correct
40 Correct 12 ms 24268 KB Output is correct
41 Correct 12 ms 24268 KB Output is correct
42 Correct 13 ms 24328 KB Output is correct
43 Correct 15 ms 24500 KB Output is correct
44 Correct 12 ms 23756 KB Output is correct
45 Correct 13 ms 24396 KB Output is correct
46 Correct 13 ms 24268 KB Output is correct
47 Correct 11 ms 23800 KB Output is correct
48 Correct 14 ms 24268 KB Output is correct
49 Correct 14 ms 24328 KB Output is correct
50 Correct 13 ms 24328 KB Output is correct
51 Correct 17 ms 24268 KB Output is correct
52 Correct 14 ms 24284 KB Output is correct
53 Correct 14 ms 24396 KB Output is correct
54 Correct 13 ms 24272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 11 ms 23800 KB Output is correct
4 Correct 11 ms 23768 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 14 ms 23844 KB Output is correct
7 Correct 13 ms 23756 KB Output is correct
8 Correct 15 ms 23748 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 12 ms 23808 KB Output is correct
13 Correct 11 ms 23804 KB Output is correct
14 Correct 11 ms 23848 KB Output is correct
15 Correct 11 ms 23756 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 12 ms 23800 KB Output is correct
18 Correct 12 ms 23804 KB Output is correct
19 Correct 12 ms 23832 KB Output is correct
20 Correct 12 ms 23800 KB Output is correct
21 Correct 11 ms 23756 KB Output is correct
22 Correct 11 ms 23756 KB Output is correct
23 Correct 12 ms 23828 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 11 ms 23868 KB Output is correct
26 Correct 60 ms 38184 KB Output is correct
27 Correct 95 ms 38224 KB Output is correct
28 Correct 16 ms 24340 KB Output is correct
29 Correct 12 ms 23756 KB Output is correct
30 Correct 11 ms 23756 KB Output is correct
31 Correct 82 ms 38108 KB Output is correct
32 Correct 13 ms 24312 KB Output is correct
33 Correct 135 ms 45440 KB Output is correct
34 Correct 99 ms 38248 KB Output is correct
35 Correct 14 ms 24268 KB Output is correct
36 Correct 116 ms 38848 KB Output is correct
37 Correct 13 ms 24204 KB Output is correct
38 Correct 13 ms 24252 KB Output is correct
39 Correct 95 ms 38160 KB Output is correct
40 Correct 13 ms 24524 KB Output is correct
41 Correct 107 ms 38124 KB Output is correct
42 Correct 118 ms 40572 KB Output is correct
43 Correct 11 ms 23756 KB Output is correct
44 Correct 125 ms 45680 KB Output is correct
45 Correct 156 ms 42004 KB Output is correct
46 Correct 13 ms 24268 KB Output is correct
47 Correct 13 ms 24268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 38184 KB Output is correct
2 Correct 73 ms 41444 KB Output is correct
3 Correct 13 ms 24268 KB Output is correct
4 Correct 13 ms 24268 KB Output is correct
5 Correct 11 ms 23860 KB Output is correct
6 Correct 11 ms 23808 KB Output is correct
7 Correct 12 ms 24396 KB Output is correct
8 Correct 108 ms 39372 KB Output is correct
9 Correct 14 ms 24288 KB Output is correct
10 Correct 84 ms 38244 KB Output is correct
11 Correct 12 ms 23756 KB Output is correct
12 Correct 105 ms 38168 KB Output is correct
13 Correct 98 ms 39208 KB Output is correct
14 Correct 98 ms 41156 KB Output is correct
15 Correct 72 ms 38048 KB Output is correct
16 Correct 17 ms 24332 KB Output is correct
17 Correct 12 ms 23852 KB Output is correct
18 Correct 72 ms 41100 KB Output is correct
19 Correct 129 ms 45128 KB Output is correct
20 Correct 12 ms 24268 KB Output is correct
21 Correct 12 ms 23844 KB Output is correct
22 Correct 73 ms 39572 KB Output is correct
23 Correct 13 ms 24200 KB Output is correct
24 Correct 103 ms 38580 KB Output is correct
25 Correct 108 ms 43776 KB Output is correct
26 Correct 14 ms 24336 KB Output is correct
27 Correct 13 ms 24348 KB Output is correct
28 Correct 13 ms 24320 KB Output is correct
29 Correct 15 ms 24268 KB Output is correct
30 Correct 12 ms 24248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 11 ms 23800 KB Output is correct
4 Correct 11 ms 23768 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 14 ms 23844 KB Output is correct
7 Correct 13 ms 23756 KB Output is correct
8 Correct 15 ms 23748 KB Output is correct
9 Correct 12 ms 23732 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 11 ms 23756 KB Output is correct
12 Correct 12 ms 23808 KB Output is correct
13 Correct 11 ms 23804 KB Output is correct
14 Correct 11 ms 23848 KB Output is correct
15 Correct 11 ms 23756 KB Output is correct
16 Correct 11 ms 23764 KB Output is correct
17 Correct 12 ms 23800 KB Output is correct
18 Correct 12 ms 23804 KB Output is correct
19 Correct 12 ms 23832 KB Output is correct
20 Correct 12 ms 23800 KB Output is correct
21 Correct 11 ms 23756 KB Output is correct
22 Correct 11 ms 23756 KB Output is correct
23 Correct 12 ms 23828 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 12 ms 23828 KB Output is correct
26 Correct 13 ms 24268 KB Output is correct
27 Correct 13 ms 24308 KB Output is correct
28 Correct 13 ms 24452 KB Output is correct
29 Correct 13 ms 24396 KB Output is correct
30 Correct 13 ms 24316 KB Output is correct
31 Correct 12 ms 23764 KB Output is correct
32 Correct 13 ms 24528 KB Output is correct
33 Correct 12 ms 23852 KB Output is correct
34 Correct 14 ms 24236 KB Output is correct
35 Correct 17 ms 24284 KB Output is correct
36 Correct 12 ms 24268 KB Output is correct
37 Correct 13 ms 24328 KB Output is correct
38 Correct 11 ms 23756 KB Output is correct
39 Correct 12 ms 24268 KB Output is correct
40 Correct 12 ms 24268 KB Output is correct
41 Correct 12 ms 24268 KB Output is correct
42 Correct 13 ms 24328 KB Output is correct
43 Correct 15 ms 24500 KB Output is correct
44 Correct 12 ms 23756 KB Output is correct
45 Correct 13 ms 24396 KB Output is correct
46 Correct 13 ms 24268 KB Output is correct
47 Correct 11 ms 23800 KB Output is correct
48 Correct 14 ms 24268 KB Output is correct
49 Correct 14 ms 24328 KB Output is correct
50 Correct 13 ms 24328 KB Output is correct
51 Correct 17 ms 24268 KB Output is correct
52 Correct 14 ms 24284 KB Output is correct
53 Correct 14 ms 24396 KB Output is correct
54 Correct 13 ms 24272 KB Output is correct
55 Correct 11 ms 23868 KB Output is correct
56 Correct 60 ms 38184 KB Output is correct
57 Correct 95 ms 38224 KB Output is correct
58 Correct 16 ms 24340 KB Output is correct
59 Correct 12 ms 23756 KB Output is correct
60 Correct 11 ms 23756 KB Output is correct
61 Correct 82 ms 38108 KB Output is correct
62 Correct 13 ms 24312 KB Output is correct
63 Correct 135 ms 45440 KB Output is correct
64 Correct 99 ms 38248 KB Output is correct
65 Correct 14 ms 24268 KB Output is correct
66 Correct 116 ms 38848 KB Output is correct
67 Correct 13 ms 24204 KB Output is correct
68 Correct 13 ms 24252 KB Output is correct
69 Correct 95 ms 38160 KB Output is correct
70 Correct 13 ms 24524 KB Output is correct
71 Correct 107 ms 38124 KB Output is correct
72 Correct 118 ms 40572 KB Output is correct
73 Correct 11 ms 23756 KB Output is correct
74 Correct 125 ms 45680 KB Output is correct
75 Correct 156 ms 42004 KB Output is correct
76 Correct 13 ms 24268 KB Output is correct
77 Correct 13 ms 24268 KB Output is correct
78 Correct 59 ms 38184 KB Output is correct
79 Correct 73 ms 41444 KB Output is correct
80 Correct 13 ms 24268 KB Output is correct
81 Correct 13 ms 24268 KB Output is correct
82 Correct 11 ms 23860 KB Output is correct
83 Correct 11 ms 23808 KB Output is correct
84 Correct 12 ms 24396 KB Output is correct
85 Correct 108 ms 39372 KB Output is correct
86 Correct 14 ms 24288 KB Output is correct
87 Correct 84 ms 38244 KB Output is correct
88 Correct 12 ms 23756 KB Output is correct
89 Correct 105 ms 38168 KB Output is correct
90 Correct 98 ms 39208 KB Output is correct
91 Correct 98 ms 41156 KB Output is correct
92 Correct 72 ms 38048 KB Output is correct
93 Correct 17 ms 24332 KB Output is correct
94 Correct 12 ms 23852 KB Output is correct
95 Correct 72 ms 41100 KB Output is correct
96 Correct 129 ms 45128 KB Output is correct
97 Correct 12 ms 24268 KB Output is correct
98 Correct 12 ms 23844 KB Output is correct
99 Correct 73 ms 39572 KB Output is correct
100 Correct 13 ms 24200 KB Output is correct
101 Correct 103 ms 38580 KB Output is correct
102 Correct 108 ms 43776 KB Output is correct
103 Correct 14 ms 24336 KB Output is correct
104 Correct 13 ms 24348 KB Output is correct
105 Correct 13 ms 24320 KB Output is correct
106 Correct 15 ms 24268 KB Output is correct
107 Correct 12 ms 24248 KB Output is correct
108 Correct 501 ms 97784 KB Output is correct
109 Correct 999 ms 109680 KB Output is correct
110 Correct 1056 ms 120824 KB Output is correct
111 Correct 996 ms 129992 KB Output is correct
112 Correct 988 ms 119236 KB Output is correct
113 Correct 570 ms 106464 KB Output is correct
114 Correct 642 ms 91612 KB Output is correct
115 Correct 600 ms 91476 KB Output is correct
116 Correct 736 ms 94188 KB Output is correct
117 Correct 742 ms 106952 KB Output is correct
118 Correct 625 ms 91288 KB Output is correct
119 Correct 730 ms 106908 KB Output is correct
120 Correct 951 ms 120840 KB Output is correct
121 Correct 728 ms 106896 KB Output is correct
122 Correct 812 ms 98840 KB Output is correct
123 Correct 565 ms 108004 KB Output is correct
124 Correct 940 ms 104228 KB Output is correct