Submission #563189

# Submission time Handle Problem Language Result Execution time Memory
563189 2022-05-16T13:15:22 Z zaneyu Mergers (JOI19_mergers) C++14
100 / 100
712 ms 82368 KB
/*input
2 2
1 2
1
2
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=998244353;
const ld PI=acos(-1.0L);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f<<' '<<P.s;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]+1<<((i!=sz(V)-1)?" ":"\n");
    return out;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mult(ll a,ll b,ll mod){
    ll res=0;
    while(b){
        if(b&1) res=(res+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return res;
}
ll mypow(ll a,ll b,ll mod){
    if(b<=0) return 1;
    a%=mod;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int par[maxn],dep[maxn];
vector<int> v[maxn],comp[maxn];
struct UFDS{
    int par[maxn];
    void init(int n){
        REP(i,n) par[i]=i;
    }
    void merge(int a,int b){
        a=find(a),b=find(b);
        if(a==b) return;
        par[a]=b;
    }
    int find(int u){
        if(par[u]==u) return u;
        return par[u]=find(par[u]);
    }
}uf;
void dfs(int u,int p){
    par[u]=p;
    for(int x:v[u]){
        if(x==p) continue;
        dep[x]=dep[u]+1;
        dfs(x,u);
    }
}
void merge(int a,int b){
    a=uf.find(a),b=uf.find(b);
    while(a!=b){
        if(dep[a]>=dep[b]){
            uf.merge(a,par[a]);
            a=uf.find(a);
        }
        else{
            uf.merge(b,par[b]);
            b=uf.find(b);
        }
    }
}
int cnt[maxn];
int32_t main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,k;
    cin>>n>>k;
    REP(i,n-1){
        int a,b;
        cin>>a>>b;
        --a,--b;
        v[a].pb(b),v[b].pb(a);
    }
    dfs(0,-1);
    REP(i,n){
        int x;
        cin>>x;
        --x;
        comp[x].pb(i);
    }
    uf.init(n);
    REP(i,n){
        for(int x:comp[i]){
            merge(x,comp[i][0]);
        }
    }
    REP(i,n){
        for(int x:v[i]){
            if(uf.find(i)!=uf.find(x)){
                cnt[uf.find(i)]++,cnt[uf.find(x)]++;
            }
        }
    }
    int ans=0;
    REP(i,n) if(uf.find(i)==i and cnt[i]==2) ++ans;
    cout<<(ans+1)/2;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23812 KB Output is correct
2 Correct 12 ms 23828 KB Output is correct
3 Correct 12 ms 23832 KB Output is correct
4 Correct 13 ms 23760 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 12 ms 23800 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 14 ms 23956 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 14 ms 23844 KB Output is correct
11 Correct 12 ms 23828 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 12 ms 23804 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23828 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 16 ms 23784 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23812 KB Output is correct
2 Correct 12 ms 23828 KB Output is correct
3 Correct 12 ms 23832 KB Output is correct
4 Correct 13 ms 23760 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 12 ms 23800 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 14 ms 23956 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 14 ms 23844 KB Output is correct
11 Correct 12 ms 23828 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 12 ms 23804 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23828 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 16 ms 23784 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 12 ms 23752 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 13 ms 23972 KB Output is correct
28 Correct 14 ms 24148 KB Output is correct
29 Correct 15 ms 24104 KB Output is correct
30 Correct 13 ms 24020 KB Output is correct
31 Correct 13 ms 23772 KB Output is correct
32 Correct 14 ms 24192 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 14 ms 23944 KB Output is correct
35 Correct 14 ms 24020 KB Output is correct
36 Correct 18 ms 24020 KB Output is correct
37 Correct 18 ms 24020 KB Output is correct
38 Correct 14 ms 23828 KB Output is correct
39 Correct 14 ms 24012 KB Output is correct
40 Correct 13 ms 24020 KB Output is correct
41 Correct 13 ms 24020 KB Output is correct
42 Correct 13 ms 24092 KB Output is correct
43 Correct 14 ms 24020 KB Output is correct
44 Correct 12 ms 23764 KB Output is correct
45 Correct 14 ms 24104 KB Output is correct
46 Correct 14 ms 23972 KB Output is correct
47 Correct 13 ms 23840 KB Output is correct
48 Correct 14 ms 24012 KB Output is correct
49 Correct 14 ms 24108 KB Output is correct
50 Correct 15 ms 24096 KB Output is correct
51 Correct 15 ms 23996 KB Output is correct
52 Correct 16 ms 24020 KB Output is correct
53 Correct 14 ms 24020 KB Output is correct
54 Correct 17 ms 24004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23812 KB Output is correct
2 Correct 12 ms 23828 KB Output is correct
3 Correct 12 ms 23832 KB Output is correct
4 Correct 13 ms 23760 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 12 ms 23800 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 14 ms 23956 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 14 ms 23844 KB Output is correct
11 Correct 12 ms 23828 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 12 ms 23804 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23828 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 16 ms 23784 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 54 ms 30532 KB Output is correct
27 Correct 77 ms 30172 KB Output is correct
28 Correct 14 ms 24044 KB Output is correct
29 Correct 14 ms 23764 KB Output is correct
30 Correct 13 ms 23828 KB Output is correct
31 Correct 76 ms 30232 KB Output is correct
32 Correct 14 ms 24020 KB Output is correct
33 Correct 69 ms 32896 KB Output is correct
34 Correct 75 ms 30148 KB Output is correct
35 Correct 15 ms 24020 KB Output is correct
36 Correct 84 ms 30496 KB Output is correct
37 Correct 15 ms 24012 KB Output is correct
38 Correct 15 ms 23972 KB Output is correct
39 Correct 57 ms 30484 KB Output is correct
40 Correct 15 ms 24096 KB Output is correct
41 Correct 82 ms 30136 KB Output is correct
42 Correct 81 ms 30988 KB Output is correct
43 Correct 13 ms 23748 KB Output is correct
44 Correct 86 ms 32916 KB Output is correct
45 Correct 75 ms 31624 KB Output is correct
46 Correct 17 ms 24044 KB Output is correct
47 Correct 14 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 30436 KB Output is correct
2 Correct 65 ms 33736 KB Output is correct
3 Correct 14 ms 24020 KB Output is correct
4 Correct 16 ms 24044 KB Output is correct
5 Correct 15 ms 23740 KB Output is correct
6 Correct 14 ms 23736 KB Output is correct
7 Correct 16 ms 23972 KB Output is correct
8 Correct 86 ms 31868 KB Output is correct
9 Correct 14 ms 24020 KB Output is correct
10 Correct 74 ms 30748 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 88 ms 30616 KB Output is correct
13 Correct 98 ms 31820 KB Output is correct
14 Correct 100 ms 33440 KB Output is correct
15 Correct 56 ms 30460 KB Output is correct
16 Correct 14 ms 24036 KB Output is correct
17 Correct 13 ms 23764 KB Output is correct
18 Correct 71 ms 33456 KB Output is correct
19 Correct 104 ms 34960 KB Output is correct
20 Correct 19 ms 23988 KB Output is correct
21 Correct 16 ms 23764 KB Output is correct
22 Correct 84 ms 32012 KB Output is correct
23 Correct 14 ms 24012 KB Output is correct
24 Correct 92 ms 31032 KB Output is correct
25 Correct 87 ms 34360 KB Output is correct
26 Correct 15 ms 24020 KB Output is correct
27 Correct 15 ms 24112 KB Output is correct
28 Correct 17 ms 23960 KB Output is correct
29 Correct 13 ms 24020 KB Output is correct
30 Correct 13 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23812 KB Output is correct
2 Correct 12 ms 23828 KB Output is correct
3 Correct 12 ms 23832 KB Output is correct
4 Correct 13 ms 23760 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 12 ms 23800 KB Output is correct
7 Correct 12 ms 23816 KB Output is correct
8 Correct 14 ms 23956 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 14 ms 23844 KB Output is correct
11 Correct 12 ms 23828 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 12 ms 23804 KB Output is correct
15 Correct 12 ms 23764 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23828 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 16 ms 23784 KB Output is correct
24 Correct 13 ms 23764 KB Output is correct
25 Correct 12 ms 23752 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 13 ms 23972 KB Output is correct
28 Correct 14 ms 24148 KB Output is correct
29 Correct 15 ms 24104 KB Output is correct
30 Correct 13 ms 24020 KB Output is correct
31 Correct 13 ms 23772 KB Output is correct
32 Correct 14 ms 24192 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 14 ms 23944 KB Output is correct
35 Correct 14 ms 24020 KB Output is correct
36 Correct 18 ms 24020 KB Output is correct
37 Correct 18 ms 24020 KB Output is correct
38 Correct 14 ms 23828 KB Output is correct
39 Correct 14 ms 24012 KB Output is correct
40 Correct 13 ms 24020 KB Output is correct
41 Correct 13 ms 24020 KB Output is correct
42 Correct 13 ms 24092 KB Output is correct
43 Correct 14 ms 24020 KB Output is correct
44 Correct 12 ms 23764 KB Output is correct
45 Correct 14 ms 24104 KB Output is correct
46 Correct 14 ms 23972 KB Output is correct
47 Correct 13 ms 23840 KB Output is correct
48 Correct 14 ms 24012 KB Output is correct
49 Correct 14 ms 24108 KB Output is correct
50 Correct 15 ms 24096 KB Output is correct
51 Correct 15 ms 23996 KB Output is correct
52 Correct 16 ms 24020 KB Output is correct
53 Correct 14 ms 24020 KB Output is correct
54 Correct 17 ms 24004 KB Output is correct
55 Correct 12 ms 23764 KB Output is correct
56 Correct 54 ms 30532 KB Output is correct
57 Correct 77 ms 30172 KB Output is correct
58 Correct 14 ms 24044 KB Output is correct
59 Correct 14 ms 23764 KB Output is correct
60 Correct 13 ms 23828 KB Output is correct
61 Correct 76 ms 30232 KB Output is correct
62 Correct 14 ms 24020 KB Output is correct
63 Correct 69 ms 32896 KB Output is correct
64 Correct 75 ms 30148 KB Output is correct
65 Correct 15 ms 24020 KB Output is correct
66 Correct 84 ms 30496 KB Output is correct
67 Correct 15 ms 24012 KB Output is correct
68 Correct 15 ms 23972 KB Output is correct
69 Correct 57 ms 30484 KB Output is correct
70 Correct 15 ms 24096 KB Output is correct
71 Correct 82 ms 30136 KB Output is correct
72 Correct 81 ms 30988 KB Output is correct
73 Correct 13 ms 23748 KB Output is correct
74 Correct 86 ms 32916 KB Output is correct
75 Correct 75 ms 31624 KB Output is correct
76 Correct 17 ms 24044 KB Output is correct
77 Correct 14 ms 24020 KB Output is correct
78 Correct 55 ms 30436 KB Output is correct
79 Correct 65 ms 33736 KB Output is correct
80 Correct 14 ms 24020 KB Output is correct
81 Correct 16 ms 24044 KB Output is correct
82 Correct 15 ms 23740 KB Output is correct
83 Correct 14 ms 23736 KB Output is correct
84 Correct 16 ms 23972 KB Output is correct
85 Correct 86 ms 31868 KB Output is correct
86 Correct 14 ms 24020 KB Output is correct
87 Correct 74 ms 30748 KB Output is correct
88 Correct 12 ms 23764 KB Output is correct
89 Correct 88 ms 30616 KB Output is correct
90 Correct 98 ms 31820 KB Output is correct
91 Correct 100 ms 33440 KB Output is correct
92 Correct 56 ms 30460 KB Output is correct
93 Correct 14 ms 24036 KB Output is correct
94 Correct 13 ms 23764 KB Output is correct
95 Correct 71 ms 33456 KB Output is correct
96 Correct 104 ms 34960 KB Output is correct
97 Correct 19 ms 23988 KB Output is correct
98 Correct 16 ms 23764 KB Output is correct
99 Correct 84 ms 32012 KB Output is correct
100 Correct 14 ms 24012 KB Output is correct
101 Correct 92 ms 31032 KB Output is correct
102 Correct 87 ms 34360 KB Output is correct
103 Correct 15 ms 24020 KB Output is correct
104 Correct 15 ms 24112 KB Output is correct
105 Correct 17 ms 23960 KB Output is correct
106 Correct 13 ms 24020 KB Output is correct
107 Correct 13 ms 24020 KB Output is correct
108 Correct 450 ms 65644 KB Output is correct
109 Correct 561 ms 62460 KB Output is correct
110 Correct 597 ms 66944 KB Output is correct
111 Correct 712 ms 82368 KB Output is correct
112 Correct 677 ms 78480 KB Output is correct
113 Correct 513 ms 73320 KB Output is correct
114 Correct 488 ms 55608 KB Output is correct
115 Correct 487 ms 55528 KB Output is correct
116 Correct 620 ms 61836 KB Output is correct
117 Correct 631 ms 73496 KB Output is correct
118 Correct 511 ms 58364 KB Output is correct
119 Correct 624 ms 73500 KB Output is correct
120 Correct 700 ms 78784 KB Output is correct
121 Correct 674 ms 73440 KB Output is correct
122 Correct 647 ms 67236 KB Output is correct
123 Correct 430 ms 74888 KB Output is correct
124 Correct 709 ms 71956 KB Output is correct