Submission #236456

# Submission time Handle Problem Language Result Execution time Memory
236456 2020-06-02T10:11:25 Z Charis02 Mergers (JOI19_mergers) C++14
100 / 100
1527 ms 251348 KB
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define mid (low+high)/2
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 500004

using namespace std;

struct dsu{
    ll par[N];
    ll sz[N];
    ll col[N];

    ll get(ll x)
    {
        return (par[x] == x) ? x : get(par[x]);
    }

    void join(ll x,ll y)
    {
        ll fx=get(x);
        ll fy = get(y);
        if(fx==fy)
            return;
        if(sz[fx]<sz[fy])
            swap(fx,fy);
        sz[fx]+=sz[fy];
        par[fy]=fx;
    }

    ll get_col(ll x)
    {
        return col[get(x)];
    }
};

struct segment_tree{
    ll val[8*N];

    void upd(ll low,ll high,ll pos,ll slow,ll v)
    {
        if(low==high&&low==slow)
        {
            val[pos]+=v;
            return;
        }
        if(low>slow||high<slow)
            return;
        upd(low,mid,pos*2+1,slow,v);
        upd(mid+1,high,pos*2+2,slow,v);
        val[pos]=val[pos*2+1]+val[pos*2+2];
        return;
    }

    ll query(ll low,ll high,ll pos,ll slow,ll shigh)
    {
        if(low>=slow&&high<=shigh)
            return val[pos];
        if(low>shigh||high<slow)
            return 0;
        return query(low,mid,pos*2+1,slow,shigh)+query(mid+1,high,pos*2+2,slow,shigh);
    }
};

ll n,k,answer;
vector < ll > graph[N];
vector < ll > cities[N];
vector < ll > queries[N];
ll cnt,colors;
ll ar[N],deg[N];
ll dp[N][20],tin[N],tout[N];
segment_tree seg;
dsu comp;

ll get_par(ll x,ll i)
{
    if(dp[x][i] != -1)
        return dp[x][i];
    return dp[x][i] = get_par(get_par(x,i-1),i-1);
}

bool is_ancestor(ll x,ll y)
{
    return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}

ll get_lca(ll x,ll y)
{
    if(is_ancestor(x,y))
        return x;
    if(is_ancestor(y,x))
        return y;

    for(int i = 19;i >= 0;i--)
    {
        if(!is_ancestor(get_par(x,i),y))
            x = get_par(x,i);
    }

    return get_par(x,0);
}

void init(ll cur,ll par)
{
    dp[cur][0] = par;
    tin[cur] = cnt++;

    rep(i,0,graph[cur].size())
    {
        ll v = graph[cur][i];
        if(v==par)
            continue;
        init(v,cur);
    }

    tout[cur]=cnt++;
    return;
}

void dfs(ll cur,ll par)
{
    rep(i,0,queries[cur].size())
        seg.upd(0,2*n,0,tin[queries[cur][i]],1);

    rep(i,0,graph[cur].size())
    {
        ll v= graph[cur][i];

        if(v==par)
            continue;

        if(seg.query(0,2*n,0,tin[v],tout[v])!=0)
            comp.join(cur,v);
        dfs(v,cur);
    }

    rep(i,0,queries[cur].size())
        seg.upd(0,2*n,0,tin[queries[cur][i]],-1);
}

void process_paths()
{
    rep(s,1,k+1)
    {
        ll x = cities[s][0];

        rep(j,1,cities[s].size())
        {
            ll y = cities[s][j];
            ll lca = get_lca(x,y);

            if(lca==x)
                queries[x].push_back(y);
            else if(lca==y)
                queries[y].push_back(x);
            else
            {
                queries[lca].push_back(x);
                queries[lca].push_back(y);
            }
        }
    }

    dfs(1,1);

    rep(i,1,n+1)
    {
        if(comp.get_col(i) == 0)
            comp.col[comp.get(i)] = ++colors;
        ar[i] = comp.get_col(i);
    }

    return;
}

