Submission #470677

# Submission time Handle Problem Language Result Execution time Memory
470677 2021-09-05T00:37:13 Z aZvezda Mergers (JOI19_mergers) C++14
34 / 100
3000 ms 36056 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 5e5 + 10;
vector<int> g[MAX_N], nw[MAX_N];
int cnt[MAX_N], cntNow[MAX_N], col[MAX_N];
int n, k, ret;
int sz[MAX_N], big[MAX_N];
int cntok;

void add(int x, int p, int delta = 1) {
    if(cntNow[col[x]] == 0 || cntNow[col[x]] == cnt[col[x]]) {
        cntok --;
    }
    cntNow[col[x]] += delta;
    if(cntNow[col[x]] == 0 || cntNow[col[x]] == cnt[col[x]]) {
        cntok ++;
    }
    for(auto it : g[x]) {
        if(it == p) {continue;}
        add(it, x, delta);
    }
}

void calcSize(int x, int p) {
    sz[x] = 1;
    big[x] = 0;
    for(auto it : g[x]) {
        if(it == p) {continue;}
        calcSize(it, x);
        sz[x] += sz[it];
        if(sz[it] > sz[big[x]]) {
            big[x] = it;
        }
    }
}

vector<int> stck;
void dfs(int x, int p, bool keep = false) {
    int topRet = stck.size();

    for(auto it : g[x]) {
        if(it == p || it == big[x]) {continue;}
        dfs(it, x, false);
    }

    if(big[x] != 0) {
        dfs(big[x], x, true);
    }

    for(auto it : g[x]) {
        if(it == p || it == big[x]) {continue;}
        add(it, x, 1);
    }

    if(cntNow[col[x]] == 0 || cntNow[col[x]] == cnt[col[x]]) {
        cntok --;
    }
    cntNow[col[x]] ++;
    if(cntNow[col[x]] == 0 || cntNow[col[x]] == cnt[col[x]]) {
        cntok ++;
    }

    if(cntok == k) {
        while(stck.size() > topRet) {
            nw[x].push_back(stck.back());
            stck.pop_back();
        }
        stck.push_back(x);
    }

    if(!keep) {
        add(x, p, -1);
    }
}

