Submission #376948

# Submission time Handle Problem Language Result Execution time Memory
376948 2021-03-12T09:47:52 Z wiwiho Mergers (JOI19_mergers) C++14
100 / 100
1396 ms 174464 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

vector<vector<int>> g;
vector<int> s, cnt;
vector<map<int, int>> st;
stack<int> stk;
vector<int> id;
int ts = 0;
void dfs(int now, int p){
    stk.push(now);
    st[now][s[now]]++;
    if(st[now][s[now]] == cnt[s[now]]) st[now].erase(s[now]);
    for(int i : g[now]){
        if(i == p) continue;
        dfs(i, now);
        if(st[now].size() < st[i].size()) st[now].swap(st[i]);
        for(pii j : st[i]){
            st[now][j.F] += j.S;
            if(st[now][j.F] == cnt[j.F]) st[now].erase(j.F);
        }
    }
    if(st[now].empty()){
        while(true){
            int v = stk.top();
            id[v] = ts;
            stk.pop();
            if(v == now) break;
        }
        ts++;
    }
}

int main(){
    StarBurstStream

    int n, k;
    cin >> n >> k;

    g.resize(n + 1);
    s.resize(n + 1);
    cnt.resize(k + 1);
    st.resize(n + 1);
    id.resize(n + 1);

    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
    }
    for(int i = 1; i <= n; i++){
        cin >> s[i];
        cnt[s[i]]++;
    }

    dfs(1, 1);
//    printv(id, cerr);
    vector<set<int>> deg(ts);
    for(int i = 1; i <= n; i++){
        for(int j : g[i]){
            if(id[i] != id[j]) deg[id[i]].insert(id[j]);
        }
    }

    int o = 0;
    for(int i = 0; i < ts; i++){
//        cerr << i << "  ";
//        printv(deg[i], cerr);
        if(deg[i].size() == 1) o++;
    }
    if(o == 1) cout << "0\n";
    else cout << (o + 1) / 2 << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 376 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 376 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 3 ms 1004 KB Output is correct
