Submission #236453

# Submission time Handle Problem Language Result Execution time Memory
236453 2020-06-02T10:09:26 Z Charis02 Mergers (JOI19_mergers) C++14
70 / 100
1249 ms 196188 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[4*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 74 ms 129620 KB Output is correct
2 Correct 74 ms 129528 KB Output is correct
3 Correct 72 ms 129528 KB Output is correct
4 Correct 71 ms 129504 KB Output is correct
5 Correct 72 ms 129528 KB Output is correct
6 Correct 78 ms 129528 KB Output is correct
7 Correct 111 ms 129564 KB Output is correct
8 Correct 81 ms 129560 KB Output is correct
9 Correct 73 ms 129528 KB Output is correct
10 Correct 69 ms 129528 KB Output is correct
11 Correct 68 ms 129528 KB Output is correct
12 Correct 69 ms 129528 KB Output is correct
13 Correct 72 ms 129504 KB Output is correct
14 Correct 75 ms 129528 KB Output is correct
15 Correct 70 ms 129528 KB Output is correct
16 Correct 70 ms 129528 KB Output is correct
17 Correct 69 ms 129528 KB Output is correct
18 Correct 74 ms 129528 KB Output is correct
19 Correct 69 ms 129528 KB Output is correct
20 Correct 72 ms 129528 KB Output is correct
21 Correct 73 ms 129528 KB Output is correct
22 Correct 71 ms 129532 KB Output is correct
23 Correct 70 ms 129528 KB Output is correct
24 Correct 71 ms 129656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 129620 KB Output is correct
2 Correct 74 ms 129528 KB Output is correct
3 Correct 72 ms 129528 KB Output is correct
4 Correct 71 ms 129504 KB Output is correct
5 Correct 72 ms 129528 KB Output is correct
6 Correct 78 ms 129528 KB Output is correct
7 Correct 111 ms 129564 KB Output is correct
8 Correct 81 ms 129560 KB Output is correct
9 Correct 73 ms 129528 KB Output is correct
10 Correct 69 ms 129528 KB Output is correct
11 Correct 68 ms 129528 KB Output is correct
12 Correct 69 ms 129528 KB Output is correct
13 Correct 72 ms 129504 KB Output is correct
14 Correct 75 ms 129528 KB Output is correct
15 Correct 70 ms 129528 KB Output is correct
16 Correct 70 ms 129528 KB Output is correct
17 Correct 69 ms 129528 KB Output is correct
18 Correct 74 ms 129528 KB Output is correct
19 Correct 69 ms 129528 KB Output is correct
20 Correct 72 ms 129528 KB Output is correct
21 Correct 73 ms 129528 KB Output is correct
22 Correct 71 ms 129532 KB Output is correct
23 Correct 70 ms 129528 KB Output is correct
24 Correct 71 ms 129656 KB Output is correct
25 Correct 67 ms 129528 KB Output is correct
26 Correct 75 ms 129912 KB Output is correct
27 Correct 79 ms 129912 KB Output is correct
28 Correct 71 ms 130168 KB Output is correct
29 Correct 79 ms 130088 KB Output is correct
30 Correct 87 ms 130040 KB Output is correct
31 Correct 69 ms 129528 KB Output is correct
32 Correct 73 ms 130168 KB Output is correct
33 Correct 69 ms 129528 KB Output is correct
34 Correct 72 ms 130044 KB Output is correct
35 Correct 72 ms 130040 KB Output is correct
36 Correct 79 ms 129916 KB Output is correct
37 Correct 74 ms 129912 KB Output is correct
38 Correct 70 ms 129656 KB Output is correct
39 Correct 80 ms 130040 KB Output is correct
40 Correct 73 ms 129912 KB Output is correct
41 Correct 73 ms 129912 KB Output is correct
42 Correct 72 ms 129912 KB Output is correct
43 Correct 71 ms 130296 KB Output is correct
44 Correct 72 ms 129528 KB Output is correct
45 Correct 72 ms 130040 KB Output is correct
46 Correct 73 ms 129912 KB Output is correct
47 Correct 70 ms 129656 KB Output is correct
48 Correct 76 ms 129916 KB Output is correct
49 Correct 71 ms 130040 KB Output is correct
50 Correct 70 ms 130040 KB Output is correct
51 Correct 82 ms 129912 KB Output is correct
52 Correct 73 ms 130040 KB Output is correct
53 Correct 77 ms 130040 KB Output is correct
54 Correct 82 ms 129928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 129620 KB Output is correct
2 Correct 74 ms 129528 KB Output is correct
3 Correct 72 ms 129528 KB Output is correct
4 Correct 71 ms 129504 KB Output is correct
5 Correct 72 ms 129528 KB Output is correct
6 Correct 78 ms 129528 KB Output is correct
7 Correct 111 ms 129564 KB Output is correct
8 Correct 81 ms 129560 KB Output is correct
9 Correct 73 ms 129528 KB Output is correct
10 Correct 69 ms 129528 KB Output is correct
11 Correct 68 ms 129528 KB Output is correct
12 Correct 69 ms 129528 KB Output is correct
13 Correct 72 ms 129504 KB Output is correct
14 Correct 75 ms 129528 KB Output is correct
15 Correct 70 ms 129528 KB Output is correct
16 Correct 70 ms 129528 KB Output is correct
17 Correct 69 ms 129528 KB Output is correct
18 Correct 74 ms 129528 KB Output is correct
19 Correct 69 ms 129528 KB Output is correct
20 Correct 72 ms 129528 KB Output is correct
21 Correct 73 ms 129528 KB Output is correct
22 Correct 71 ms 129532 KB Output is correct
23 Correct 70 ms 129528 KB Output is correct
24 Correct 71 ms 129656 KB Output is correct
25 Correct 71 ms 129528 KB Output is correct
26 Correct 258 ms 142308 KB Output is correct
27 Correct 273 ms 142212 KB Output is correct
28 Correct 78 ms 129912 KB Output is correct
29 Correct 77 ms 129528 KB Output is correct
30 Correct 69 ms 129528 KB Output is correct
31 Correct 271 ms 142304 KB Output is correct
32 Correct 85 ms 130040 KB Output is correct
33 Correct 369 ms 153332 KB Output is correct
34 Correct 288 ms 142180 KB Output is correct
35 Correct 99 ms 129912 KB Output is correct
36 Correct 274 ms 143056 KB Output is correct
37 Correct 73 ms 129916 KB Output is correct
38 Correct 79 ms 130008 KB Output is correct
39 Correct 228 ms 142688 KB Output is correct
40 Correct 76 ms 130272 KB Output is correct
41 Correct 233 ms 141168 KB Output is correct
42 Correct 334 ms 145384 KB Output is correct
43 Correct 72 ms 129528 KB Output is correct
44 Correct 252 ms 152556 KB Output is correct
45 Correct 342 ms 148728 KB Output is correct
46 Correct 74 ms 130040 KB Output is correct
47 Correct 76 ms 129916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 141828 KB Output is correct
2 Correct 156 ms 143212 KB Output is correct
3 Correct 74 ms 129912 KB Output is correct
4 Correct 76 ms 129912 KB Output is correct
5 Correct 77 ms 129528 KB Output is correct
6 Correct 73 ms 129528 KB Output is correct
7 Correct 76 ms 130040 KB Output is correct
8 Correct 266 ms 142844 KB Output is correct
9 Correct 79 ms 130168 KB Output is correct
10 Correct 257 ms 142840 KB Output is correct
11 Correct 74 ms 129528 KB Output is correct
12 Correct 255 ms 142200 KB Output is correct
13 Correct 280 ms 142968 KB Output is correct
14 Correct 190 ms 143736 KB Output is correct
15 Correct 235 ms 142564 KB Output is correct
16 Correct 74 ms 130044 KB Output is correct
17 Correct 78 ms 129528 KB Output is correct
18 Correct 181 ms 143588 KB Output is correct
19 Correct 225 ms 149624 KB Output is correct
20 Correct 74 ms 129912 KB Output is correct
21 Correct 74 ms 129528 KB Output is correct
22 Correct 229 ms 142696 KB Output is correct
23 Correct 75 ms 129912 KB Output is correct
24 Correct 270 ms 142840 KB Output is correct
25 Correct 233 ms 148088 KB Output is correct
26 Correct 76 ms 129912 KB Output is correct
27 Correct 74 ms 130040 KB Output is correct
28 Correct 75 ms 129912 KB Output is correct
29 Correct 75 ms 130040 KB Output is correct
30 Correct 74 ms 129944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 129620 KB Output is correct
2 Correct 74 ms 129528 KB Output is correct
3 Correct 72 ms 129528 KB Output is correct
4 Correct 71 ms 129504 KB Output is correct
5 Correct 72 ms 129528 KB Output is correct
6 Correct 78 ms 129528 KB Output is correct
7 Correct 111 ms 129564 KB Output is correct
8 Correct 81 ms 129560 KB Output is correct
9 Correct 73 ms 129528 KB Output is correct
10 Correct 69 ms 129528 KB Output is correct
11 Correct 68 ms 129528 KB Output is correct
12 Correct 69 ms 129528 KB Output is correct
13 Correct 72 ms 129504 KB Output is correct
14 Correct 75 ms 129528 KB Output is correct
15 Correct 70 ms 129528 KB Output is correct
16 Correct 70 ms 129528 KB Output is correct
17 Correct 69 ms 129528 KB Output is correct
18 Correct 74 ms 129528 KB Output is correct
19 Correct 69 ms 129528 KB Output is correct
20 Correct 72 ms 129528 KB Output is correct
21 Correct 73 ms 129528 KB Output is correct
22 Correct 71 ms 129532 KB Output is correct
23 Correct 70 ms 129528 KB Output is correct
24 Correct 71 ms 129656 KB Output is correct
25 Correct 67 ms 129528 KB Output is correct
26 Correct 75 ms 129912 KB Output is correct
27 Correct 79 ms 129912 KB Output is correct
28 Correct 71 ms 130168 KB Output is correct
29 Correct 79 ms 130088 KB Output is correct
30 Correct 87 ms 130040 KB Output is correct
31 Correct 69 ms 129528 KB Output is correct
32 Correct 73 ms 130168 KB Output is correct
33 Correct 69 ms 129528 KB Output is correct
34 Correct 72 ms 130044 KB Output is correct
35 Correct 72 ms 130040 KB Output is correct
36 Correct 79 ms 129916 KB Output is correct
37 Correct 74 ms 129912 KB Output is correct
38 Correct 70 ms 129656 KB Output is correct
39 Correct 80 ms 130040 KB Output is correct
40 Correct 73 ms 129912 KB Output is correct
41 Correct 73 ms 129912 KB Output is correct
42 Correct 72 ms 129912 KB Output is correct
43 Correct 71 ms 130296 KB Output is correct
44 Correct 72 ms 129528 KB Output is correct
45 Correct 72 ms 130040 KB Output is correct
46 Correct 73 ms 129912 KB Output is correct
47 Correct 70 ms 129656 KB Output is correct
48 Correct 76 ms 129916 KB Output is correct
49 Correct 71 ms 130040 KB Output is correct
50 Correct 70 ms 130040 KB Output is correct
51 Correct 82 ms 129912 KB Output is correct
52 Correct 73 ms 130040 KB Output is correct
53 Correct 77 ms 130040 KB Output is correct
54 Correct 82 ms 129928 KB Output is correct
55 Correct 71 ms 129528 KB Output is correct
56 Correct 258 ms 142308 KB Output is correct
57 Correct 273 ms 142212 KB Output is correct
58 Correct 78 ms 129912 KB Output is correct
59 Correct 77 ms 129528 KB Output is correct
60 Correct 69 ms 129528 KB Output is correct
61 Correct 271 ms 142304 KB Output is correct
62 Correct 85 ms 130040 KB Output is correct
63 Correct 369 ms 153332 KB Output is correct
64 Correct 288 ms 142180 KB Output is correct
65 Correct 99 ms 129912 KB Output is correct
66 Correct 274 ms 143056 KB Output is correct
67 Correct 73 ms 129916 KB Output is correct
68 Correct 79 ms 130008 KB Output is correct
69 Correct 228 ms 142688 KB Output is correct
70 Correct 76 ms 130272 KB Output is correct
71 Correct 233 ms 141168 KB Output is correct
72 Correct 334 ms 145384 KB Output is correct
73 Correct 72 ms 129528 KB Output is correct
74 Correct 252 ms 152556 KB Output is correct
75 Correct 342 ms 148728 KB Output is correct
76 Correct 74 ms 130040 KB Output is correct
77 Correct 76 ms 129916 KB Output is correct
78 Correct 224 ms 141828 KB Output is correct
79 Correct 156 ms 143212 KB Output is correct
80 Correct 74 ms 129912 KB Output is correct
81 Correct 76 ms 129912 KB Output is correct
82 Correct 77 ms 129528 KB Output is correct
83 Correct 73 ms 129528 KB Output is correct
84 Correct 76 ms 130040 KB Output is correct
85 Correct 266 ms 142844 KB Output is correct
86 Correct 79 ms 130168 KB Output is correct
87 Correct 257 ms 142840 KB Output is correct
88 Correct 74 ms 129528 KB Output is correct
89 Correct 255 ms 142200 KB Output is correct
90 Correct 280 ms 142968 KB Output is correct
91 Correct 190 ms 143736 KB Output is correct
92 Correct 235 ms 142564 KB Output is correct
93 Correct 74 ms 130044 KB Output is correct
94 Correct 78 ms 129528 KB Output is correct
95 Correct 181 ms 143588 KB Output is correct
96 Correct 225 ms 149624 KB Output is correct
97 Correct 74 ms 129912 KB Output is correct
98 Correct 74 ms 129528 KB Output is correct
99 Correct 229 ms 142696 KB Output is correct
100 Correct 75 ms 129912 KB Output is correct
101 Correct 270 ms 142840 KB Output is correct
102 Correct 233 ms 148088 KB Output is correct
103 Correct 76 ms 129912 KB Output is correct
104 Correct 74 ms 130040 KB Output is correct
105 Correct 75 ms 129912 KB Output is correct
106 Correct 75 ms 130040 KB Output is correct
107 Correct 74 ms 129944 KB Output is correct
108 Incorrect 1249 ms 196188 KB Output isn't correct
109 Halted 0 ms 0 KB -