답안 #655785

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
655785 2022-11-05T14:45:23 Z aZvezda Mergers (JOI19_mergers) C++14
100 / 100
770 ms 72300 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;
void *arenaPtr;
size_t arenaSize;
template<class T>
struct MyAllocator {
    typedef T value_type;

    MyAllocator () = default;
    template<class U>
    constexpr MyAllocator (const MyAllocator <U>&) noexcept {}

	T* allocate(size_t n) {
		const auto ret = (T*)((char*)arenaPtr + arenaSize);
		arenaSize += n * sizeof(T);
		return ret;
    }
	void deallocate(T* p, size_t n) {
    	return;
	}
};

vector<int , MyAllocator<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 count(int x, 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 ++;
    }
}

void add(int x, int p, int delta = 1) {
    count(x, delta);
    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, MyAllocator<int> > stck;
void dfs(int x, int p, bool keep = true) {
    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);
    }
    count(x);

    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() {
	arenaPtr = malloc(MAX_N * 1000);
	arenaSize = 0;

	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]] ++;
    }
    calcSize(1, 0);
    dfs(1, 0, 1);
    calc(1, 0);
    if(ret != 1) {
        cout << (ret + 1) / 2 << endl;
    } else {
        cout << 0 << endl;
    }

	free(arenaPtr);
    return 0;
}

Compilation message

mergers.cpp: In function 'void dfs(int, int, bool)':
mergers.cpp:91:27: warning: comparison of integer expressions of different signedness: 'std::vector<int, MyAllocator<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   91 |         while(stck.size() > topRet) {
      |               ~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Correct 10 ms 23764 KB Output is correct
