Submission #564820

# Submission time Handle Problem Language Result Execution time Memory
564820 2022-05-19T18:12:11 Z urosk Mergers (JOI19_mergers) C++14
100 / 100
746 ms 87376 KB
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;

#define maxn 500005
ll n,k;
vector<ll> g[maxn];
vector<ll> v;
ll a[maxn];
ll in[maxn];
ll out[maxn];
ll dp[maxn][3];
ll b[maxn][3];
ll it = 1;
void dfs(ll u,ll par){
    in[u] = it++;
    for(ll s : g[u]){
        if(s==par) continue;
        dfs(s,u);
    }
    out[u] = it-1;
}
bool intree(ll x,ll i){
    return x>=in[i]&&out[i]>=x;
}
bool bio[maxn];
ll ans = 0;
void dfs2(ll u,ll par){
    dp[u][1] = b[a[u]][1];
    dp[u][2] = b[a[u]][2];
    for(ll s : g[u]){
        if(s==par) continue;
        dfs2(s,u);
        dp[u][1] = min(dp[u][1],dp[s][1]);
        dp[u][2] = max(dp[u][2],dp[s][2]);
        bio[u]|=bio[s];
    }
    bool da = 0;
    if(u!=par){
        if(intree(dp[u][1],u)&&intree(dp[u][2],u)) da = 1;
    }
    if((bio[u]==0)&&(da==1)){
        ans++;
    }
    bio[u]|=da;
}
ll cnt = 0;
void dfs3(ll u,ll par){
    if(u!=par){
        if(intree(dp[u][1],u)&&intree(dp[u][2],u)){
            cnt++;
            return;
        }
    }
    for(ll s : g[u]){
        if(s==par) continue;
        dfs3(s,u);
    }
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> k;
    for(ll i = 1;i<=n-1;i++){
        ll x,y; cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    for(ll i = 1;i<=n;i++) cin >> a[i];
    dfs(1,1);
    for(ll i = 1;i<=k;i++){
        b[i][1] = llinf;
        b[i][2] = 0;
    }
    for(ll i = 1;i<=n;i++){
        ll col = a[i];
        b[col][1] = min(b[col][1],in[i]);
        b[col][2] = max(b[col][2],in[i]);
    }
    dfs2(1,1);
    dfs3(1,1);
    if(cnt==1) ans++;
    ans = (ans+1)/2;
    cout<<ans<<endl;
    return 0;
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

5 5
1 2
2 3
3 4
4 5
1
2
3
4
5
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12068 KB Output is correct
5 Correct 6 ms 12028 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 12116 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 7 ms 12080 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 9 ms 12116 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12116 KB Output is correct
22 Correct 6 ms 12068 KB Output is correct
23 Correct 6 ms 12064 KB Output is correct
24 Correct 6 ms 12072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12068 KB Output is correct
5 Correct 6 ms 12028 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 12116 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 7 ms 12080 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 9 ms 12116 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12116 KB Output is correct
22 Correct 6 ms 12068 KB Output is correct
23 Correct 6 ms 12064 KB Output is correct
24 Correct 6 ms 12072 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 8 ms 12336 KB Output is correct
27 Correct 8 ms 12372 KB Output is correct
28 Correct 9 ms 12544 KB Output is correct
29 Correct 8 ms 12372 KB Output is correct
30 Correct 9 ms 12412 KB Output is correct
31 Correct 6 ms 12116 KB Output is correct
32 Correct 8 ms 12500 KB Output is correct
33 Correct 7 ms 12076 KB Output is correct
34 Correct 8 ms 12344 KB Output is correct
35 Correct 7 ms 12468 KB Output is correct
36 Correct 8 ms 12372 KB Output is correct
37 Correct 8 ms 12340 KB Output is correct
38 Correct 9 ms 12116 KB Output is correct
39 Correct 9 ms 12320 KB Output is correct
40 Correct 9 ms 12280 KB Output is correct
41 Correct 10 ms 12372 KB Output is correct
42 Correct 8 ms 12372 KB Output is correct
43 Correct 8 ms 12604 KB Output is correct
44 Correct 6 ms 12116 KB Output is correct
45 Correct 10 ms 12500 KB Output is correct
46 Correct 8 ms 12344 KB Output is correct
47 Correct 6 ms 12120 KB Output is correct
48 Correct 8 ms 12372 KB Output is correct
49 Correct 8 ms 12456 KB Output is correct
50 Correct 8 ms 12500 KB Output is correct
51 Correct 8 ms 12372 KB Output is correct
52 Correct 8 ms 12372 KB Output is correct
53 Correct 8 ms 12372 KB Output is correct
54 Correct 8 ms 12340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12068 KB Output is correct
5 Correct 6 ms 12028 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 12116 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 7 ms 12080 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 9 ms 12116 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12116 KB Output is correct
22 Correct 6 ms 12068 KB Output is correct
23 Correct 6 ms 12064 KB Output is correct
24 Correct 6 ms 12072 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 45 ms 21376 KB Output is correct
27 Correct 72 ms 21224 KB Output is correct
28 Correct 8 ms 12372 KB Output is correct
29 Correct 6 ms 11988 KB Output is correct
30 Correct 8 ms 12116 KB Output is correct
31 Correct 78 ms 21280 KB Output is correct
32 Correct 8 ms 12404 KB Output is correct
33 Correct 128 ms 28024 KB Output is correct
34 Correct 65 ms 21196 KB Output is correct
35 Correct 7 ms 12428 KB Output is correct
36 Correct 72 ms 21504 KB Output is correct
37 Correct 8 ms 12372 KB Output is correct
38 Correct 8 ms 12372 KB Output is correct
39 Correct 53 ms 21400 KB Output is correct
40 Correct 8 ms 12596 KB Output is correct
41 Correct 76 ms 21248 KB Output is correct
42 Correct 82 ms 23084 KB Output is correct
43 Correct 7 ms 12116 KB Output is correct
44 Correct 114 ms 28200 KB Output is correct
45 Correct 81 ms 25656 KB Output is correct
46 Correct 8 ms 12372 KB Output is correct
47 Correct 10 ms 12352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 20728 KB Output is correct
2 Correct 71 ms 23136 KB Output is correct
3 Correct 8 ms 12372 KB Output is correct
4 Correct 7 ms 12244 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12284 KB Output is correct
8 Correct 75 ms 21740 KB Output is correct
9 Correct 7 ms 12244 KB Output is correct
10 Correct 71 ms 20588 KB Output is correct
11 Correct 8 ms 12116 KB Output is correct
12 Correct 82 ms 20764 KB Output is correct
13 Correct 87 ms 22364 KB Output is correct
14 Correct 72 ms 23612 KB Output is correct
15 Correct 63 ms 21336 KB Output is correct
16 Correct 8 ms 12344 KB Output is correct
17 Correct 8 ms 12068 KB Output is correct
18 Correct 77 ms 23488 KB Output is correct
19 Correct 94 ms 26920 KB Output is correct
20 Correct 8 ms 12416 KB Output is correct
21 Correct 8 ms 12064 KB Output is correct
22 Correct 52 ms 22596 KB Output is correct
23 Correct 7 ms 12340 KB Output is correct
24 Correct 87 ms 21180 KB Output is correct
25 Correct 101 ms 26828 KB Output is correct
26 Correct 7 ms 12372 KB Output is correct
27 Correct 8 ms 12500 KB Output is correct
28 Correct 7 ms 12408 KB Output is correct
29 Correct 9 ms 12372 KB Output is correct
30 Correct 8 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12044 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12068 KB Output is correct
5 Correct 6 ms 12028 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 12116 KB Output is correct
11 Correct 7 ms 12116 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 7 ms 12080 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 9 ms 12116 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12116 KB Output is correct
22 Correct 6 ms 12068 KB Output is correct
23 Correct 6 ms 12064 KB Output is correct
24 Correct 6 ms 12072 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 8 ms 12336 KB Output is correct
27 Correct 8 ms 12372 KB Output is correct
28 Correct 9 ms 12544 KB Output is correct
29 Correct 8 ms 12372 KB Output is correct
30 Correct 9 ms 12412 KB Output is correct
31 Correct 6 ms 12116 KB Output is correct
32 Correct 8 ms 12500 KB Output is correct
33 Correct 7 ms 12076 KB Output is correct
34 Correct 8 ms 12344 KB Output is correct
35 Correct 7 ms 12468 KB Output is correct
36 Correct 8 ms 12372 KB Output is correct
37 Correct 8 ms 12340 KB Output is correct
38 Correct 9 ms 12116 KB Output is correct
39 Correct 9 ms 12320 KB Output is correct
40 Correct 9 ms 12280 KB Output is correct
41 Correct 10 ms 12372 KB Output is correct
42 Correct 8 ms 12372 KB Output is correct
43 Correct 8 ms 12604 KB Output is correct
44 Correct 6 ms 12116 KB Output is correct
45 Correct 10 ms 12500 KB Output is correct
46 Correct 8 ms 12344 KB Output is correct
47 Correct 6 ms 12120 KB Output is correct
48 Correct 8 ms 12372 KB Output is correct
49 Correct 8 ms 12456 KB Output is correct
50 Correct 8 ms 12500 KB Output is correct
51 Correct 8 ms 12372 KB Output is correct
52 Correct 8 ms 12372 KB Output is correct
53 Correct 8 ms 12372 KB Output is correct
54 Correct 8 ms 12340 KB Output is correct
55 Correct 7 ms 12116 KB Output is correct
56 Correct 45 ms 21376 KB Output is correct
57 Correct 72 ms 21224 KB Output is correct
58 Correct 8 ms 12372 KB Output is correct
59 Correct 6 ms 11988 KB Output is correct
60 Correct 8 ms 12116 KB Output is correct
61 Correct 78 ms 21280 KB Output is correct
62 Correct 8 ms 12404 KB Output is correct
63 Correct 128 ms 28024 KB Output is correct
64 Correct 65 ms 21196 KB Output is correct
65 Correct 7 ms 12428 KB Output is correct
66 Correct 72 ms 21504 KB Output is correct
67 Correct 8 ms 12372 KB Output is correct
68 Correct 8 ms 12372 KB Output is correct
69 Correct 53 ms 21400 KB Output is correct
70 Correct 8 ms 12596 KB Output is correct
71 Correct 76 ms 21248 KB Output is correct
72 Correct 82 ms 23084 KB Output is correct
73 Correct 7 ms 12116 KB Output is correct
74 Correct 114 ms 28200 KB Output is correct
75 Correct 81 ms 25656 KB Output is correct
76 Correct 8 ms 12372 KB Output is correct
77 Correct 10 ms 12352 KB Output is correct
78 Correct 44 ms 20728 KB Output is correct
79 Correct 71 ms 23136 KB Output is correct
80 Correct 8 ms 12372 KB Output is correct
81 Correct 7 ms 12244 KB Output is correct
82 Correct 7 ms 11988 KB Output is correct
83 Correct 6 ms 11988 KB Output is correct
84 Correct 7 ms 12284 KB Output is correct
85 Correct 75 ms 21740 KB Output is correct
86 Correct 7 ms 12244 KB Output is correct
87 Correct 71 ms 20588 KB Output is correct
88 Correct 8 ms 12116 KB Output is correct
89 Correct 82 ms 20764 KB Output is correct
90 Correct 87 ms 22364 KB Output is correct
91 Correct 72 ms 23612 KB Output is correct
92 Correct 63 ms 21336 KB Output is correct
93 Correct 8 ms 12344 KB Output is correct
94 Correct 8 ms 12068 KB Output is correct
95 Correct 77 ms 23488 KB Output is correct
96 Correct 94 ms 26920 KB Output is correct
97 Correct 8 ms 12416 KB Output is correct
98 Correct 8 ms 12064 KB Output is correct
99 Correct 52 ms 22596 KB Output is correct
100 Correct 7 ms 12340 KB Output is correct
101 Correct 87 ms 21180 KB Output is correct
102 Correct 101 ms 26828 KB Output is correct
103 Correct 7 ms 12372 KB Output is correct
104 Correct 8 ms 12500 KB Output is correct
105 Correct 7 ms 12408 KB Output is correct
106 Correct 9 ms 12372 KB Output is correct
107 Correct 8 ms 12372 KB Output is correct
108 Correct 394 ms 60940 KB Output is correct
109 Correct 680 ms 70836 KB Output is correct
110 Correct 746 ms 82072 KB Output is correct
111 Correct 657 ms 87376 KB Output is correct
112 Correct 662 ms 83580 KB Output is correct
113 Correct 348 ms 66540 KB Output is correct
114 Correct 539 ms 55156 KB Output is correct
115 Correct 540 ms 55052 KB Output is correct
116 Correct 533 ms 57612 KB Output is correct
117 Correct 563 ms 66852 KB Output is correct
118 Correct 489 ms 54236 KB Output is correct
119 Correct 541 ms 66884 KB Output is correct
120 Correct 608 ms 83828 KB Output is correct
121 Correct 550 ms 66468 KB Output is correct
122 Correct 649 ms 61784 KB Output is correct
123 Correct 312 ms 67296 KB Output is correct
124 Correct 609 ms 62852 KB Output is correct