Submission #155636

# Submission time Handle Problem Language Result Execution time Memory
155636 2019-09-29T12:14:50 Z HellAngel Mergers (JOI19_mergers) C++14
100 / 100
1269 ms 100720 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e5 + 7;
const int maxState = (1 << 8);
int n, m, u, v, Min[maxn], Max[maxn], deg[maxn], cnt, sum[maxn], cur, st[maxn], last1 = 0, en[maxn], ITSum[maxn * 4], ITMax[4 * maxn];
vector<int> vt[maxn], col, ahihi;
vector<int> prvans, ans;
set<int> St;
bool ok[maxn];
/// first: sum
/// second: last
void Update(int id, int l, int r, int pos, pair<int, int> x)
{
    if(l == r)
    {
        ITSum[id] = x.first;
        ITMax[id] = x.second;
        return;
    }
    int mid = l + r >> 1;
    if(pos <= mid) Update(id * 2,l , mid, pos, x);
    else Update(id * 2 + 1, mid + 1, r, pos, x);
    ITSum[id] = ITSum[id * 2] + ITSum[id * 2 + 1];
    ITMax[id] = max(ITMax[id * 2], ITMax[id * 2 + 1]);
}

pair<int, int> Get(int id, int l, int r, int u, int v)
{
    if(l > v || r < u) return {0, 0};
    if(u <= l && r <= v)
    {
        return {ITSum[id], ITMax[id]};
    }
    int mid = l + r >> 1;
    pair<int, int> gau1 = Get(id * 2, l, mid, u, v);
    pair<int, int> gau2 = Get(id * 2 + 1, mid + 1, r, u, v);
    return {gau1.first + gau2.first, max(gau1.second, gau2.second)};
}

void DFS(int u, int p)
{
    st[u] = ++cnt;
    for(auto v: vt[u])
    {
        if(v != p) DFS(v, u);
    }
    en[u] = cnt;
}

void Add(int x)
{
    while(++cur < prvans.size() && st[prvans[cur]] <= en[x])
    {
        deg[x]++;
        deg[prvans[cur]]++;
        Add(prvans[cur]);
    }
    cur--;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= m; i++) Min[i] = INT_MAX;
    for(int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        vt[u].push_back(v);
        vt[v].push_back(u);
    }
    DFS(1, 1);
    for(int i = 1; i <= n; i++)
    {
        ahihi.push_back(i);
        int x;
        cin >> x;
        Max[x] = max(Max[x], st[i]);
        Min[x] = min(Min[x], st[i]);
        sum[x]++;
    }
    for(int i = 1; i <= m; i++) Update(1, 1, n, Min[i], {sum[i], Max[i]});
    for(int i = 1; i <= n; i++)
    {
        pair<int, int> gau = Get(1, 1, n, st[i], en[i]);
        if(gau.second <= en[i] && gau.first == en[i] - st[i] + 1)
        {
            ok[i] = true;
            prvans.push_back(i);
        }
    }
    sort(prvans.begin(), prvans.end(), [](const int &a, const int &b)
         {
            return st[a] < st[b];
         });
    Add(1);
    cout << (count(deg + 1, deg + n + 1, 1) + 1) / 2;
}

Compilation message

mergers.cpp: In function 'void Update(long long int, long long int, long long int, long long int, std::pair<long long int, long long int>)':
mergers.cpp:21:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
mergers.cpp: In function 'std::pair<long long int, long long int> Get(long long int, long long int, long long int, long long int, long long int)':
mergers.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
mergers.cpp: In function 'void Add(long long int)':
mergers.cpp:53:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(++cur < prvans.size() && st[prvans[cur]] <= en[x])
           ~~~~~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:66:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:66:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 13 ms 12152 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12156 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 15 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 15 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 15 ms 12152 KB Output is correct