void calc(int x, int d) {
    if(x == 1 && nw[x].size() == 1) {
        ret ++;
    } else if(nw[x].size() == 0) {
        ret ++;
    }
    for(auto it : nw[x]) {
        calc(it, d + 1);
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> k;
    cntok = k;
    for(int i = 0; i < n - 1; i ++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 1; i <= n; i ++) {
        cin >> col[i];
        cnt[col[i]] ++;
    }
    dfs(1, 0, 1);
    calc(1, 0);
    if(ret != 1) {
        cout << (ret + 1) / 2 << endl;
    } else {
        cout << 0 << endl;
    }
    return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int, bool)':
mergers.cpp:75:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   75 |         while(stck.size() > topRet) {
      |               ~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23756 KB Output is correct
4 Correct 14 ms 23824 KB Output is correct
5 Correct 13 ms 23720 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 13 ms 23836 KB Output is correct
9 Correct 13 ms 23824 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23732 KB Output is correct
12 Correct 13 ms 23756 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 15 ms 23720 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23724 KB Output is correct
17 Correct 13 ms 23788 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 14 ms 23760 KB Output is correct
21 Correct 13 ms 23736 KB Output is correct
22 Correct 13 ms 23780 KB Output is correct
23 Correct 13 ms 23708 KB Output is correct
24 Correct 14 ms 23728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23756 KB Output is correct
4 Correct 14 ms 23824 KB Output is correct
5 Correct 13 ms 23720 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 13 ms 23836 KB Output is correct
9 Correct 13 ms 23824 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23732 KB Output is correct
12 Correct 13 ms 23756 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 15 ms 23720 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23724 KB Output is correct
17 Correct 13 ms 23788 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 14 ms 23760 KB Output is correct
21 Correct 13 ms 23736 KB Output is correct
22 Correct 13 ms 23780 KB Output is correct
23 Correct 13 ms 23708 KB Output is correct
24 Correct 14 ms 23728 KB Output is correct
25 Correct 13 ms 23776 KB Output is correct
26 Correct 17 ms 23960 KB Output is correct
27 Correct 29 ms 23884 KB Output is correct
28 Correct 112 ms 24100 KB Output is correct
29 Correct 17 ms 23964 KB Output is correct
30 Correct 18 ms 23944 KB Output is correct
31 Correct 13 ms 23748 KB Output is correct
32 Correct 141 ms 24156 KB Output is correct
33 Correct 13 ms 23800 KB Output is correct
34 Correct 15 ms 23848 KB Output is correct
35 Correct 18 ms 24016 KB Output is correct
36 Correct 19 ms 23892 KB Output is correct
37 Correct 20 ms 23952 KB Output is correct
38 Correct 14 ms 23824 KB Output is correct
39 Correct 16 ms 23884 KB Output is correct
40 Correct 17 ms 23884 KB Output is correct
41 Correct 17 ms 23884 KB Output is correct
42 Correct 15 ms 23976 KB Output is correct
43 Correct 210 ms 24084 KB Output is correct
44 Correct 12 ms 23756 KB Output is correct
45 Correct 67 ms 24004 KB Output is correct
46 Correct 22 ms 23884 KB Output is correct
47 Correct 14 ms 23832 KB Output is correct
48 Correct 20 ms 23948 KB Output is correct
49 Correct 14 ms 24012 KB Output is correct
50 Correct 62 ms 24176 KB Output is correct
51 Correct 20 ms 23884 KB Output is correct
52 Correct 16 ms 23840 KB Output is correct
53 Correct 14 ms 23880 KB Output is correct
54 Correct 16 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23756 KB Output is correct
4 Correct 14 ms 23824 KB Output is correct
5 Correct 13 ms 23720 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 13 ms 23836 KB Output is correct
9 Correct 13 ms 23824 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23732 KB Output is correct
12 Correct 13 ms 23756 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 15 ms 23720 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23724 KB Output is correct
17 Correct 13 ms 23788 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 14 ms 23760 KB Output is correct
21 Correct 13 ms 23736 KB Output is correct
22 Correct 13 ms 23780 KB Output is correct
23 Correct 13 ms 23708 KB Output is correct
24 Correct 14 ms 23728 KB Output is correct
25 Correct 13 ms 23756 KB Output is correct
26 Correct 64 ms 27728 KB Output is correct
27 Correct 252 ms 27332 KB Output is correct
28 Correct 31 ms 23956 KB Output is correct
29 Correct 13 ms 23756 KB Output is correct
30 Correct 14 ms 23756 KB Output is correct
31 Correct 774 ms 27460 KB Output is correct
32 Correct 15 ms 23884 KB Output is correct
33 Execution timed out 3047 ms 36056 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 27640 KB Output is correct
2 Correct 70 ms 29604 KB Output is correct
3 Correct 19 ms 23988 KB Output is correct
4 Correct 30 ms 24012 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 15 ms 23884 KB Output is correct
8 Correct 1420 ms 27940 KB Output is correct
9 Correct 19 ms 23884 KB Output is correct
10 Correct 863 ms 27644 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
12 Execution timed out 3070 ms 27472 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 14 ms 23756 KB Output is correct
3 Correct 14 ms 23756 KB Output is correct
4 Correct 14 ms 23824 KB Output is correct
5 Correct 13 ms 23720 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23792 KB Output is correct
8 Correct 13 ms 23836 KB Output is correct
9 Correct 13 ms 23824 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23732 KB Output is correct
12 Correct 13 ms 23756 KB Output is correct
13 Correct 13 ms 23816 KB Output is correct
14 Correct 15 ms 23720 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23724 KB Output is correct
17 Correct 13 ms 23788 KB Output is correct
18 Correct 13 ms 23756 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 14 ms 23760 KB Output is correct
21 Correct 13 ms 23736 KB Output is correct
22 Correct 13 ms 23780 KB Output is correct
23 Correct 13 ms 23708 KB Output is correct
24 Correct 14 ms 23728 KB Output is correct
25 Correct 13 ms 23776 KB Output is correct
26 Correct 17 ms 23960 KB Output is correct
27 Correct 29 ms 23884 KB Output is correct
28 Correct 112 ms 24100 KB Output is correct
29 Correct 17 ms 23964 KB Output is correct
30 Correct 18 ms 23944 KB Output is correct
31 Correct 13 ms 23748 KB Output is correct
32 Correct 141 ms 24156 KB Output is correct
33 Correct 13 ms 23800 KB Output is correct
34 Correct 15 ms 23848 KB Output is correct
35 Correct 18 ms 24016 KB Output is correct
36 Correct 19 ms 23892 KB Output is correct
37 Correct 20 ms 23952 KB Output is correct
38 Correct 14 ms 23824 KB Output is correct
39 Correct 16 ms 23884 KB Output is correct
40 Correct 17 ms 23884 KB Output is correct
41 Correct 17 ms 23884 KB Output is correct
42 Correct 15 ms 23976 KB Output is correct
43 Correct 210 ms 24084 KB Output is correct
44 Correct 12 ms 23756 KB Output is correct
45 Correct 67 ms 24004 KB Output is correct
46 Correct 22 ms 23884 KB Output is correct
47 Correct 14 ms 23832 KB Output is correct
48 Correct 20 ms 23948 KB Output is correct
49 Correct 14 ms 24012 KB Output is correct
50 Correct 62 ms 24176 KB Output is correct
51 Correct 20 ms 23884 KB Output is correct
52 Correct 16 ms 23840 KB Output is correct
53 Correct 14 ms 23880 KB Output is correct
54 Correct 16 ms 23936 KB Output is correct
55 Correct 13 ms 23756 KB Output is correct
56 Correct 64 ms 27728 KB Output is correct
57 Correct 252 ms 27332 KB Output is correct
58 Correct 31 ms 23956 KB Output is correct
59 Correct 13 ms 23756 KB Output is correct
60 Correct 14 ms 23756 KB Output is correct
61 Correct 774 ms 27460 KB Output is correct
62 Correct 15 ms 23884 KB Output is correct
63 Execution timed out 3047 ms 36056 KB Time limit exceeded
64 Halted 0 ms 0 KB -