27 Correct 2 ms 876 KB Output is correct
28 Correct 3 ms 1132 KB Output is correct
29 Correct 3 ms 1132 KB Output is correct
30 Correct 4 ms 1132 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 3 ms 1132 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 3 ms 876 KB Output is correct
35 Correct 3 ms 1152 KB Output is correct
36 Correct 3 ms 876 KB Output is correct
37 Correct 4 ms 1016 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 5 ms 1132 KB Output is correct
40 Correct 3 ms 876 KB Output is correct
41 Correct 2 ms 876 KB Output is correct
42 Correct 3 ms 1004 KB Output is correct
43 Correct 3 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 3 ms 896 KB Output is correct
46 Correct 3 ms 1004 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 876 KB Output is correct
49 Correct 3 ms 1132 KB Output is correct
50 Correct 3 ms 1260 KB Output is correct
51 Correct 3 ms 876 KB Output is correct
52 Correct 3 ms 876 KB Output is correct
53 Correct 3 ms 876 KB Output is correct
54 Correct 4 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 376 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 102 ms 18236 KB Output is correct
27 Correct 156 ms 24684 KB Output is correct
28 Correct 3 ms 876 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 118 ms 17516 KB Output is correct
32 Correct 3 ms 876 KB Output is correct
33 Correct 152 ms 31084 KB Output is correct
34 Correct 140 ms 23148 KB Output is correct
35 Correct 3 ms 1004 KB Output is correct
36 Correct 140 ms 18924 KB Output is correct
37 Correct 3 ms 876 KB Output is correct
38 Correct 3 ms 876 KB Output is correct
39 Correct 86 ms 18148 KB Output is correct
40 Correct 3 ms 1260 KB Output is correct
41 Correct 108 ms 17900 KB Output is correct
42 Correct 126 ms 22380 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 127 ms 31596 KB Output is correct
45 Correct 132 ms 25836 KB Output is correct
46 Correct 3 ms 880 KB Output is correct
47 Correct 4 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 17508 KB Output is correct
2 Correct 125 ms 26980 KB Output is correct
3 Correct 3 ms 1004 KB Output is correct
4 Correct 2 ms 876 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 876 KB Output is correct
8 Correct 148 ms 21952 KB Output is correct
9 Correct 3 ms 876 KB Output is correct
10 Correct 110 ms 17772 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 111 ms 17772 KB Output is correct
13 Correct 124 ms 19820 KB Output is correct
14 Correct 144 ms 27628 KB Output is correct
15 Correct 91 ms 18184 KB Output is correct
16 Correct 3 ms 1004 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 125 ms 26212 KB Output is correct
19 Correct 153 ms 34796 KB Output is correct
20 Correct 3 ms 1004 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 127 ms 19940 KB Output is correct
23 Correct 3 ms 876 KB Output is correct
24 Correct 113 ms 18668 KB Output is correct
25 Correct 156 ms 32364 KB Output is correct
26 Correct 3 ms 1132 KB Output is correct
27 Correct 3 ms 1260 KB Output is correct
28 Correct 3 ms 876 KB Output is correct
29 Correct 2 ms 876 KB Output is correct
30 Correct 3 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 376 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 3 ms 1004 KB Output is correct
27 Correct 2 ms 876 KB Output is correct
28 Correct 3 ms 1132 KB Output is correct
29 Correct 3 ms 1132 KB Output is correct
30 Correct 4 ms 1132 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 3 ms 1132 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 3 ms 876 KB Output is correct
35 Correct 3 ms 1152 KB Output is correct
36 Correct 3 ms 876 KB Output is correct
37 Correct 4 ms 1016 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 5 ms 1132 KB Output is correct
40 Correct 3 ms 876 KB Output is correct
41 Correct 2 ms 876 KB Output is correct
42 Correct 3 ms 1004 KB Output is correct
43 Correct 3 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 3 ms 896 KB Output is correct
46 Correct 3 ms 1004 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 876 KB Output is correct
49 Correct 3 ms 1132 KB Output is correct
50 Correct 3 ms 1260 KB Output is correct
51 Correct 3 ms 876 KB Output is correct
52 Correct 3 ms 876 KB Output is correct
53 Correct 3 ms 876 KB Output is correct
54 Correct 4 ms 1004 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 102 ms 18236 KB Output is correct
57 Correct 156 ms 24684 KB Output is correct
58 Correct 3 ms 876 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 118 ms 17516 KB Output is correct
62 Correct 3 ms 876 KB Output is correct
63 Correct 152 ms 31084 KB Output is correct
64 Correct 140 ms 23148 KB Output is correct
65 Correct 3 ms 1004 KB Output is correct
66 Correct 140 ms 18924 KB Output is correct
67 Correct 3 ms 876 KB Output is correct
68 Correct 3 ms 876 KB Output is correct
69 Correct 86 ms 18148 KB Output is correct
70 Correct 3 ms 1260 KB Output is correct
71 Correct 108 ms 17900 KB Output is correct
72 Correct 126 ms 22380 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 127 ms 31596 KB Output is correct
75 Correct 132 ms 25836 KB Output is correct
76 Correct 3 ms 880 KB Output is correct
77 Correct 4 ms 1004 KB Output is correct
78 Correct 81 ms 17508 KB Output is correct
79 Correct 125 ms 26980 KB Output is correct
80 Correct 3 ms 1004 KB Output is correct
81 Correct 2 ms 876 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 2 ms 876 KB Output is correct
85 Correct 148 ms 21952 KB Output is correct
86 Correct 3 ms 876 KB Output is correct
87 Correct 110 ms 17772 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 111 ms 17772 KB Output is correct
90 Correct 124 ms 19820 KB Output is correct
91 Correct 144 ms 27628 KB Output is correct
92 Correct 91 ms 18184 KB Output is correct
93 Correct 3 ms 1004 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 125 ms 26212 KB Output is correct
96 Correct 153 ms 34796 KB Output is correct
97 Correct 3 ms 1004 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 127 ms 19940 KB Output is correct
100 Correct 3 ms 876 KB Output is correct
101 Correct 113 ms 18668 KB Output is correct
102 Correct 156 ms 32364 KB Output is correct
103 Correct 3 ms 1132 KB Output is correct
104 Correct 3 ms 1260 KB Output is correct
105 Correct 3 ms 876 KB Output is correct
106 Correct 2 ms 876 KB Output is correct
107 Correct 3 ms 876 KB Output is correct
108 Correct 1000 ms 95716 KB Output is correct
109 Correct 873 ms 121452 KB Output is correct
110 Correct 880 ms 141804 KB Output is correct
111 Correct 1036 ms 174464 KB Output is correct
112 Correct 1153 ms 129516 KB Output is correct
113 Correct 839 ms 130260 KB Output is correct
114 Correct 773 ms 87532 KB Output is correct
115 Correct 779 ms 88556 KB Output is correct
116 Correct 1194 ms 146284 KB Output is correct
117 Correct 984 ms 137580 KB Output is correct
118 Correct 776 ms 85496 KB Output is correct
119 Correct 990 ms 137580 KB Output is correct
120 Correct 1035 ms 163268 KB Output is correct
121 Correct 981 ms 137452 KB Output is correct
122 Correct 1396 ms 165536 KB Output is correct
123 Correct 791 ms 139132 KB Output is correct
124 Correct 894 ms 110828 KB Output is correct