14 Correct 16 ms 12280 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12188 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 13 ms 12280 KB Output is correct
24 Correct 14 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 13 ms 12152 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12156 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 15 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 15 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 15 ms 12152 KB Output is correct
14 Correct 16 ms 12280 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12188 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 13 ms 12280 KB Output is correct
24 Correct 14 ms 12124 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 19 ms 12636 KB Output is correct
27 Correct 18 ms 12540 KB Output is correct
28 Correct 15 ms 12664 KB Output is correct
29 Correct 16 ms 12664 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 13 ms 12152 KB Output is correct
32 Correct 16 ms 12664 KB Output is correct
33 Correct 13 ms 12152 KB Output is correct
34 Correct 15 ms 12408 KB Output is correct
35 Correct 16 ms 12664 KB Output is correct
36 Correct 15 ms 12552 KB Output is correct
37 Correct 15 ms 12664 KB Output is correct
38 Correct 13 ms 12152 KB Output is correct
39 Correct 15 ms 12664 KB Output is correct
40 Correct 14 ms 12412 KB Output is correct
41 Correct 15 ms 12536 KB Output is correct
42 Correct 18 ms 12536 KB Output is correct
43 Correct 18 ms 12536 KB Output is correct
44 Correct 15 ms 12280 KB Output is correct
45 Correct 18 ms 12664 KB Output is correct
46 Correct 18 ms 12664 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 18 ms 12664 KB Output is correct
50 Correct 19 ms 12792 KB Output is correct
51 Correct 18 ms 12536 KB Output is correct
52 Correct 14 ms 12408 KB Output is correct
53 Correct 15 ms 12740 KB Output is correct
54 Correct 17 ms 12408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 13 ms 12152 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12156 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 15 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 15 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 15 ms 12152 KB Output is correct
14 Correct 16 ms 12280 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12188 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 13 ms 12280 KB Output is correct
24 Correct 14 ms 12124 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 89 ms 20624 KB Output is correct
27 Correct 115 ms 20080 KB Output is correct
28 Correct 15 ms 12536 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 16 ms 12152 KB Output is correct
31 Correct 131 ms 21496 KB Output is correct
32 Correct 17 ms 12536 KB Output is correct
33 Correct 122 ms 23916 KB Output is correct
34 Correct 106 ms 20084 KB Output is correct
35 Correct 16 ms 12520 KB Output is correct
36 Correct 152 ms 21452 KB Output is correct
37 Correct 17 ms 12408 KB Output is correct
38 Correct 18 ms 12536 KB Output is correct
39 Correct 97 ms 20244 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 133 ms 19968 KB Output is correct
42 Correct 124 ms 20848 KB Output is correct
43 Correct 14 ms 12152 KB Output is correct
44 Correct 134 ms 23932 KB Output is correct
45 Correct 130 ms 22892 KB Output is correct
46 Correct 15 ms 12408 KB Output is correct
47 Correct 15 ms 12408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 19532 KB Output is correct
2 Correct 157 ms 27172 KB Output is correct
3 Correct 16 ms 12536 KB Output is correct
4 Correct 16 ms 12536 KB Output is correct
5 Correct 14 ms 12280 KB Output is correct
6 Correct 13 ms 12152 KB Output is correct
7 Correct 16 ms 12536 KB Output is correct
8 Correct 155 ms 26480 KB Output is correct
9 Correct 20 ms 12536 KB Output is correct
10 Correct 138 ms 24912 KB Output is correct
11 Correct 14 ms 12152 KB Output is correct
12 Correct 140 ms 24304 KB Output is correct
13 Correct 180 ms 26536 KB Output is correct
14 Correct 227 ms 28400 KB Output is correct
15 Correct 95 ms 20328 KB Output is correct
16 Correct 16 ms 12664 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 146 ms 27976 KB Output is correct
19 Correct 195 ms 30320 KB Output is correct
20 Correct 15 ms 12664 KB Output is correct
21 Correct 0 ms 12152 KB Output is correct
22 Correct 149 ms 26728 KB Output is correct
23 Correct 19 ms 12536 KB Output is correct
24 Correct 142 ms 25004 KB Output is correct
25 Correct 191 ms 30440 KB Output is correct
26 Correct 16 ms 12664 KB Output is correct
27 Correct 17 ms 12664 KB Output is correct
28 Correct 16 ms 12536 KB Output is correct
29 Correct 15 ms 12524 KB Output is correct
30 Correct 15 ms 12664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 13 ms 12152 KB Output is correct
3 Correct 15 ms 12152 KB Output is correct
4 Correct 13 ms 12152 KB Output is correct
5 Correct 13 ms 12152 KB Output is correct
6 Correct 14 ms 12152 KB Output is correct
7 Correct 13 ms 12156 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 15 ms 12152 KB Output is correct
10 Correct 15 ms 12152 KB Output is correct
11 Correct 15 ms 12152 KB Output is correct
12 Correct 13 ms 12152 KB Output is correct
13 Correct 15 ms 12152 KB Output is correct
14 Correct 16 ms 12280 KB Output is correct
15 Correct 13 ms 12152 KB Output is correct
16 Correct 13 ms 12152 KB Output is correct
17 Correct 13 ms 12152 KB Output is correct
18 Correct 13 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 13 ms 12188 KB Output is correct
21 Correct 13 ms 12152 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 13 ms 12280 KB Output is correct
24 Correct 14 ms 12124 KB Output is correct
25 Correct 13 ms 12152 KB Output is correct
26 Correct 19 ms 12636 KB Output is correct
27 Correct 18 ms 12540 KB Output is correct
28 Correct 15 ms 12664 KB Output is correct
29 Correct 16 ms 12664 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 13 ms 12152 KB Output is correct
32 Correct 16 ms 12664 KB Output is correct
33 Correct 13 ms 12152 KB Output is correct
34 Correct 15 ms 12408 KB Output is correct
35 Correct 16 ms 12664 KB Output is correct
36 Correct 15 ms 12552 KB Output is correct
37 Correct 15 ms 12664 KB Output is correct
38 Correct 13 ms 12152 KB Output is correct
39 Correct 15 ms 12664 KB Output is correct
40 Correct 14 ms 12412 KB Output is correct
41 Correct 15 ms 12536 KB Output is correct
42 Correct 18 ms 12536 KB Output is correct
43 Correct 18 ms 12536 KB Output is correct
44 Correct 15 ms 12280 KB Output is correct
45 Correct 18 ms 12664 KB Output is correct
46 Correct 18 ms 12664 KB Output is correct
47 Correct 13 ms 12152 KB Output is correct
48 Correct 15 ms 12536 KB Output is correct
49 Correct 18 ms 12664 KB Output is correct
50 Correct 19 ms 12792 KB Output is correct
51 Correct 18 ms 12536 KB Output is correct
52 Correct 14 ms 12408 KB Output is correct
53 Correct 15 ms 12740 KB Output is correct
54 Correct 17 ms 12408 KB Output is correct
55 Correct 13 ms 12152 KB Output is correct
56 Correct 89 ms 20624 KB Output is correct
57 Correct 115 ms 20080 KB Output is correct
58 Correct 15 ms 12536 KB Output is correct
59 Correct 13 ms 12152 KB Output is correct
60 Correct 16 ms 12152 KB Output is correct
61 Correct 131 ms 21496 KB Output is correct
62 Correct 17 ms 12536 KB Output is correct
63 Correct 122 ms 23916 KB Output is correct
64 Correct 106 ms 20084 KB Output is correct
65 Correct 16 ms 12520 KB Output is correct
66 Correct 152 ms 21452 KB Output is correct
67 Correct 17 ms 12408 KB Output is correct
68 Correct 18 ms 12536 KB Output is correct
69 Correct 97 ms 20244 KB Output is correct
70 Correct 15 ms 12536 KB Output is correct
71 Correct 133 ms 19968 KB Output is correct
72 Correct 124 ms 20848 KB Output is correct
73 Correct 14 ms 12152 KB Output is correct
74 Correct 134 ms 23932 KB Output is correct
75 Correct 130 ms 22892 KB Output is correct
76 Correct 15 ms 12408 KB Output is correct
77 Correct 15 ms 12408 KB Output is correct
78 Correct 94 ms 19532 KB Output is correct
79 Correct 157 ms 27172 KB Output is correct
80 Correct 16 ms 12536 KB Output is correct
81 Correct 16 ms 12536 KB Output is correct
82 Correct 14 ms 12280 KB Output is correct
83 Correct 13 ms 12152 KB Output is correct
84 Correct 16 ms 12536 KB Output is correct
85 Correct 155 ms 26480 KB Output is correct
86 Correct 20 ms 12536 KB Output is correct
87 Correct 138 ms 24912 KB Output is correct
88 Correct 14 ms 12152 KB Output is correct
89 Correct 140 ms 24304 KB Output is correct
90 Correct 180 ms 26536 KB Output is correct
91 Correct 227 ms 28400 KB Output is correct
92 Correct 95 ms 20328 KB Output is correct
93 Correct 16 ms 12664 KB Output is correct
94 Correct 13 ms 12152 KB Output is correct
95 Correct 146 ms 27976 KB Output is correct
96 Correct 195 ms 30320 KB Output is correct
97 Correct 15 ms 12664 KB Output is correct
98 Correct 0 ms 12152 KB Output is correct
99 Correct 149 ms 26728 KB Output is correct
100 Correct 19 ms 12536 KB Output is correct
101 Correct 142 ms 25004 KB Output is correct
102 Correct 191 ms 30440 KB Output is correct
103 Correct 16 ms 12664 KB Output is correct
104 Correct 17 ms 12664 KB Output is correct
105 Correct 16 ms 12536 KB Output is correct
106 Correct 15 ms 12524 KB Output is correct
107 Correct 15 ms 12664 KB Output is correct
108 Correct 724 ms 79712 KB Output is correct
109 Correct 765 ms 58580 KB Output is correct
110 Correct 772 ms 65296 KB Output is correct
111 Correct 1228 ms 100132 KB Output is correct
112 Correct 1200 ms 98568 KB Output is correct
113 Correct 896 ms 88196 KB Output is correct
114 Correct 781 ms 50752 KB Output is correct
115 Correct 675 ms 50516 KB Output is correct
116 Correct 816 ms 73944 KB Output is correct
117 Correct 1198 ms 89192 KB Output is correct
118 Correct 822 ms 64844 KB Output is correct
119 Correct 1114 ms 89328 KB Output is correct
120 Correct 1269 ms 100720 KB Output is correct
121 Correct 1144 ms 89016 KB Output is correct
122 Correct 929 ms 82036 KB Output is correct
123 Correct 922 ms 90080 KB Output is correct
124 Correct 1155 ms 83892 KB Output is correct