Submission #443749

# Submission time Handle Problem Language Result Execution time Memory
443749 2021-07-12T02:27:19 Z Killer2501 Mergers (JOI19_mergers) C++14
100 / 100
1338 ms 213000 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "slingshot"
#define pll pair<ll, ll>
#define pii pair<pll, ll>
#define fi first
#define se second
#define ull unsigned long long

using namespace std;
const ll mod =  2111992;
const ll N = 5e5+5;
const int base = 300;
ll n, m, t, ans, k, a[N], b[N], c[N], tong,  cnt, q, d[N], P[N][21], h[N], lab[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct point
{
    ll x, y;
}p[N];
vector<ll> dx, dy;
vector<ll> adj[N], kq, gr[N];

string s[N];
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
ll A(ll u, ll v)
{
    if(u > v)return 0;
    return b[v] * c[v-u] % mod;
}
ll findp(ll u)
{
    return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(ll u, ll v)
{
    u = findp(u);
    v = findp(v);
    if(u == v)return;
    if(lab[u] > lab[v])swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
}
void add(ll id)
{
    for(; id <= n;id += id & -id)++d[id];
}
ll get(ll id)
{
    ll total = 0;
    for(; id; id -= id & -id)total += d[id];
    return total;
}
ll lwr(ll x)
{
    return lower_bound(kq.begin(), kq.end(), x) - kq.begin() + 1;
}
void dfs(ll u, ll p)
{
    a[u] = ++m;
    for(int i = 1; i <= 20; i ++)P[u][i] = P[P[u][i-1]][i-1];
    for(ll v : adj[u])
    {
        if(v == p)continue;
        h[v] = h[u] + 1;
        P[v][0] = u;
        dfs(v, u);
    }
}
bool cmp(const ll& x, const ll& y)
{
    return a[x] < a[y];
}
ll lca(ll u, ll v)
{
    if(h[u] < h[v])swap(u, v);
    ll log = log2(h[u]);
    for(int i = log; i >= 0; i --)if(h[u] >= h[v] + (1<<i))u = P[u][i];
    if(u == v)return u;
    for(int i = log; i >= 0; i --)
    {
        if(P[u][i] && P[u][i] != P[v][i])
        {
            u = P[u][i];
            v = P[v][i];
        }
    }
    return P[u][0];
}
void cal(ll u, ll p)
{
    for(ll v : adj[u])
    {
        if(v == p)continue;
        cal(v, u);
        c[u] += c[v];
    }
    if(c[u])hop(u, P[u][0]);
}
void sol()
{
    cin >> n >> k;
    for(int i = 1; i < n; i ++)
    {
        ll x, y;
        cin >> x >> y;
        p[i].x = x;
        p[i].y = y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    for(int i = 1; i <= n; i ++)
    {
        cin >> b[i];
        gr[b[i]].pb(i);
    }
    dfs(1, 0);
    for(int i = 1; i <= k; i ++)
    {
        sort(gr[i].begin(), gr[i].end(), cmp);
        for(int j = 1; j < gr[i].size(); j ++)
        {
            ll x = gr[i][j], y = gr[i][j-1], z = lca(x, y);
            ++c[x];
            ++c[y];
            c[z] -= 2;
        }
    }
    fill_n(lab, n+2, -1);
    cal(1, 0);
    for(int i = 1; i < n; i ++)
    {
        ll u = findp(p[i].x), v = findp(p[i].y);
        if(u != v)
        {
            ++d[u];
            ++d[v];
        }
    }
    for(int i = 1; i <= n; i ++)ans += (d[i] == 1);
    cout << (ans + 1) / 2;
}
int main()
{
    if(fopen(task".in", "r")){
       freopen(task".in", "r", stdin);
       freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}

Compilation message

mergers.cpp: In function 'void sol()':
mergers.cpp:131:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         for(int j = 1; j < gr[i].size(); j ++)
      |                        ~~^~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:156:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |        freopen(task".in", "r", stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:157:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |        freopen(task".out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39500 KB Output is correct
2 Correct 21 ms 39500 KB Output is correct
3 Correct 22 ms 39524 KB Output is correct
4 Correct 21 ms 39500 KB Output is correct
5 Correct 21 ms 39444 KB Output is correct
6 Correct 21 ms 39464 KB Output is correct
7 Correct 21 ms 39400 KB Output is correct
8 Correct 22 ms 39500 KB Output is correct
9 Correct 21 ms 39400 KB Output is correct
10 Correct 22 ms 39492 KB Output is correct
11 Correct 21 ms 39500 KB Output is correct
12 Correct 21 ms 39468 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 21 ms 39500 KB Output is correct
15 Correct 21 ms 39496 KB Output is correct
16 Correct 24 ms 39452 KB Output is correct
17 Correct 21 ms 39400 KB Output is correct
18 Correct 21 ms 39500 KB Output is correct
19 Correct 24 ms 39500 KB Output is correct
20 Correct 22 ms 39432 KB Output is correct
21 Correct 24 ms 39512 KB Output is correct
22 Correct 22 ms 39544 KB Output is correct
23 Correct 21 ms 39500 KB Output is correct
24 Correct 21 ms 39460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39500 KB Output is correct
2 Correct 21 ms 39500 KB Output is correct
3 Correct 22 ms 39524 KB Output is correct
4 Correct 21 ms 39500 KB Output is correct
5 Correct 21 ms 39444 KB Output is correct
6 Correct 21 ms 39464 KB Output is correct
7 Correct 21 ms 39400 KB Output is correct
8 Correct 22 ms 39500 KB Output is correct
9 Correct 21 ms 39400 KB Output is correct
10 Correct 22 ms 39492 KB Output is correct
11 Correct 21 ms 39500 KB Output is correct
12 Correct 21 ms 39468 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 21 ms 39500 KB Output is correct
15 Correct 21 ms 39496 KB Output is correct
16 Correct 24 ms 39452 KB Output is correct
17 Correct 21 ms 39400 KB Output is correct
18 Correct 21 ms 39500 KB Output is correct
19 Correct 24 ms 39500 KB Output is correct
20 Correct 22 ms 39432 KB Output is correct
21 Correct 24 ms 39512 KB Output is correct
22 Correct 22 ms 39544 KB Output is correct
23 Correct 21 ms 39500 KB Output is correct
24 Correct 21 ms 39460 KB Output is correct
25 Correct 22 ms 39472 KB Output is correct
26 Correct 24 ms 40348 KB Output is correct
27 Correct 24 ms 40364 KB Output is correct
28 Correct 25 ms 40524 KB Output is correct
29 Correct 24 ms 40448 KB Output is correct
30 Correct 24 ms 40376 KB Output is correct
31 Correct 21 ms 39516 KB Output is correct
32 Correct 25 ms 40580 KB Output is correct
33 Correct 22 ms 39400 KB Output is correct
34 Correct 25 ms 40268 KB Output is correct
35 Correct 23 ms 40396 KB Output is correct
36 Correct 24 ms 40308 KB Output is correct
37 Correct 24 ms 40356 KB Output is correct
38 Correct 21 ms 39440 KB Output is correct
39 Correct 27 ms 40356 KB Output is correct
40 Correct 25 ms 40252 KB Output is correct
41 Correct 24 ms 40396 KB Output is correct
42 Correct 24 ms 40336 KB Output is correct
43 Correct 25 ms 40480 KB Output is correct
44 Correct 23 ms 39456 KB Output is correct
45 Correct 25 ms 40396 KB Output is correct
46 Correct 24 ms 40372 KB Output is correct
47 Correct 22 ms 39408 KB Output is correct
48 Correct 24 ms 40292 KB Output is correct
49 Correct 24 ms 40372 KB Output is correct
50 Correct 25 ms 40396 KB Output is correct
51 Correct 24 ms 40320 KB Output is correct
52 Correct 25 ms 40360 KB Output is correct
53 Correct 24 ms 40396 KB Output is correct
54 Correct 26 ms 40284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39500 KB Output is correct
2 Correct 21 ms 39500 KB Output is correct
3 Correct 22 ms 39524 KB Output is correct
4 Correct 21 ms 39500 KB Output is correct
5 Correct 21 ms 39444 KB Output is correct
6 Correct 21 ms 39464 KB Output is correct
7 Correct 21 ms 39400 KB Output is correct
8 Correct 22 ms 39500 KB Output is correct
9 Correct 21 ms 39400 KB Output is correct
10 Correct 22 ms 39492 KB Output is correct
11 Correct 21 ms 39500 KB Output is correct
12 Correct 21 ms 39468 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 21 ms 39500 KB Output is correct
15 Correct 21 ms 39496 KB Output is correct
16 Correct 24 ms 39452 KB Output is correct
17 Correct 21 ms 39400 KB Output is correct
18 Correct 21 ms 39500 KB Output is correct
19 Correct 24 ms 39500 KB Output is correct
20 Correct 22 ms 39432 KB Output is correct
21 Correct 24 ms 39512 KB Output is correct
22 Correct 22 ms 39544 KB Output is correct
23 Correct 21 ms 39500 KB Output is correct
24 Correct 21 ms 39460 KB Output is correct
25 Correct 22 ms 39468 KB Output is correct
26 Correct 144 ms 67860 KB Output is correct
27 Correct 175 ms 67548 KB Output is correct
28 Correct 24 ms 40268 KB Output is correct
29 Correct 22 ms 39492 KB Output is correct
30 Correct 21 ms 39500 KB Output is correct
31 Correct 169 ms 67736 KB Output is correct
32 Correct 24 ms 40376 KB Output is correct
33 Correct 216 ms 72788 KB Output is correct
34 Correct 216 ms 67616 KB Output is correct
35 Correct 24 ms 40268 KB Output is correct
36 Correct 181 ms 67840 KB Output is correct
37 Correct 25 ms 40440 KB Output is correct
38 Correct 24 ms 40280 KB Output is correct
39 Correct 132 ms 67796 KB Output is correct
40 Correct 26 ms 40496 KB Output is correct
41 Correct 171 ms 67380 KB Output is correct
42 Correct 206 ms 68944 KB Output is correct
43 Correct 23 ms 39472 KB Output is correct
44 Correct 215 ms 72788 KB Output is correct
45 Correct 243 ms 71364 KB Output is correct
46 Correct 28 ms 40260 KB Output is correct
47 Correct 26 ms 40388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 67844 KB Output is correct
2 Correct 109 ms 70144 KB Output is correct
3 Correct 25 ms 40396 KB Output is correct
4 Correct 23 ms 40292 KB Output is correct
5 Correct 22 ms 39400 KB Output is correct
6 Correct 21 ms 39500 KB Output is correct
7 Correct 25 ms 40268 KB Output is correct
8 Correct 177 ms 69444 KB Output is correct
9 Correct 25 ms 40340 KB Output is correct
10 Correct 175 ms 68548 KB Output is correct
11 Correct 25 ms 39476 KB Output is correct
12 Correct 200 ms 68128 KB Output is correct
13 Correct 164 ms 69316 KB Output is correct
14 Correct 136 ms 70804 KB Output is correct
15 Correct 164 ms 67756 KB Output is correct
16 Correct 24 ms 40364 KB Output is correct
17 Correct 22 ms 39460 KB Output is correct
18 Correct 120 ms 70696 KB Output is correct
19 Correct 165 ms 73352 KB Output is correct
20 Correct 28 ms 40268 KB Output is correct
21 Correct 25 ms 39468 KB Output is correct
22 Correct 136 ms 69560 KB Output is correct
23 Correct 25 ms 40396 KB Output is correct
24 Correct 179 ms 68824 KB Output is correct
25 Correct 183 ms 73080 KB Output is correct
26 Correct 25 ms 40380 KB Output is correct
27 Correct 24 ms 40416 KB Output is correct
28 Correct 25 ms 40348 KB Output is correct
29 Correct 24 ms 40376 KB Output is correct
30 Correct 25 ms 40304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39500 KB Output is correct
2 Correct 21 ms 39500 KB Output is correct
3 Correct 22 ms 39524 KB Output is correct
4 Correct 21 ms 39500 KB Output is correct
5 Correct 21 ms 39444 KB Output is correct
6 Correct 21 ms 39464 KB Output is correct
7 Correct 21 ms 39400 KB Output is correct
8 Correct 22 ms 39500 KB Output is correct
9 Correct 21 ms 39400 KB Output is correct
10 Correct 22 ms 39492 KB Output is correct
11 Correct 21 ms 39500 KB Output is correct
12 Correct 21 ms 39468 KB Output is correct
13 Correct 21 ms 39520 KB Output is correct
14 Correct 21 ms 39500 KB Output is correct
15 Correct 21 ms 39496 KB Output is correct
16 Correct 24 ms 39452 KB Output is correct
17 Correct 21 ms 39400 KB Output is correct
18 Correct 21 ms 39500 KB Output is correct
19 Correct 24 ms 39500 KB Output is correct
20 Correct 22 ms 39432 KB Output is correct
21 Correct 24 ms 39512 KB Output is correct
22 Correct 22 ms 39544 KB Output is correct
23 Correct 21 ms 39500 KB Output is correct
24 Correct 21 ms 39460 KB Output is correct
25 Correct 22 ms 39472 KB Output is correct
26 Correct 24 ms 40348 KB Output is correct
27 Correct 24 ms 40364 KB Output is correct
28 Correct 25 ms 40524 KB Output is correct
29 Correct 24 ms 40448 KB Output is correct
30 Correct 24 ms 40376 KB Output is correct
31 Correct 21 ms 39516 KB Output is correct
32 Correct 25 ms 40580 KB Output is correct
33 Correct 22 ms 39400 KB Output is correct
34 Correct 25 ms 40268 KB Output is correct
35 Correct 23 ms 40396 KB Output is correct
36 Correct 24 ms 40308 KB Output is correct
37 Correct 24 ms 40356 KB Output is correct
38 Correct 21 ms 39440 KB Output is correct
39 Correct 27 ms 40356 KB Output is correct
40 Correct 25 ms 40252 KB Output is correct
41 Correct 24 ms 40396 KB Output is correct
42 Correct 24 ms 40336 KB Output is correct
43 Correct 25 ms 40480 KB Output is correct
44 Correct 23 ms 39456 KB Output is correct
45 Correct 25 ms 40396 KB Output is correct
46 Correct 24 ms 40372 KB Output is correct
47 Correct 22 ms 39408 KB Output is correct
48 Correct 24 ms 40292 KB Output is correct
49 Correct 24 ms 40372 KB Output is correct
50 Correct 25 ms 40396 KB Output is correct
51 Correct 24 ms 40320 KB Output is correct
52 Correct 25 ms 40360 KB Output is correct
53 Correct 24 ms 40396 KB Output is correct
54 Correct 26 ms 40284 KB Output is correct
55 Correct 22 ms 39468 KB Output is correct
56 Correct 144 ms 67860 KB Output is correct
57 Correct 175 ms 67548 KB Output is correct
58 Correct 24 ms 40268 KB Output is correct
59 Correct 22 ms 39492 KB Output is correct
60 Correct 21 ms 39500 KB Output is correct
61 Correct 169 ms 67736 KB Output is correct
62 Correct 24 ms 40376 KB Output is correct
63 Correct 216 ms 72788 KB Output is correct
64 Correct 216 ms 67616 KB Output is correct
65 Correct 24 ms 40268 KB Output is correct
66 Correct 181 ms 67840 KB Output is correct
67 Correct 25 ms 40440 KB Output is correct
68 Correct 24 ms 40280 KB Output is correct
69 Correct 132 ms 67796 KB Output is correct
70 Correct 26 ms 40496 KB Output is correct
71 Correct 171 ms 67380 KB Output is correct
72 Correct 206 ms 68944 KB Output is correct
73 Correct 23 ms 39472 KB Output is correct
74 Correct 215 ms 72788 KB Output is correct
75 Correct 243 ms 71364 KB Output is correct
76 Correct 28 ms 40260 KB Output is correct
77 Correct 26 ms 40388 KB Output is correct
78 Correct 142 ms 67844 KB Output is correct
79 Correct 109 ms 70144 KB Output is correct
80 Correct 25 ms 40396 KB Output is correct
81 Correct 23 ms 40292 KB Output is correct
82 Correct 22 ms 39400 KB Output is correct
83 Correct 21 ms 39500 KB Output is correct
84 Correct 25 ms 40268 KB Output is correct
85 Correct 177 ms 69444 KB Output is correct
86 Correct 25 ms 40340 KB Output is correct
87 Correct 175 ms 68548 KB Output is correct
88 Correct 25 ms 39476 KB Output is correct
89 Correct 200 ms 68128 KB Output is correct
90 Correct 164 ms 69316 KB Output is correct
91 Correct 136 ms 70804 KB Output is correct
92 Correct 164 ms 67756 KB Output is correct
93 Correct 24 ms 40364 KB Output is correct
94 Correct 22 ms 39460 KB Output is correct
95 Correct 120 ms 70696 KB Output is correct
96 Correct 165 ms 73352 KB Output is correct
97 Correct 28 ms 40268 KB Output is correct
98 Correct 25 ms 39468 KB Output is correct
99 Correct 136 ms 69560 KB Output is correct
100 Correct 25 ms 40396 KB Output is correct
101 Correct 179 ms 68824 KB Output is correct
102 Correct 183 ms 73080 KB Output is correct
103 Correct 25 ms 40380 KB Output is correct
104 Correct 24 ms 40416 KB Output is correct
105 Correct 25 ms 40348 KB Output is correct
106 Correct 24 ms 40376 KB Output is correct
107 Correct 25 ms 40304 KB Output is correct
108 Correct 795 ms 191020 KB Output is correct
109 Correct 1300 ms 191204 KB Output is correct
110 Correct 1338 ms 200212 KB Output is correct
111 Correct 1236 ms 213000 KB Output is correct
112 Correct 1202 ms 209284 KB Output is correct
113 Correct 673 ms 196708 KB Output is correct
114 Correct 1002 ms 179532 KB Output is correct
115 Correct 1039 ms 179620 KB Output is correct
116 Correct 1152 ms 187200 KB Output is correct
117 Correct 927 ms 197400 KB Output is correct
118 Correct 1002 ms 181708 KB Output is correct
119 Correct 902 ms 197196 KB Output is correct
120 Correct 1186 ms 209876 KB Output is correct
121 Correct 894 ms 197340 KB Output is correct
122 Correct 1037 ms 191544 KB Output is correct
123 Correct 649 ms 194312 KB Output is correct
124 Correct 1094 ms 191492 KB Output is correct