Submission #236457

# Submission time Handle Problem Language Result Execution time Memory
236457 2020-06-02T10:12:26 Z Charis02 Mergers (JOI19_mergers) C++14
100 / 100
1440 ms 168824 KB
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll int
#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(int, 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(int, 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 52 ms 90360 KB Output is correct
2 Correct 50 ms 90360 KB Output is correct
3 Correct 53 ms 90360 KB Output is correct
4 Correct 61 ms 90360 KB Output is correct
5 Correct 58 ms 90360 KB Output is correct
6 Correct 58 ms 90488 KB Output is correct
7 Correct 60 ms 90360 KB Output is correct
8 Correct 56 ms 90360 KB Output is correct
9 Correct 51 ms 90360 KB Output is correct
10 Correct 52 ms 90360 KB Output is correct
11 Correct 52 ms 90360 KB Output is correct
12 Correct 51 ms 90368 KB Output is correct
13 Correct 52 ms 90488 KB Output is correct
14 Correct 51 ms 90360 KB Output is correct
15 Correct 51 ms 90360 KB Output is correct
16 Correct 51 ms 90364 KB Output is correct
17 Correct 53 ms 90360 KB Output is correct
18 Correct 52 ms 90488 KB Output is correct
19 Correct 50 ms 90360 KB Output is correct
20 Correct 51 ms 90360 KB Output is correct
21 Correct 54 ms 90360 KB Output is correct
22 Correct 53 ms 90360 KB Output is correct
23 Correct 52 ms 90364 KB Output is correct
24 Correct 52 ms 90488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 90360 KB Output is correct
2 Correct 50 ms 90360 KB Output is correct
3 Correct 53 ms 90360 KB Output is correct
4 Correct 61 ms 90360 KB Output is correct
5 Correct 58 ms 90360 KB Output is correct
6 Correct 58 ms 90488 KB Output is correct
7 Correct 60 ms 90360 KB Output is correct
8 Correct 56 ms 90360 KB Output is correct
9 Correct 51 ms 90360 KB Output is correct
10 Correct 52 ms 90360 KB Output is correct
11 Correct 52 ms 90360 KB Output is correct
12 Correct 51 ms 90368 KB Output is correct
13 Correct 52 ms 90488 KB Output is correct
14 Correct 51 ms 90360 KB Output is correct
15 Correct 51 ms 90360 KB Output is correct
16 Correct 51 ms 90364 KB Output is correct
17 Correct 53 ms 90360 KB Output is correct
18 Correct 52 ms 90488 KB Output is correct
19 Correct 50 ms 90360 KB Output is correct
20 Correct 51 ms 90360 KB Output is correct
21 Correct 54 ms 90360 KB Output is correct
22 Correct 53 ms 90360 KB Output is correct
23 Correct 52 ms 90364 KB Output is correct
24 Correct 52 ms 90488 KB Output is correct
25 Correct 50 ms 90360 KB Output is correct
26 Correct 53 ms 90744 KB Output is correct
27 Correct 56 ms 90744 KB Output is correct
28 Correct 53 ms 90872 KB Output is correct
29 Correct 55 ms 90744 KB Output is correct
30 Correct 56 ms 90872 KB Output is correct
31 Correct 53 ms 90360 KB Output is correct
32 Correct 55 ms 91000 KB Output is correct
33 Correct 52 ms 90360 KB Output is correct
34 Correct 54 ms 90744 KB Output is correct
35 Correct 53 ms 90744 KB Output is correct
36 Correct 56 ms 90616 KB Output is correct
37 Correct 55 ms 90616 KB Output is correct
38 Correct 51 ms 90360 KB Output is correct
39 Correct 55 ms 90616 KB Output is correct
40 Correct 55 ms 90616 KB Output is correct
41 Correct 54 ms 90616 KB Output is correct
42 Correct 56 ms 90744 KB Output is correct
43 Correct 59 ms 91000 KB Output is correct
44 Correct 50 ms 90360 KB Output is correct
45 Correct 54 ms 90756 KB Output is correct
46 Correct 56 ms 90616 KB Output is correct
47 Correct 51 ms 90360 KB Output is correct
48 Correct 54 ms 90744 KB Output is correct
49 Correct 54 ms 90744 KB Output is correct
50 Correct 53 ms 90744 KB Output is correct
51 Correct 53 ms 90616 KB Output is correct
52 Correct 55 ms 90744 KB Output is correct
53 Correct 53 ms 90744 KB Output is correct
54 Correct 56 ms 90616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 90360 KB Output is correct
2 Correct 50 ms 90360 KB Output is correct
3 Correct 53 ms 90360 KB Output is correct
4 Correct 61 ms 90360 KB Output is correct
5 Correct 58 ms 90360 KB Output is correct
6 Correct 58 ms 90488 KB Output is correct
7 Correct 60 ms 90360 KB Output is correct
8 Correct 56 ms 90360 KB Output is correct
9 Correct 51 ms 90360 KB Output is correct
10 Correct 52 ms 90360 KB Output is correct
11 Correct 52 ms 90360 KB Output is correct
12 Correct 51 ms 90368 KB Output is correct
13 Correct 52 ms 90488 KB Output is correct
14 Correct 51 ms 90360 KB Output is correct
15 Correct 51 ms 90360 KB Output is correct
16 Correct 51 ms 90364 KB Output is correct
17 Correct 53 ms 90360 KB Output is correct
18 Correct 52 ms 90488 KB Output is correct
19 Correct 50 ms 90360 KB Output is correct
20 Correct 51 ms 90360 KB Output is correct
21 Correct 54 ms 90360 KB Output is correct
22 Correct 53 ms 90360 KB Output is correct
23 Correct 52 ms 90364 KB Output is correct
24 Correct 52 ms 90488 KB Output is correct
25 Correct 50 ms 90360 KB Output is correct
26 Correct 194 ms 98428 KB Output is correct
27 Correct 230 ms 98160 KB Output is correct
28 Correct 59 ms 90744 KB Output is correct
29 Correct 52 ms 90360 KB Output is correct
30 Correct 53 ms 90360 KB Output is correct
31 Correct 218 ms 98168 KB Output is correct
32 Correct 54 ms 90744 KB Output is correct
33 Correct 293 ms 108920 KB Output is correct
34 Correct 231 ms 98164 KB Output is correct
35 Correct 57 ms 90616 KB Output is correct
36 Correct 242 ms 99808 KB Output is correct
37 Correct 55 ms 90616 KB Output is correct
38 Correct 54 ms 90616 KB Output is correct
39 Correct 190 ms 98540 KB Output is correct
40 Correct 53 ms 91000 KB Output is correct
41 Correct 220 ms 97644 KB Output is correct
42 Correct 273 ms 101748 KB Output is correct
43 Correct 51 ms 90360 KB Output is correct
44 Correct 241 ms 108140 KB Output is correct
45 Correct 275 ms 103800 KB Output is correct
46 Correct 53 ms 90752 KB Output is correct
47 Correct 53 ms 90744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 98932 KB Output is correct
2 Correct 134 ms 100972 KB Output is correct
3 Correct 56 ms 90744 KB Output is correct
4 Correct 53 ms 90744 KB Output is correct
5 Correct 51 ms 90364 KB Output is correct
6 Correct 52 ms 90488 KB Output is correct
7 Correct 54 ms 90744 KB Output is correct
8 Correct 228 ms 99052 KB Output is correct
9 Correct 55 ms 90752 KB Output is correct
10 Correct 225 ms 98424 KB Output is correct
11 Correct 50 ms 90488 KB Output is correct
12 Correct 220 ms 98832 KB Output is correct
13 Correct 220 ms 99192 KB Output is correct
14 Correct 170 ms 100216 KB Output is correct
15 Correct 190 ms 98540 KB Output is correct
16 Correct 52 ms 90744 KB Output is correct
17 Correct 50 ms 90360 KB Output is correct
18 Correct 155 ms 100340 KB Output is correct
19 Correct 219 ms 105724 KB Output is correct
20 Correct 53 ms 90616 KB Output is correct
21 Correct 51 ms 90360 KB Output is correct
22 Correct 200 ms 99184 KB Output is correct
23 Correct 53 ms 90624 KB Output is correct
24 Correct 229 ms 98940 KB Output is correct
25 Correct 189 ms 103928 KB Output is correct
26 Correct 56 ms 90872 KB Output is correct
27 Correct 52 ms 90744 KB Output is correct
28 Correct 54 ms 90616 KB Output is correct
29 Correct 53 ms 90744 KB Output is correct
30 Correct 53 ms 90744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 90360 KB Output is correct
2 Correct 50 ms 90360 KB Output is correct
3 Correct 53 ms 90360 KB Output is correct
4 Correct 61 ms 90360 KB Output is correct
5 Correct 58 ms 90360 KB Output is correct
6 Correct 58 ms 90488 KB Output is correct
7 Correct 60 ms 90360 KB Output is correct
8 Correct 56 ms 90360 KB Output is correct
9 Correct 51 ms 90360 KB Output is correct
10 Correct 52 ms 90360 KB Output is correct
11 Correct 52 ms 90360 KB Output is correct
12 Correct 51 ms 90368 KB Output is correct
13 Correct 52 ms 90488 KB Output is correct
14 Correct 51 ms 90360 KB Output is correct
15 Correct 51 ms 90360 KB Output is correct
16 Correct 51 ms 90364 KB Output is correct
17 Correct 53 ms 90360 KB Output is correct
18 Correct 52 ms 90488 KB Output is correct
19 Correct 50 ms 90360 KB Output is correct
20 Correct 51 ms 90360 KB Output is correct
21 Correct 54 ms 90360 KB Output is correct
22 Correct 53 ms 90360 KB Output is correct
23 Correct 52 ms 90364 KB Output is correct
24 Correct 52 ms 90488 KB Output is correct
25 Correct 50 ms 90360 KB Output is correct
26 Correct 53 ms 90744 KB Output is correct
27 Correct 56 ms 90744 KB Output is correct
28 Correct 53 ms 90872 KB Output is correct
29 Correct 55 ms 90744 KB Output is correct
30 Correct 56 ms 90872 KB Output is correct
31 Correct 53 ms 90360 KB Output is correct
32 Correct 55 ms 91000 KB Output is correct
33 Correct 52 ms 90360 KB Output is correct
34 Correct 54 ms 90744 KB Output is correct
35 Correct 53 ms 90744 KB Output is correct
36 Correct 56 ms 90616 KB Output is correct
37 Correct 55 ms 90616 KB Output is correct
38 Correct 51 ms 90360 KB Output is correct
39 Correct 55 ms 90616 KB Output is correct
40 Correct 55 ms 90616 KB Output is correct
41 Correct 54 ms 90616 KB Output is correct
42 Correct 56 ms 90744 KB Output is correct
43 Correct 59 ms 91000 KB Output is correct
44 Correct 50 ms 90360 KB Output is correct
45 Correct 54 ms 90756 KB Output is correct
46 Correct 56 ms 90616 KB Output is correct
47 Correct 51 ms 90360 KB Output is correct
48 Correct 54 ms 90744 KB Output is correct
49 Correct 54 ms 90744 KB Output is correct
50 Correct 53 ms 90744 KB Output is correct
51 Correct 53 ms 90616 KB Output is correct
52 Correct 55 ms 90744 KB Output is correct
53 Correct 53 ms 90744 KB Output is correct
54 Correct 56 ms 90616 KB Output is correct
55 Correct 50 ms 90360 KB Output is correct
56 Correct 194 ms 98428 KB Output is correct
57 Correct 230 ms 98160 KB Output is correct
58 Correct 59 ms 90744 KB Output is correct
59 Correct 52 ms 90360 KB Output is correct
60 Correct 53 ms 90360 KB Output is correct
61 Correct 218 ms 98168 KB Output is correct
62 Correct 54 ms 90744 KB Output is correct
63 Correct 293 ms 108920 KB Output is correct
64 Correct 231 ms 98164 KB Output is correct
65 Correct 57 ms 90616 KB Output is correct
66 Correct 242 ms 99808 KB Output is correct
67 Correct 55 ms 90616 KB Output is correct
68 Correct 54 ms 90616 KB Output is correct
69 Correct 190 ms 98540 KB Output is correct
70 Correct 53 ms 91000 KB Output is correct
71 Correct 220 ms 97644 KB Output is correct
72 Correct 273 ms 101748 KB Output is correct
73 Correct 51 ms 90360 KB Output is correct
74 Correct 241 ms 108140 KB Output is correct
75 Correct 275 ms 103800 KB Output is correct
76 Correct 53 ms 90752 KB Output is correct
77 Correct 53 ms 90744 KB Output is correct
78 Correct 212 ms 98932 KB Output is correct
79 Correct 134 ms 100972 KB Output is correct
80 Correct 56 ms 90744 KB Output is correct
81 Correct 53 ms 90744 KB Output is correct
82 Correct 51 ms 90364 KB Output is correct
83 Correct 52 ms 90488 KB Output is correct
84 Correct 54 ms 90744 KB Output is correct
85 Correct 228 ms 99052 KB Output is correct
86 Correct 55 ms 90752 KB Output is correct
87 Correct 225 ms 98424 KB Output is correct
88 Correct 50 ms 90488 KB Output is correct
89 Correct 220 ms 98832 KB Output is correct
90 Correct 220 ms 99192 KB Output is correct
91 Correct 170 ms 100216 KB Output is correct
92 Correct 190 ms 98540 KB Output is correct
93 Correct 52 ms 90744 KB Output is correct
94 Correct 50 ms 90360 KB Output is correct
95 Correct 155 ms 100340 KB Output is correct
96 Correct 219 ms 105724 KB Output is correct
97 Correct 53 ms 90616 KB Output is correct
98 Correct 51 ms 90360 KB Output is correct
99 Correct 200 ms 99184 KB Output is correct
100 Correct 53 ms 90624 KB Output is correct
101 Correct 229 ms 98940 KB Output is correct
102 Correct 189 ms 103928 KB Output is correct
103 Correct 56 ms 90872 KB Output is correct
104 Correct 52 ms 90744 KB Output is correct
105 Correct 54 ms 90616 KB Output is correct
106 Correct 53 ms 90744 KB Output is correct
107 Correct 53 ms 90744 KB Output is correct
108 Correct 1156 ms 129384 KB Output is correct
109 Correct 1307 ms 151800 KB Output is correct
110 Correct 1253 ms 164192 KB Output is correct
111 Correct 1128 ms 168824 KB Output is correct
112 Correct 1143 ms 154360 KB Output is correct
113 Correct 763 ms 136668 KB Output is correct
114 Correct 1108 ms 125584 KB Output is correct
115 Correct 1113 ms 123028 KB Output is correct
116 Correct 1440 ms 127864 KB Output is correct
117 Correct 926 ms 136184 KB Output is correct
118 Correct 1178 ms 126636 KB Output is correct
119 Correct 931 ms 136312 KB Output is correct
120 Correct 1012 ms 155640 KB Output is correct
121 Correct 966 ms 135928 KB Output is correct
122 Correct 1313 ms 130408 KB Output is correct
123 Correct 651 ms 137316 KB Output is correct
124 Correct 1040 ms 134520 KB Output is correct