답안 #486579

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486579 2021-11-12T04:42:57 Z i_am_noob Mergers (JOI19_mergers) C++17
100 / 100
1071 ms 190160 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 12 ms 23864 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 12 ms 23836 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23804 KB Output is correct
7 Correct 12 ms 23808 KB Output is correct
8 Correct 13 ms 23884 KB Output is correct
9 Correct 15 ms 23936 KB Output is correct
10 Correct 13 ms 23828 KB Output is correct
11 Correct 15 ms 23884 KB Output is correct
12 Correct 13 ms 23884 KB Output is correct
13 Correct 12 ms 23752 KB Output is correct
14 Correct 12 ms 23768 KB Output is correct
15 Correct 12 ms 23808 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 12 ms 23772 KB Output is correct
19 Correct 12 ms 23804 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 12 ms 23884 KB Output is correct
22 Correct 12 ms 23884 KB Output is correct
23 Correct 12 ms 23756 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 12 ms 23864 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 12 ms 23836 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23804 KB Output is correct
7 Correct 12 ms 23808 KB Output is correct
8 Correct 13 ms 23884 KB Output is correct
9 Correct 15 ms 23936 KB Output is correct
10 Correct 13 ms 23828 KB Output is correct
11 Correct 15 ms 23884 KB Output is correct
12 Correct 13 ms 23884 KB Output is correct
13 Correct 12 ms 23752 KB Output is correct
14 Correct 12 ms 23768 KB Output is correct
15 Correct 12 ms 23808 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 12 ms 23772 KB Output is correct
19 Correct 12 ms 23804 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 12 ms 23884 KB Output is correct
22 Correct 12 ms 23884 KB Output is correct
23 Correct 12 ms 23756 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23756 KB Output is correct
26 Correct 13 ms 24600 KB Output is correct
27 Correct 13 ms 24576 KB Output is correct
28 Correct 14 ms 24780 KB Output is correct
29 Correct 14 ms 24600 KB Output is correct
30 Correct 14 ms 24596 KB Output is correct
31 Correct 12 ms 23756 KB Output is correct
32 Correct 14 ms 24844 KB Output is correct
33 Correct 14 ms 23808 KB Output is correct
34 Correct 14 ms 24524 KB Output is correct
35 Correct 15 ms 24652 KB Output is correct
36 Correct 14 ms 24584 KB Output is correct
37 Correct 14 ms 24616 KB Output is correct
38 Correct 13 ms 23756 KB Output is correct
39 Correct 14 ms 24536 KB Output is correct
40 Correct 14 ms 24652 KB Output is correct
41 Correct 14 ms 24624 KB Output is correct
42 Correct 13 ms 24652 KB Output is correct
43 Correct 14 ms 24780 KB Output is correct
44 Correct 12 ms 23780 KB Output is correct
45 Correct 14 ms 24652 KB Output is correct
46 Correct 14 ms 24588 KB Output is correct
47 Correct 12 ms 23756 KB Output is correct
48 Correct 14 ms 24652 KB Output is correct
49 Correct 15 ms 24636 KB Output is correct
50 Correct 13 ms 24780 KB Output is correct
51 Correct 14 ms 24652 KB Output is correct
52 Correct 13 ms 24524 KB Output is correct
53 Correct 14 ms 24608 KB Output is correct
54 Correct 14 ms 24532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 12 ms 23864 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 12 ms 23836 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23804 KB Output is correct
7 Correct 12 ms 23808 KB Output is correct
8 Correct 13 ms 23884 KB Output is correct
9 Correct 15 ms 23936 KB Output is correct
10 Correct 13 ms 23828 KB Output is correct
11 Correct 15 ms 23884 KB Output is correct
12 Correct 13 ms 23884 KB Output is correct
13 Correct 12 ms 23752 KB Output is correct
14 Correct 12 ms 23768 KB Output is correct
15 Correct 12 ms 23808 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 12 ms 23772 KB Output is correct
19 Correct 12 ms 23804 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 12 ms 23884 KB Output is correct
22 Correct 12 ms 23884 KB Output is correct
23 Correct 12 ms 23756 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23836 KB Output is correct
26 Correct 95 ms 49004 KB Output is correct
27 Correct 107 ms 49628 KB Output is correct
28 Correct 14 ms 24584 KB Output is correct
29 Correct 12 ms 23756 KB Output is correct
30 Correct 13 ms 23756 KB Output is correct
31 Correct 96 ms 49512 KB Output is correct
32 Correct 14 ms 24588 KB Output is correct
33 Correct 143 ms 56224 KB Output is correct
34 Correct 112 ms 49752 KB Output is correct
35 Correct 14 ms 24572 KB Output is correct
36 Correct 127 ms 49732 KB Output is correct
37 Correct 14 ms 24588 KB Output is correct
38 Correct 13 ms 24584 KB Output is correct
39 Correct 70 ms 48880 KB Output is correct
40 Correct 14 ms 24780 KB Output is correct
41 Correct 89 ms 49300 KB Output is correct
42 Correct 136 ms 51332 KB Output is correct
43 Correct 12 ms 23756 KB Output is correct
44 Correct 142 ms 56312 KB Output is correct
45 Correct 151 ms 53960 KB Output is correct
46 Correct 14 ms 24636 KB Output is correct
47 Correct 15 ms 24524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 49092 KB Output is correct
2 Correct 78 ms 52892 KB Output is correct
3 Correct 14 ms 24652 KB Output is correct
4 Correct 14 ms 24532 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23812 KB Output is correct
7 Correct 13 ms 24588 KB Output is correct
8 Correct 116 ms 50688 KB Output is correct
9 Correct 14 ms 24524 KB Output is correct
10 Correct 99 ms 49800 KB Output is correct
11 Correct 12 ms 23756 KB Output is correct
12 Correct 120 ms 49256 KB Output is correct
13 Correct 114 ms 50692 KB Output is correct
14 Correct 99 ms 52676 KB Output is correct
15 Correct 69 ms 48996 KB Output is correct
16 Correct 14 ms 24592 KB Output is correct
17 Correct 12 ms 23808 KB Output is correct
18 Correct 85 ms 52452 KB Output is correct
19 Correct 138 ms 56132 KB Output is correct
20 Correct 14 ms 24532 KB Output is correct
21 Correct 12 ms 23756 KB Output is correct
22 Correct 79 ms 50872 KB Output is correct
23 Correct 14 ms 24608 KB Output is correct
24 Correct 134 ms 49988 KB Output is correct
25 Correct 124 ms 55616 KB Output is correct
26 Correct 15 ms 24780 KB Output is correct
27 Correct 14 ms 24752 KB Output is correct
28 Correct 14 ms 24632 KB Output is correct
29 Correct 13 ms 24652 KB Output is correct
30 Correct 14 ms 24652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 12 ms 23864 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 12 ms 23836 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23804 KB Output is correct
7 Correct 12 ms 23808 KB Output is correct
8 Correct 13 ms 23884 KB Output is correct
9 Correct 15 ms 23936 KB Output is correct
10 Correct 13 ms 23828 KB Output is correct
11 Correct 15 ms 23884 KB Output is correct
12 Correct 13 ms 23884 KB Output is correct
13 Correct 12 ms 23752 KB Output is correct
14 Correct 12 ms 23768 KB Output is correct
15 Correct 12 ms 23808 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 12 ms 23772 KB Output is correct
19 Correct 12 ms 23804 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 12 ms 23884 KB Output is correct
22 Correct 12 ms 23884 KB Output is correct
23 Correct 12 ms 23756 KB Output is correct
24 Correct 15 ms 23740 KB Output is correct
25 Correct 12 ms 23756 KB Output is correct
26 Correct 13 ms 24600 KB Output is correct
27 Correct 13 ms 24576 KB Output is correct
28 Correct 14 ms 24780 KB Output is correct
29 Correct 14 ms 24600 KB Output is correct
30 Correct 14 ms 24596 KB Output is correct
31 Correct 12 ms 23756 KB Output is correct
32 Correct 14 ms 24844 KB Output is correct
33 Correct 14 ms 23808 KB Output is correct
34 Correct 14 ms 24524 KB Output is correct
35 Correct 15 ms 24652 KB Output is correct
36 Correct 14 ms 24584 KB Output is correct
37 Correct 14 ms 24616 KB Output is correct
38 Correct 13 ms 23756 KB Output is correct
39 Correct 14 ms 24536 KB Output is correct
40 Correct 14 ms 24652 KB Output is correct
41 Correct 14 ms 24624 KB Output is correct
42 Correct 13 ms 24652 KB Output is correct
43 Correct 14 ms 24780 KB Output is correct
44 Correct 12 ms 23780 KB Output is correct
45 Correct 14 ms 24652 KB Output is correct
46 Correct 14 ms 24588 KB Output is correct
47 Correct 12 ms 23756 KB Output is correct
48 Correct 14 ms 24652 KB Output is correct
49 Correct 15 ms 24636 KB Output is correct
50 Correct 13 ms 24780 KB Output is correct
51 Correct 14 ms 24652 KB Output is correct
52 Correct 13 ms 24524 KB Output is correct
53 Correct 14 ms 24608 KB Output is correct
54 Correct 14 ms 24532 KB Output is correct
55 Correct 12 ms 23836 KB Output is correct
56 Correct 95 ms 49004 KB Output is correct
57 Correct 107 ms 49628 KB Output is correct
58 Correct 14 ms 24584 KB Output is correct
59 Correct 12 ms 23756 KB Output is correct
60 Correct 13 ms 23756 KB Output is correct
61 Correct 96 ms 49512 KB Output is correct
62 Correct 14 ms 24588 KB Output is correct
63 Correct 143 ms 56224 KB Output is correct
64 Correct 112 ms 49752 KB Output is correct
65 Correct 14 ms 24572 KB Output is correct
66 Correct 127 ms 49732 KB Output is correct
67 Correct 14 ms 24588 KB Output is correct
68 Correct 13 ms 24584 KB Output is correct
69 Correct 70 ms 48880 KB Output is correct
70 Correct 14 ms 24780 KB Output is correct
71 Correct 89 ms 49300 KB Output is correct
72 Correct 136 ms 51332 KB Output is correct
73 Correct 12 ms 23756 KB Output is correct
74 Correct 142 ms 56312 KB Output is correct
75 Correct 151 ms 53960 KB Output is correct
76 Correct 14 ms 24636 KB Output is correct
77 Correct 15 ms 24524 KB Output is correct
78 Correct 72 ms 49092 KB Output is correct
79 Correct 78 ms 52892 KB Output is correct
80 Correct 14 ms 24652 KB Output is correct
81 Correct 14 ms 24532 KB Output is correct
82 Correct 12 ms 23756 KB Output is correct
83 Correct 12 ms 23812 KB Output is correct
84 Correct 13 ms 24588 KB Output is correct
85 Correct 116 ms 50688 KB Output is correct
86 Correct 14 ms 24524 KB Output is correct
87 Correct 99 ms 49800 KB Output is correct
88 Correct 12 ms 23756 KB Output is correct
89 Correct 120 ms 49256 KB Output is correct
90 Correct 114 ms 50692 KB Output is correct
91 Correct 99 ms 52676 KB Output is correct
92 Correct 69 ms 48996 KB Output is correct
93 Correct 14 ms 24592 KB Output is correct
94 Correct 12 ms 23808 KB Output is correct
95 Correct 85 ms 52452 KB Output is correct
96 Correct 138 ms 56132 KB Output is correct
97 Correct 14 ms 24532 KB Output is correct
98 Correct 12 ms 23756 KB Output is correct
99 Correct 79 ms 50872 KB Output is correct
100 Correct 14 ms 24608 KB Output is correct
101 Correct 134 ms 49988 KB Output is correct
102 Correct 124 ms 55616 KB Output is correct
103 Correct 15 ms 24780 KB Output is correct
104 Correct 14 ms 24752 KB Output is correct
105 Correct 14 ms 24632 KB Output is correct
106 Correct 13 ms 24652 KB Output is correct
107 Correct 14 ms 24652 KB Output is correct
108 Correct 511 ms 160068 KB Output is correct
109 Correct 1043 ms 167404 KB Output is correct
110 Correct 1071 ms 178796 KB Output is correct
111 Correct 1052 ms 190160 KB Output is correct
112 Correct 1028 ms 182848 KB Output is correct
113 Correct 552 ms 168604 KB Output is correct
114 Correct 681 ms 151940 KB Output is correct
115 Correct 683 ms 151912 KB Output is correct
116 Correct 839 ms 157344 KB Output is correct
117 Correct 779 ms 169924 KB Output is correct
118 Correct 655 ms 152628 KB Output is correct
119 Correct 791 ms 169760 KB Output is correct
120 Correct 1064 ms 185368 KB Output is correct
121 Correct 805 ms 169780 KB Output is correct
122 Correct 846 ms 161032 KB Output is correct
123 Correct 540 ms 170852 KB Output is correct
124 Correct 974 ms 163180 KB Output is correct