void calc()
{
    rep(i,1,n+1)
    {
        rep(j,0,graph[i].size())
        {
            ll v = graph[i][j];
            if(ar[i] != ar[v])
                deg[ar[i]]++;
        }
    }

    ll leaves = 0;

    rep(i,1,colors+1)
    {
        if(deg[i] == 1)
            leaves++;
    }

    answer = (leaves+1)/2;

    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    rep(i,0,n-1)
    {
        ll a,b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    rep(i,1,n+1)
    {
        ll s;
        comp.par[i]=i;
        comp.col[i] = 0;
        comp.sz[i]=1;
        cin >> s;
        cities[s].push_back(i);
    }

    memset(seg.val,0,sizeof seg.val);
    memset(dp,-1,sizeof dp);
    init(1,1);
    process_paths();
    calc();

    cout << answer << endl;

    return 0;
}

/*
5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

3 2
2 1
3 1
2
2
1

5 2
1 2
4 5
4 2
3 4
2
1
2
1
2


*/

Compilation message

mergers.cpp: In function 'void init(long long int, long long int)':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:118:9:
     rep(i,0,graph[cur].size())
         ~~~~~~~~~~~~~~~~~~~~~       
mergers.cpp:118:5: note: in expansion of macro 'rep'
     rep(i,0,graph[cur].size())
     ^~~
mergers.cpp: In function 'void dfs(long long int, long long int)':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:132:9:
     rep(i,0,queries[cur].size())
         ~~~~~~~~~~~~~~~~~~~~~~~     
mergers.cpp:132:5: note: in expansion of macro 'rep'
     rep(i,0,queries[cur].size())
     ^~~
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:135:9:
     rep(i,0,graph[cur].size())
         ~~~~~~~~~~~~~~~~~~~~~       
mergers.cpp:135:5: note: in expansion of macro 'rep'
     rep(i,0,graph[cur].size())
     ^~~
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:147:9:
     rep(i,0,queries[cur].size())
         ~~~~~~~~~~~~~~~~~~~~~~~     
mergers.cpp:147:5: note: in expansion of macro 'rep'
     rep(i,0,queries[cur].size())
     ^~~
mergers.cpp: In function 'void process_paths()':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:157:13:
         rep(j,1,cities[s].size())
             ~~~~~~~~~~~~~~~~~~~~    
mergers.cpp:157:9: note: in expansion of macro 'rep'
         rep(j,1,cities[s].size())
         ^~~
mergers.cpp: In function 'void calc()':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:190:13:
         rep(j,0,graph[i].size())
             ~~~~~~~~~~~~~~~~~~~     
mergers.cpp:190:9: note: in expansion of macro 'rep'
         rep(j,0,graph[i].size())
         ^~~
# Verdict Execution time Memory Grader output
1 Correct 81 ms 145144 KB Output is correct
2 Correct 81 ms 145144 KB Output is correct
3 Correct 78 ms 145272 KB Output is correct
4 Correct 79 ms 145144 KB Output is correct
5 Correct 82 ms 145144 KB Output is correct
6 Correct 79 ms 145144 KB Output is correct
7 Correct 80 ms 145144 KB Output is correct
8 Correct 80 ms 145144 KB Output is correct
9 Correct 79 ms 145144 KB Output is correct
10 Correct 84 ms 145144 KB Output is correct
11 Correct 79 ms 145272 KB Output is correct
12 Correct 79 ms 145144 KB Output is correct
13 Correct 79 ms 145276 KB Output is correct
14 Correct 82 ms 145272 KB Output is correct
15 Correct 84 ms 145276 KB Output is correct
16 Correct 82 ms 145144 KB Output is correct
17 Correct 79 ms 145144 KB Output is correct
18 Correct 80 ms 145156 KB Output is correct
19 Correct 80 ms 145272 KB Output is correct
20 Correct 79 ms 145144 KB Output is correct
21 Correct 79 ms 145272 KB Output is correct
22 Correct 81 ms 145272 KB Output is correct
23 Correct 82 ms 145272 KB Output is correct
24 Correct 90 ms 145124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 145144 KB Output is correct
2 Correct 81 ms 145144 KB Output is correct
3 Correct 78 ms 145272 KB Output is correct
4 Correct 79 ms 145144 KB Output is correct
5 Correct 82 ms 145144 KB Output is correct
6 Correct 79 ms 145144 KB Output is correct
7 Correct 80 ms 145144 KB Output is correct
8 Correct 80 ms 145144 KB Output is correct
9 Correct 79 ms 145144 KB Output is correct
10 Correct 84 ms 145144 KB Output is correct
11 Correct 79 ms 145272 KB Output is correct
12 Correct 79 ms 145144 KB Output is correct
13 Correct 79 ms 145276 KB Output is correct
14 Correct 82 ms 145272 KB Output is correct
15 Correct 84 ms 145276 KB Output is correct
16 Correct 82 ms 145144 KB Output is correct
17 Correct 79 ms 145144 KB Output is correct
18 Correct 80 ms 145156 KB Output is correct
19 Correct 80 ms 145272 KB Output is correct
20 Correct 79 ms 145144 KB Output is correct
21 Correct 79 ms 145272 KB Output is correct
22 Correct 81 ms 145272 KB Output is correct
23 Correct 82 ms 145272 KB Output is correct
24 Correct 90 ms 145124 KB Output is correct
25 Correct 78 ms 145272 KB Output is correct
26 Correct 86 ms 145528 KB Output is correct
27 Correct 85 ms 145528 KB Output is correct
28 Correct 83 ms 145784 KB Output is correct
29 Correct 85 ms 145528 KB Output is correct
30 Correct 84 ms 145648 KB Output is correct
31 Correct 81 ms 145144 KB Output is correct
32 Correct 81 ms 145784 KB Output is correct
33 Correct 79 ms 145148 KB Output is correct
34 Correct 82 ms 145656 KB Output is correct
35 Correct 81 ms 145528 KB Output is correct
36 Correct 84 ms 145528 KB Output is correct
37 Correct 83 ms 145528 KB Output is correct
38 Correct 83 ms 145144 KB Output is correct
39 Correct 82 ms 145528 KB Output is correct
40 Correct 86 ms 145528 KB Output is correct
41 Correct 87 ms 145656 KB Output is correct
42 Correct 83 ms 145528 KB Output is correct
43 Correct 81 ms 145912 KB Output is correct
44 Correct 82 ms 145144 KB Output is correct
45 Correct 84 ms 145656 KB Output is correct
46 Correct 84 ms 145528 KB Output is correct
47 Correct 79 ms 145144 KB Output is correct
48 Correct 81 ms 145528 KB Output is correct
49 Correct 83 ms 145528 KB Output is correct
50 Correct 86 ms 145656 KB Output is correct
51 Correct 86 ms 145528 KB Output is correct
52 Correct 82 ms 145656 KB Output is correct
53 Correct 84 ms 145660 KB Output is correct
54 Correct 84 ms 145528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 145144 KB Output is correct
2 Correct 81 ms 145144 KB Output is correct
3 Correct 78 ms 145272 KB Output is correct
4 Correct 79 ms 145144 KB Output is correct
5 Correct 82 ms 145144 KB Output is correct
6 Correct 79 ms 145144 KB Output is correct
7 Correct 80 ms 145144 KB Output is correct
8 Correct 80 ms 145144 KB Output is correct
9 Correct 79 ms 145144 KB Output is correct
10 Correct 84 ms 145144 KB Output is correct
11 Correct 79 ms 145272 KB Output is correct
12 Correct 79 ms 145144 KB Output is correct
13 Correct 79 ms 145276 KB Output is correct
14 Correct 82 ms 145272 KB Output is correct
15 Correct 84 ms 145276 KB Output is correct
16 Correct 82 ms 145144 KB Output is correct
17 Correct 79 ms 145144 KB Output is correct
18 Correct 80 ms 145156 KB Output is correct
19 Correct 80 ms 145272 KB Output is correct
20 Correct 79 ms 145144 KB Output is correct
21 Correct 79 ms 145272 KB Output is correct
22 Correct 81 ms 145272 KB Output is correct
23 Correct 82 ms 145272 KB Output is correct
24 Correct 90 ms 145124 KB Output is correct
25 Correct 85 ms 145144 KB Output is correct
26 Correct 252 ms 156932 KB Output is correct
27 Correct 271 ms 156820 KB Output is correct
28 Correct 83 ms 145528 KB Output is correct
29 Correct 80 ms 145252 KB Output is correct
30 Correct 80 ms 145268 KB Output is correct
31 Correct 262 ms 156920 KB Output is correct
32 Correct 82 ms 145656 KB Output is correct
33 Correct 352 ms 167924 KB Output is correct
34 Correct 256 ms 156776 KB Output is correct
35 Correct 84 ms 145656 KB Output is correct
36 Correct 270 ms 157700 KB Output is correct
37 Correct 82 ms 145656 KB Output is correct
38 Correct 83 ms 145656 KB Output is correct
39 Correct 246 ms 157284 KB Output is correct
40 Correct 83 ms 145912 KB Output is correct
41 Correct 232 ms 155728 KB Output is correct
42 Correct 314 ms 159976 KB Output is correct
43 Correct 82 ms 145272 KB Output is correct
44 Correct 264 ms 167272 KB Output is correct
45 Correct 346 ms 163320 KB Output is correct
46 Correct 83 ms 145656 KB Output is correct
47 Correct 86 ms 145528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 254 ms 157184 KB Output is correct
2 Correct 193 ms 158420 KB Output is correct
3 Correct 82 ms 145656 KB Output is correct
4 Correct 83 ms 145528 KB Output is correct
5 Correct 78 ms 145184 KB Output is correct
6 Correct 82 ms 145144 KB Output is correct
7 Correct 84 ms 145736 KB Output is correct
8 Correct 269 ms 157172 KB Output is correct
9 Correct 86 ms 145528 KB Output is correct
10 Correct 271 ms 157432 KB Output is correct
11 Correct 82 ms 145144 KB Output is correct
12 Correct 264 ms 156792 KB Output is correct
13 Correct 300 ms 157304 KB Output is correct
14 Correct 200 ms 157944 KB Output is correct
15 Correct 238 ms 157404 KB Output is correct
16 Correct 81 ms 145656 KB Output is correct
17 Correct 80 ms 145216 KB Output is correct
18 Correct 187 ms 157928 KB Output is correct
19 Correct 230 ms 163832 KB Output is correct
20 Correct 83 ms 145656 KB Output is correct
21 Correct 78 ms 145144 KB Output is correct
22 Correct 228 ms 157164 KB Output is correct
23 Correct 82 ms 145532 KB Output is correct
24 Correct 281 ms 157304 KB Output is correct
25 Correct 223 ms 162424 KB Output is correct
26 Correct 80 ms 145656 KB Output is correct
27 Correct 80 ms 145656 KB Output is correct
28 Correct 90 ms 145656 KB Output is correct
29 Correct 84 ms 145656 KB Output is correct
30 Correct 82 ms 145656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 145144 KB Output is correct
2 Correct 81 ms 145144 KB Output is correct
3 Correct 78 ms 145272 KB Output is correct
4 Correct 79 ms 145144 KB Output is correct
5 Correct 82 ms 145144 KB Output is correct
6 Correct 79 ms 145144 KB Output is correct
7 Correct 80 ms 145144 KB Output is correct
8 Correct 80 ms 145144 KB Output is correct
9 Correct 79 ms 145144 KB Output is correct
10 Correct 84 ms 145144 KB Output is correct
11 Correct 79 ms 145272 KB Output is correct
12 Correct 79 ms 145144 KB Output is correct
13 Correct 79 ms 145276 KB Output is correct
14 Correct 82 ms 145272 KB Output is correct
15 Correct 84 ms 145276 KB Output is correct
16 Correct 82 ms 145144 KB Output is correct
17 Correct 79 ms 145144 KB Output is correct
18 Correct 80 ms 145156 KB Output is correct
19 Correct 80 ms 145272 KB Output is correct
20 Correct 79 ms 145144 KB Output is correct
21 Correct 79 ms 145272 KB Output is correct
22 Correct 81 ms 145272 KB Output is correct
23 Correct 82 ms 145272 KB Output is correct
24 Correct 90 ms 145124 KB Output is correct
25 Correct 78 ms 145272 KB Output is correct
26 Correct 86 ms 145528 KB Output is correct
27 Correct 85 ms 145528 KB Output is correct
28 Correct 83 ms 145784 KB Output is correct
29 Correct 85 ms 145528 KB Output is correct
30 Correct 84 ms 145648 KB Output is correct
31 Correct 81 ms 145144 KB Output is correct
32 Correct 81 ms 145784 KB Output is correct
33 Correct 79 ms 145148 KB Output is correct
34 Correct 82 ms 145656 KB Output is correct
35 Correct 81 ms 145528 KB Output is correct
36 Correct 84 ms 145528 KB Output is correct
37 Correct 83 ms 145528 KB Output is correct
38 Correct 83 ms 145144 KB Output is correct
39 Correct 82 ms 145528 KB Output is correct
40 Correct 86 ms 145528 KB Output is correct
41 Correct 87 ms 145656 KB Output is correct
42 Correct 83 ms 145528 KB Output is correct
43 Correct 81 ms 145912 KB Output is correct
44 Correct 82 ms 145144 KB Output is correct
45 Correct 84 ms 145656 KB Output is correct
46 Correct 84 ms 145528 KB Output is correct
47 Correct 79 ms 145144 KB Output is correct
48 Correct 81 ms 145528 KB Output is correct
49 Correct 83 ms 145528 KB Output is correct
50 Correct 86 ms 145656 KB Output is correct
51 Correct 86 ms 145528 KB Output is correct
52 Correct 82 ms 145656 KB Output is correct
53 Correct 84 ms 145660 KB Output is correct
54 Correct 84 ms 145528 KB Output is correct
55 Correct 85 ms 145144 KB Output is correct
56 Correct 252 ms 156932 KB Output is correct
57 Correct 271 ms 156820 KB Output is correct
58 Correct 83 ms 145528 KB Output is correct
59 Correct 80 ms 145252 KB Output is correct
60 Correct 80 ms 145268 KB Output is correct
61 Correct 262 ms 156920 KB Output is correct
62 Correct 82 ms 145656 KB Output is correct
63 Correct 352 ms 167924 KB Output is correct
64 Correct 256 ms 156776 KB Output is correct
65 Correct 84 ms 145656 KB Output is correct
66 Correct 270 ms 157700 KB Output is correct
67 Correct 82 ms 145656 KB Output is correct
68 Correct 83 ms 145656 KB Output is correct
69 Correct 246 ms 157284 KB Output is correct
70 Correct 83 ms 145912 KB Output is correct
71 Correct 232 ms 155728 KB Output is correct
72 Correct 314 ms 159976 KB Output is correct
73 Correct 82 ms 145272 KB Output is correct
74 Correct 264 ms 167272 KB Output is correct
75 Correct 346 ms 163320 KB Output is correct
76 Correct 83 ms 145656 KB Output is correct
77 Correct 86 ms 145528 KB Output is correct
78 Correct 254 ms 157184 KB Output is correct
79 Correct 193 ms 158420 KB Output is correct
80 Correct 82 ms 145656 KB Output is correct
81 Correct 83 ms 145528 KB Output is correct
82 Correct 78 ms 145184 KB Output is correct
83 Correct 82 ms 145144 KB Output is correct
84 Correct 84 ms 145736 KB Output is correct
85 Correct 269 ms 157172 KB Output is correct
86 Correct 86 ms 145528 KB Output is correct
87 Correct 271 ms 157432 KB Output is correct
88 Correct 82 ms 145144 KB Output is correct
89 Correct 264 ms 156792 KB Output is correct
90 Correct 300 ms 157304 KB Output is correct
91 Correct 200 ms 157944 KB Output is correct
92 Correct 238 ms 157404 KB Output is correct
93 Correct 81 ms 145656 KB Output is correct
94 Correct 80 ms 145216 KB Output is correct
95 Correct 187 ms 157928 KB Output is correct
96 Correct 230 ms 163832 KB Output is correct
97 Correct 83 ms 145656 KB Output is correct
98 Correct 78 ms 145144 KB Output is correct
99 Correct 228 ms 157164 KB Output is correct
100 Correct 82 ms 145532 KB Output is correct
101 Correct 281 ms 157304 KB Output is correct
102 Correct 223 ms 162424 KB Output is correct
103 Correct 80 ms 145656 KB Output is correct
104 Correct 80 ms 145656 KB Output is correct
105 Correct 90 ms 145656 KB Output is correct
106 Correct 84 ms 145656 KB Output is correct
107 Correct 82 ms 145656 KB Output is correct
108 Correct 1252 ms 202388 KB Output is correct
109 Correct 1337 ms 233104 KB Output is correct
110 Correct 1365 ms 247636 KB Output is correct
111 Correct 1274 ms 251348 KB Output is correct
112 Correct 1193 ms 238196 KB Output is correct
113 Correct 855 ms 217412 KB Output is correct
114 Correct 1234 ms 208272 KB Output is correct
115 Correct 1221 ms 203040 KB Output is correct
116 Correct 1527 ms 212732 KB Output is correct
117 Correct 1004 ms 216820 KB Output is correct
118 Correct 1332 ms 211448 KB Output is correct
119 Correct 963 ms 216952 KB Output is correct
120 Correct 1103 ms 241144 KB Output is correct
121 Correct 973 ms 216872 KB Output is correct
122 Correct 1523 ms 212452 KB Output is correct
123 Correct 705 ms 217852 KB Output is correct
124 Correct 1146 ms 211832 KB Output is correct