Submission #227202

# Submission time Handle Problem Language Result Execution time Memory
227202 2020-04-26T13:39:26 Z MKopchev Mergers (JOI19_mergers) C++14
100 / 100
1176 ms 75176 KB
#include <bits/stdc++.h>
using namespace std;

const int nmax=5e5+42;

int n,k;

int colour[nmax],cnt[nmax];

vector<int> adj[nmax];

int wrong=0;

int seen[nmax];

void add(int val)
{
    val=colour[val];

    if(seen[val]==0)wrong++;
    seen[val]++;
    if(seen[val]==cnt[val])wrong--;
}

void sub(int val)
{
    val=colour[val];

    if(seen[val]==cnt[val])wrong++;
    seen[val]--;
    if(seen[val]==0)wrong--;
}

int SZ[nmax];

void dfs_sz(int node,int par)
{
    SZ[node]=1;
    for(auto k:adj[node])
        if(k!=par)
        {
            dfs_sz(k,node);
            SZ[node]+=SZ[k];
        }
}

int can_divide=0;

void dfs_clean(int node,int par)
{
    sub(node);

    for(auto k:adj[node])
        if(k!=par)
        {
            dfs_clean(k,node);
        }
}

void dfs_add(int node,int par)
{
    add(node);

    for(auto k:adj[node])
        if(k!=par)
        {
            dfs_add(k,node);
        }
}