4 Correct 10 ms 23864 KB Output is correct
5 Correct 10 ms 23828 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 10 ms 23836 KB Output is correct
8 Correct 11 ms 23820 KB Output is correct
9 Correct 10 ms 23764 KB Output is correct
10 Correct 10 ms 23804 KB Output is correct
11 Correct 11 ms 23832 KB Output is correct
12 Correct 10 ms 23764 KB Output is correct
13 Correct 10 ms 23764 KB Output is correct
14 Correct 10 ms 23752 KB Output is correct
15 Correct 10 ms 23832 KB Output is correct
16 Correct 10 ms 23764 KB Output is correct
17 Correct 10 ms 23744 KB Output is correct
18 Correct 10 ms 23764 KB Output is correct
19 Correct 10 ms 23836 KB Output is correct
20 Correct 11 ms 23828 KB Output is correct
21 Correct 10 ms 23832 KB Output is correct
22 Correct 9 ms 23764 KB Output is correct
23 Correct 10 ms 23836 KB Output is correct
24 Correct 10 ms 23856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Correct 10 ms 23764 KB Output is correct
4 Correct 10 ms 23864 KB Output is correct
5 Correct 10 ms 23828 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 10 ms 23836 KB Output is correct
8 Correct 11 ms 23820 KB Output is correct
9 Correct 10 ms 23764 KB Output is correct
10 Correct 10 ms 23804 KB Output is correct
11 Correct 11 ms 23832 KB Output is correct
12 Correct 10 ms 23764 KB Output is correct
13 Correct 10 ms 23764 KB Output is correct
14 Correct 10 ms 23752 KB Output is correct
15 Correct 10 ms 23832 KB Output is correct
16 Correct 10 ms 23764 KB Output is correct
17 Correct 10 ms 23744 KB Output is correct
18 Correct 10 ms 23764 KB Output is correct
19 Correct 10 ms 23836 KB Output is correct
20 Correct 11 ms 23828 KB Output is correct
21 Correct 10 ms 23832 KB Output is correct
22 Correct 9 ms 23764 KB Output is correct
23 Correct 10 ms 23836 KB Output is correct
24 Correct 10 ms 23856 KB Output is correct
25 Correct 10 ms 23764 KB Output is correct
26 Correct 14 ms 23952 KB Output is correct
27 Correct 14 ms 23916 KB Output is correct
28 Correct 12 ms 24148 KB Output is correct
29 Correct 14 ms 23972 KB Output is correct
30 Correct 12 ms 23960 KB Output is correct
31 Correct 10 ms 23764 KB Output is correct
32 Correct 11 ms 24100 KB Output is correct
33 Correct 9 ms 23764 KB Output is correct
34 Correct 12 ms 23940 KB Output is correct
35 Correct 13 ms 24020 KB Output is correct
36 Correct 11 ms 23892 KB Output is correct
37 Correct 12 ms 23892 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 12 ms 23884 KB Output is correct
40 Correct 11 ms 23976 KB Output is correct
41 Correct 11 ms 23892 KB Output is correct
42 Correct 11 ms 23892 KB Output is correct
43 Correct 11 ms 24148 KB Output is correct
44 Correct 10 ms 23832 KB Output is correct
45 Correct 11 ms 23992 KB Output is correct
46 Correct 11 ms 23984 KB Output is correct
47 Correct 10 ms 23764 KB Output is correct
48 Correct 12 ms 23892 KB Output is correct
49 Correct 11 ms 24056 KB Output is correct
50 Correct 11 ms 24020 KB Output is correct
51 Correct 11 ms 23976 KB Output is correct
52 Correct 12 ms 23972 KB Output is correct
53 Correct 13 ms 23900 KB Output is correct
54 Correct 12 ms 23956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Correct 10 ms 23764 KB Output is correct
4 Correct 10 ms 23864 KB Output is correct
5 Correct 10 ms 23828 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 10 ms 23836 KB Output is correct
8 Correct 11 ms 23820 KB Output is correct
9 Correct 10 ms 23764 KB Output is correct
10 Correct 10 ms 23804 KB Output is correct
11 Correct 11 ms 23832 KB Output is correct
12 Correct 10 ms 23764 KB Output is correct
13 Correct 10 ms 23764 KB Output is correct
14 Correct 10 ms 23752 KB Output is correct
15 Correct 10 ms 23832 KB Output is correct
16 Correct 10 ms 23764 KB Output is correct
17 Correct 10 ms 23744 KB Output is correct
18 Correct 10 ms 23764 KB Output is correct
19 Correct 10 ms 23836 KB Output is correct
20 Correct 11 ms 23828 KB Output is correct
21 Correct 10 ms 23832 KB Output is correct
22 Correct 9 ms 23764 KB Output is correct
23 Correct 10 ms 23836 KB Output is correct
24 Correct 10 ms 23856 KB Output is correct
25 Correct 10 ms 23828 KB Output is correct
26 Correct 39 ms 27340 KB Output is correct
27 Correct 72 ms 27476 KB Output is correct
28 Correct 14 ms 23928 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 84 ms 27532 KB Output is correct
32 Correct 11 ms 23892 KB Output is correct
33 Correct 56 ms 36012 KB Output is correct
34 Correct 69 ms 27488 KB Output is correct
35 Correct 12 ms 23972 KB Output is correct
36 Correct 62 ms 28316 KB Output is correct
37 Correct 12 ms 23892 KB Output is correct
38 Correct 11 ms 23892 KB Output is correct
39 Correct 47 ms 27348 KB Output is correct
40 Correct 11 ms 24232 KB Output is correct
41 Correct 70 ms 27544 KB Output is correct
42 Correct 57 ms 30156 KB Output is correct
43 Correct 15 ms 23764 KB Output is correct
44 Correct 76 ms 36304 KB Output is correct
45 Correct 70 ms 32276 KB Output is correct
46 Correct 11 ms 23884 KB Output is correct
47 Correct 12 ms 23976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 27360 KB Output is correct
2 Correct 45 ms 30020 KB Output is correct
3 Correct 12 ms 23976 KB Output is correct
4 Correct 11 ms 23972 KB Output is correct
5 Correct 10 ms 23832 KB Output is correct
6 Correct 12 ms 23844 KB Output is correct
7 Correct 10 ms 23892 KB Output is correct
8 Correct 77 ms 28064 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 70 ms 27548 KB Output is correct
11 Correct 11 ms 23824 KB Output is correct
12 Correct 72 ms 27420 KB Output is correct
13 Correct 90 ms 28072 KB Output is correct
14 Correct 84 ms 29004 KB Output is correct
15 Correct 39 ms 27456 KB Output is correct
16 Correct 11 ms 23892 KB Output is correct
17 Correct 10 ms 23764 KB Output is correct
18 Correct 56 ms 29796 KB Output is correct
19 Correct 78 ms 33316 KB Output is correct
20 Correct 11 ms 23972 KB Output is correct
21 Correct 9 ms 23832 KB Output is correct
22 Correct 45 ms 28248 KB Output is correct
23 Correct 11 ms 23952 KB Output is correct
24 Correct 77 ms 27596 KB Output is correct
25 Correct 95 ms 32612 KB Output is correct
26 Correct 12 ms 23996 KB Output is correct
27 Correct 13 ms 24020 KB Output is correct
28 Correct 11 ms 23892 KB Output is correct
29 Correct 11 ms 23884 KB Output is correct
30 Correct 13 ms 23944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Correct 10 ms 23764 KB Output is correct
4 Correct 10 ms 23864 KB Output is correct
5 Correct 10 ms 23828 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 10 ms 23836 KB Output is correct
8 Correct 11 ms 23820 KB Output is correct
9 Correct 10 ms 23764 KB Output is correct
10 Correct 10 ms 23804 KB Output is correct
11 Correct 11 ms 23832 KB Output is correct
12 Correct 10 ms 23764 KB Output is correct
13 Correct 10 ms 23764 KB Output is correct
14 Correct 10 ms 23752 KB Output is correct
15 Correct 10 ms 23832 KB Output is correct
16 Correct 10 ms 23764 KB Output is correct
17 Correct 10 ms 23744 KB Output is correct
18 Correct 10 ms 23764 KB Output is correct
19 Correct 10 ms 23836 KB Output is correct
20 Correct 11 ms 23828 KB Output is correct
21 Correct 10 ms 23832 KB Output is correct
22 Correct 9 ms 23764 KB Output is correct
23 Correct 10 ms 23836 KB Output is correct
24 Correct 10 ms 23856 KB Output is correct
25 Correct 10 ms 23764 KB Output is correct
26 Correct 14 ms 23952 KB Output is correct
27 Correct 14 ms 23916 KB Output is correct
28 Correct 12 ms 24148 KB Output is correct
29 Correct 14 ms 23972 KB Output is correct
30 Correct 12 ms 23960 KB Output is correct
31 Correct 10 ms 23764 KB Output is correct
32 Correct 11 ms 24100 KB Output is correct
33 Correct 9 ms 23764 KB Output is correct
34 Correct 12 ms 23940 KB Output is correct
35 Correct 13 ms 24020 KB Output is correct
36 Correct 11 ms 23892 KB Output is correct
37 Correct 12 ms 23892 KB Output is correct
38 Correct 12 ms 23824 KB Output is correct
39 Correct 12 ms 23884 KB Output is correct
40 Correct 11 ms 23976 KB Output is correct
41 Correct 11 ms 23892 KB Output is correct
42 Correct 11 ms 23892 KB Output is correct
43 Correct 11 ms 24148 KB Output is correct
44 Correct 10 ms 23832 KB Output is correct
45 Correct 11 ms 23992 KB Output is correct
46 Correct 11 ms 23984 KB Output is correct
47 Correct 10 ms 23764 KB Output is correct
48 Correct 12 ms 23892 KB Output is correct
49 Correct 11 ms 24056 KB Output is correct
50 Correct 11 ms 24020 KB Output is correct
51 Correct 11 ms 23976 KB Output is correct
52 Correct 12 ms 23972 KB Output is correct
53 Correct 13 ms 23900 KB Output is correct
54 Correct 12 ms 23956 KB Output is correct
55 Correct 10 ms 23828 KB Output is correct
56 Correct 39 ms 27340 KB Output is correct
57 Correct 72 ms 27476 KB Output is correct
58 Correct 14 ms 23928 KB Output is correct
59 Correct 11 ms 23764 KB Output is correct
60 Correct 11 ms 23764 KB Output is correct
61 Correct 84 ms 27532 KB Output is correct
62 Correct 11 ms 23892 KB Output is correct
63 Correct 56 ms 36012 KB Output is correct
64 Correct 69 ms 27488 KB Output is correct
65 Correct 12 ms 23972 KB Output is correct
66 Correct 62 ms 28316 KB Output is correct
67 Correct 12 ms 23892 KB Output is correct
68 Correct 11 ms 23892 KB Output is correct
69 Correct 47 ms 27348 KB Output is correct
70 Correct 11 ms 24232 KB Output is correct
71 Correct 70 ms 27544 KB Output is correct
72 Correct 57 ms 30156 KB Output is correct
73 Correct 15 ms 23764 KB Output is correct
74 Correct 76 ms 36304 KB Output is correct
75 Correct 70 ms 32276 KB Output is correct
76 Correct 11 ms 23884 KB Output is correct
77 Correct 12 ms 23976 KB Output is correct
78 Correct 45 ms 27360 KB Output is correct
79 Correct 45 ms 30020 KB Output is correct
80 Correct 12 ms 23976 KB Output is correct
81 Correct 11 ms 23972 KB Output is correct
82 Correct 10 ms 23832 KB Output is correct
83 Correct 12 ms 23844 KB Output is correct
84 Correct 10 ms 23892 KB Output is correct
85 Correct 77 ms 28064 KB Output is correct
86 Correct 12 ms 23892 KB Output is correct
87 Correct 70 ms 27548 KB Output is correct
88 Correct 11 ms 23824 KB Output is correct
89 Correct 72 ms 27420 KB Output is correct
90 Correct 90 ms 28072 KB Output is correct
91 Correct 84 ms 29004 KB Output is correct
92 Correct 39 ms 27456 KB Output is correct
93 Correct 11 ms 23892 KB Output is correct
94 Correct 10 ms 23764 KB Output is correct
95 Correct 56 ms 29796 KB Output is correct
96 Correct 78 ms 33316 KB Output is correct
97 Correct 11 ms 23972 KB Output is correct
98 Correct 9 ms 23832 KB Output is correct
99 Correct 45 ms 28248 KB Output is correct
100 Correct 11 ms 23952 KB Output is correct
101 Correct 77 ms 27596 KB Output is correct
102 Correct 95 ms 32612 KB Output is correct
103 Correct 12 ms 23996 KB Output is correct
104 Correct 13 ms 24020 KB Output is correct
105 Correct 11 ms 23892 KB Output is correct
106 Correct 11 ms 23884 KB Output is correct
107 Correct 13 ms 23944 KB Output is correct
108 Correct 274 ms 39128 KB Output is correct
109 Correct 509 ms 58844 KB Output is correct
110 Correct 481 ms 72300 KB Output is correct
111 Correct 639 ms 70568 KB Output is correct
112 Correct 560 ms 63168 KB Output is correct
113 Correct 299 ms 47292 KB Output is correct
114 Correct 660 ms 37460 KB Output is correct
115 Correct 641 ms 37488 KB Output is correct
116 Correct 706 ms 38228 KB Output is correct
117 Correct 762 ms 44616 KB Output is correct
118 Correct 658 ms 37184 KB Output is correct
119 Correct 709 ms 44432 KB Output is correct
120 Correct 630 ms 63784 KB Output is correct
121 Correct 695 ms 44228 KB Output is correct
122 Correct 730 ms 40860 KB Output is correct
123 Correct 340 ms 47564 KB Output is correct
124 Correct 770 ms 41100 KB Output is correct