int ROOT;
int dfs(int node,int par)
{
    int found=0;

    int big=0;

    for(auto k:adj[node])
        if(k!=par&&SZ[big]<SZ[k])big=k;

    for(auto k:adj[node])
        if(k!=par&&big!=k)
        {
            found=dfs(k,node)+found;

            dfs_clean(k,node);
        }
    /*
    cout<<node<<" -> "<<big<<" "<<found<<" "<<wrong<<endl;

    cout<<"seen(should be empty): ";for(int i=1;i<=k;i++)cout<<seen[i]<<" ";cout<<endl;
    */
    if(big)found=found+dfs(big,node);


    for(auto k:adj[node])
        if(k!=par&&big!=k)
            dfs_add(k,node);

    add(node);

    //cout<<node<<" -> "<<big<<" "<<found<<" "<<wrong<<endl;

    //cout<<"seen: ";for(int i=1;i<=k;i++)cout<<seen[i]<<" ";cout<<endl;

    if(wrong==0)
    {
        if(node!=ROOT)found++;

        if(found==1)can_divide++;

        return 1;
    }

    return found;
}
int main()
{
    scanf("%i%i",&n,&k);

    int u,v;
    for(int i=1;i<n;i++)
    {
        scanf("%i%i",&u,&v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for(int i=1;i<=n;i++)
    {
        scanf("%i",&colour[i]);
        cnt[colour[i]]++;
    }

    ROOT=1;

    dfs_sz(ROOT,0);

    dfs(ROOT,0);

    printf("%i\n",(can_divide+1)/2);

    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&n,&k);
     ~~~~~^~~~~~~~~~~~~~
mergers.cpp:124:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&u,&v);
         ~~~~~^~~~~~~~~~~~~~
mergers.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i",&colour[i]);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 11 ms 12016 KB Output is correct
6 Correct 13 ms 12160 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12160 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 11 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12032 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 11 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12160 KB Output is correct
18 Correct 11 ms 12032 KB Output is correct
19 Correct 11 ms 12032 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12032 KB Output is correct
23 Correct 10 ms 12032 KB Output is correct
24 Correct 11 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 11 ms 12016 KB Output is correct
6 Correct 13 ms 12160 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12160 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 11 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12032 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 11 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12160 KB Output is correct
18 Correct 11 ms 12032 KB Output is correct
19 Correct 11 ms 12032 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12032 KB Output is correct
23 Correct 10 ms 12032 KB Output is correct
24 Correct 11 ms 12160 KB Output is correct
25 Correct 11 ms 12160 KB Output is correct
26 Correct 13 ms 12288 KB Output is correct
27 Correct 13 ms 12160 KB Output is correct
28 Correct 13 ms 12416 KB Output is correct
29 Correct 13 ms 12160 KB Output is correct
30 Correct 13 ms 12160 KB Output is correct
31 Correct 11 ms 12032 KB Output is correct
32 Correct 13 ms 12416 KB Output is correct
33 Correct 11 ms 12032 KB Output is correct
34 Correct 12 ms 12160 KB Output is correct
35 Correct 13 ms 12288 KB Output is correct
36 Correct 13 ms 12288 KB Output is correct
37 Correct 13 ms 12288 KB Output is correct
38 Correct 14 ms 12032 KB Output is correct
39 Correct 13 ms 12288 KB Output is correct
40 Correct 13 ms 12288 KB Output is correct
41 Correct 13 ms 12212 KB Output is correct
42 Correct 12 ms 12288 KB Output is correct
43 Correct 13 ms 12544 KB Output is correct
44 Correct 11 ms 12160 KB Output is correct
45 Correct 13 ms 12288 KB Output is correct
46 Correct 13 ms 12288 KB Output is correct
47 Correct 11 ms 12032 KB Output is correct
48 Correct 13 ms 12288 KB Output is correct
49 Correct 12 ms 12288 KB Output is correct
50 Correct 13 ms 12288 KB Output is correct
51 Correct 13 ms 12288 KB Output is correct
52 Correct 12 ms 12416 KB Output is correct
53 Correct 12 ms 12288 KB Output is correct
54 Correct 13 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 11 ms 12016 KB Output is correct
6 Correct 13 ms 12160 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12160 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 11 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12032 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 11 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12160 KB Output is correct
18 Correct 11 ms 12032 KB Output is correct
19 Correct 11 ms 12032 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12032 KB Output is correct
23 Correct 10 ms 12032 KB Output is correct
24 Correct 11 ms 12160 KB Output is correct
25 Correct 11 ms 12160 KB Output is correct
26 Correct 65 ms 16372 KB Output is correct
27 Correct 112 ms 16120 KB Output is correct
28 Correct 13 ms 12160 KB Output is correct
29 Correct 11 ms 12032 KB Output is correct
30 Correct 11 ms 12160 KB Output is correct
31 Correct 117 ms 16120 KB Output is correct
32 Correct 12 ms 12160 KB Output is correct
33 Correct 103 ms 24696 KB Output is correct
34 Correct 112 ms 16120 KB Output is correct
35 Correct 13 ms 12288 KB Output is correct
36 Correct 113 ms 18424 KB Output is correct
37 Correct 13 ms 12288 KB Output is correct
38 Correct 13 ms 12288 KB Output is correct
39 Correct 67 ms 17776 KB Output is correct
40 Correct 13 ms 12544 KB Output is correct
41 Correct 119 ms 17528 KB Output is correct
42 Correct 112 ms 20344 KB Output is correct
43 Correct 11 ms 12160 KB Output is correct
44 Correct 104 ms 26580 KB Output is correct
45 Correct 96 ms 22008 KB Output is correct
46 Correct 12 ms 12288 KB Output is correct
47 Correct 13 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 16368 KB Output is correct
2 Correct 75 ms 17396 KB Output is correct
3 Correct 13 ms 12288 KB Output is correct
4 Correct 13 ms 12160 KB Output is correct
5 Correct 14 ms 12140 KB Output is correct
6 Correct 11 ms 12032 KB Output is correct
7 Correct 12 ms 12160 KB Output is correct
8 Correct 122 ms 16548 KB Output is correct
9 Correct 13 ms 12264 KB Output is correct
10 Correct 122 ms 17656 KB Output is correct
11 Correct 11 ms 12160 KB Output is correct
12 Correct 128 ms 17656 KB Output is correct
13 Correct 135 ms 18168 KB Output is correct
14 Correct 127 ms 18680 KB Output is correct
15 Correct 75 ms 17964 KB Output is correct
16 Correct 12 ms 12288 KB Output is correct
17 Correct 11 ms 12032 KB Output is correct
18 Correct 72 ms 18800 KB Output is correct
19 Correct 124 ms 23288 KB Output is correct
20 Correct 13 ms 12288 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 74 ms 18540 KB Output is correct
23 Correct 14 ms 12288 KB Output is correct
24 Correct 137 ms 17784 KB Output is correct
25 Correct 107 ms 21752 KB Output is correct
26 Correct 13 ms 12288 KB Output is correct
27 Correct 13 ms 12288 KB Output is correct
28 Correct 13 ms 12288 KB Output is correct
29 Correct 12 ms 12288 KB Output is correct
30 Correct 13 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12032 KB Output is correct
2 Correct 11 ms 12032 KB Output is correct
3 Correct 11 ms 12160 KB Output is correct
4 Correct 11 ms 12160 KB Output is correct
5 Correct 11 ms 12016 KB Output is correct
6 Correct 13 ms 12160 KB Output is correct
7 Correct 11 ms 12032 KB Output is correct
8 Correct 11 ms 12160 KB Output is correct
9 Correct 11 ms 12032 KB Output is correct
10 Correct 11 ms 12032 KB Output is correct
11 Correct 11 ms 12032 KB Output is correct
12 Correct 11 ms 12160 KB Output is correct
13 Correct 11 ms 12032 KB Output is correct
14 Correct 11 ms 12160 KB Output is correct
15 Correct 11 ms 12032 KB Output is correct
16 Correct 11 ms 12032 KB Output is correct
17 Correct 11 ms 12160 KB Output is correct
18 Correct 11 ms 12032 KB Output is correct
19 Correct 11 ms 12032 KB Output is correct
20 Correct 11 ms 12032 KB Output is correct
21 Correct 11 ms 12032 KB Output is correct
22 Correct 11 ms 12032 KB Output is correct
23 Correct 10 ms 12032 KB Output is correct
24 Correct 11 ms 12160 KB Output is correct
25 Correct 11 ms 12160 KB Output is correct
26 Correct 13 ms 12288 KB Output is correct
27 Correct 13 ms 12160 KB Output is correct
28 Correct 13 ms 12416 KB Output is correct
29 Correct 13 ms 12160 KB Output is correct
30 Correct 13 ms 12160 KB Output is correct
31 Correct 11 ms 12032 KB Output is correct
32 Correct 13 ms 12416 KB Output is correct
33 Correct 11 ms 12032 KB Output is correct
34 Correct 12 ms 12160 KB Output is correct
35 Correct 13 ms 12288 KB Output is correct
36 Correct 13 ms 12288 KB Output is correct
37 Correct 13 ms 12288 KB Output is correct
38 Correct 14 ms 12032 KB Output is correct
39 Correct 13 ms 12288 KB Output is correct
40 Correct 13 ms 12288 KB Output is correct
41 Correct 13 ms 12212 KB Output is correct
42 Correct 12 ms 12288 KB Output is correct
43 Correct 13 ms 12544 KB Output is correct
44 Correct 11 ms 12160 KB Output is correct
45 Correct 13 ms 12288 KB Output is correct
46 Correct 13 ms 12288 KB Output is correct
47 Correct 11 ms 12032 KB Output is correct
48 Correct 13 ms 12288 KB Output is correct
49 Correct 12 ms 12288 KB Output is correct
50 Correct 13 ms 12288 KB Output is correct
51 Correct 13 ms 12288 KB Output is correct
52 Correct 12 ms 12416 KB Output is correct
53 Correct 12 ms 12288 KB Output is correct
54 Correct 13 ms 12288 KB Output is correct
55 Correct 11 ms 12160 KB Output is correct
56 Correct 65 ms 16372 KB Output is correct
57 Correct 112 ms 16120 KB Output is correct
58 Correct 13 ms 12160 KB Output is correct
59 Correct 11 ms 12032 KB Output is correct
60 Correct 11 ms 12160 KB Output is correct
61 Correct 117 ms 16120 KB Output is correct
62 Correct 12 ms 12160 KB Output is correct
63 Correct 103 ms 24696 KB Output is correct
64 Correct 112 ms 16120 KB Output is correct
65 Correct 13 ms 12288 KB Output is correct
66 Correct 113 ms 18424 KB Output is correct
67 Correct 13 ms 12288 KB Output is correct
68 Correct 13 ms 12288 KB Output is correct
69 Correct 67 ms 17776 KB Output is correct
70 Correct 13 ms 12544 KB Output is correct
71 Correct 119 ms 17528 KB Output is correct
72 Correct 112 ms 20344 KB Output is correct
73 Correct 11 ms 12160 KB Output is correct
74 Correct 104 ms 26580 KB Output is correct
75 Correct 96 ms 22008 KB Output is correct
76 Correct 12 ms 12288 KB Output is correct
77 Correct 13 ms 12288 KB Output is correct
78 Correct 65 ms 16368 KB Output is correct
79 Correct 75 ms 17396 KB Output is correct
80 Correct 13 ms 12288 KB Output is correct
81 Correct 13 ms 12160 KB Output is correct
82 Correct 14 ms 12140 KB Output is correct
83 Correct 11 ms 12032 KB Output is correct
84 Correct 12 ms 12160 KB Output is correct
85 Correct 122 ms 16548 KB Output is correct
86 Correct 13 ms 12264 KB Output is correct
87 Correct 122 ms 17656 KB Output is correct
88 Correct 11 ms 12160 KB Output is correct
89 Correct 128 ms 17656 KB Output is correct
90 Correct 135 ms 18168 KB Output is correct
91 Correct 127 ms 18680 KB Output is correct
92 Correct 75 ms 17964 KB Output is correct
93 Correct 12 ms 12288 KB Output is correct
94 Correct 11 ms 12032 KB Output is correct
95 Correct 72 ms 18800 KB Output is correct
96 Correct 124 ms 23288 KB Output is correct
97 Correct 13 ms 12288 KB Output is correct
98 Correct 11 ms 12032 KB Output is correct
99 Correct 74 ms 18540 KB Output is correct
100 Correct 14 ms 12288 KB Output is correct
101 Correct 137 ms 17784 KB Output is correct
102 Correct 107 ms 21752 KB Output is correct
103 Correct 13 ms 12288 KB Output is correct
104 Correct 13 ms 12288 KB Output is correct
105 Correct 13 ms 12288 KB Output is correct
106 Correct 12 ms 12288 KB Output is correct
107 Correct 13 ms 12288 KB Output is correct
108 Correct 430 ms 45028 KB Output is correct
109 Correct 892 ms 61560 KB Output is correct
110 Correct 829 ms 75176 KB Output is correct
111 Correct 890 ms 73976 KB Output is correct
112 Correct 817 ms 62200 KB Output is correct
113 Correct 458 ms 47196 KB Output is correct
114 Correct 912 ms 39800 KB Output is correct
115 Correct 897 ms 39928 KB Output is correct
116 Correct 987 ms 42616 KB Output is correct
117 Correct 1030 ms 46200 KB Output is correct
118 Correct 889 ms 40312 KB Output is correct
119 Correct 997 ms 46264 KB Output is correct
120 Correct 798 ms 62896 KB Output is correct
121 Correct 980 ms 46200 KB Output is correct
122 Correct 1016 ms 44408 KB Output is correct
123 Correct 447 ms 47588 KB Output is correct
124 Correct 1176 ms 45432 KB Output is correct