Submission #376848

# Submission time Handle Problem Language Result Execution time Memory
376848 2021-03-12T05:25:52 Z daniel920712 Mergers (JOI19_mergers) C++14
70 / 100
3000 ms 132048 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <vector>

using namespace std;
set < int > a,b;
set < pair < int , int > > road;
int now=0;
vector < int > Next[500005];
int color[500005];
int deg[500005]={0};
int tt[500005]={0};
int Father[500005]={0};
vector < int > con[500005];
vector < int > use[500005];
int sum[500005]={0};
int M;
void F(int here,int fa,int x)
{
    int i,j;
    con[here][color[here]]++;
    sum[color[here]]++;
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        F(i,here,x);
        for(j=1;j<=M;j++) con[here][j]+=con[i][j];
    }
}
void F3(int here,int fa,int x)
{
    int ok=1,i,j;
    for(auto i:Next[here])
    {
        ok=1;
        if(i==fa) continue;
        F3(i,here,x);
        for(j=1;j<=M;j++)
        {
            if(con[i][j]>0&&con[i][j]<sum[j])
            {
                //printf("%d %d %d %d %d\n",here,i,j,con[i][j],sum[j]);
                ok=0;
            }
        }
        if(ok)
        {
            //printf("%d %d\n",here,i);
            road.insert(make_pair(here,i));
            road.insert(make_pair(i,here));
        }
    }
}
void F2(int here,int fa,int xx)
{
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        if(road.find(make_pair(here,i))!=road.end())
        {

            now++;
            deg[xx]++;
            deg[now]++;
            //printf("%d %d %d %d\n",here,i,deg[xx],deg[now]);
            F2(i,here,now);
        }
        else F2(i,here,xx);
    }
}
void F4(int here,int fa,int deg)
{
    tt[here]=deg;
    Father[here]=fa;
    for(auto i:Next[here])
    {
        if(i!=fa)
        {
            road.insert(make_pair(here,i));
            F4(i,here,deg+1);
        }
    }
}
int LCA(int a,int b)
{
    if(a==b) return a;
    int x,y;
    if(tt[a]==tt[b])
    {
        x=Father[a];
        y=Father[b];
        //printf("aa %d %d\n",x,a);
        //printf("bb %d %d\n",y,b);
        if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
        if(road.find(make_pair(y,b))!=road.end()) road.erase(make_pair(y,b));
        return LCA(x,y);
    }
    else
    {

        if(tt[a]<tt[b]) swap(a,b);
        x=Father[a];
        //printf("cc %d %d\n",x,a);
        if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
        return LCA(x,b);
    }
}
int main()
{
    int N,K,i,j,x,y,xx=0,ok=1;
    scanf("%d %d",&N,&M);
    for(i=1;i<N;i++)
    {
        scanf("%d %d",&x,&y);
        Next[x].push_back(y);
        Next[y].push_back(x);

    }
    for(i=1;i<=N;i++)
    {
        scanf("%d",&color[i]);
        use[color[i]].push_back(i);
    }
    
    if(N<=3000||M<=50)
    {
        for(i=1;i<=N;i++)
        {
            //scanf("%d",&color[i]);
            for(j=0;j<=M;j++) con[i].push_back(0);
        }
        F(1,-1,0);
        F3(1,-1,0);

    }
    else
    {
        F4(1,-1,0);
        for(i=1;i<=M;i++)
        {
            K=use[i].size();
            if(K>=2)
            {
                x=use[i][0];
                for(j=1;j<K;j++) x=LCA(x,use[i][j]);
            }
        }
    }


    now=0;
    F2(1,-1,0);
    for(i=0;i<=now;i++) if(deg[i]==1) xx++;

    if(xx==1) printf("0\n");
    else printf("%d\n",(xx+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'void F(int, int, int)':
mergers.cpp:22:9: warning: unused variable 'i' [-Wunused-variable]
   22 |     int i,j;
      |         ^
mergers.cpp: In function 'void F3(int, int, int)':
mergers.cpp:34:14: warning: unused variable 'i' [-Wunused-variable]
   34 |     int ok=1,i,j;
      |              ^
mergers.cpp: In function 'int main()':
mergers.cpp:112:26: warning: unused variable 'ok' [-Wunused-variable]
  112 |     int N,K,i,j,x,y,xx=0,ok=1;
      |                          ^~
mergers.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  113 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  116 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  123 |         scanf("%d",&color[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35692 KB Output is correct
2 Correct 27 ms 35564 KB Output is correct
3 Correct 25 ms 35564 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 29 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 28 ms 35564 KB Output is correct
13 Correct 25 ms 35564 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 28 ms 35564 KB Output is correct
16 Correct 25 ms 35564 KB Output is correct
17 Correct 29 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 26 ms 35564 KB Output is correct
22 Correct 25 ms 35564 KB Output is correct
23 Correct 26 ms 35692 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35692 KB Output is correct
2 Correct 27 ms 35564 KB Output is correct
3 Correct 25 ms 35564 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 29 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 28 ms 35564 KB Output is correct
13 Correct 25 ms 35564 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 28 ms 35564 KB Output is correct
16 Correct 25 ms 35564 KB Output is correct
17 Correct 29 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 26 ms 35564 KB Output is correct
22 Correct 25 ms 35564 KB Output is correct
23 Correct 26 ms 35692 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 25 ms 35692 KB Output is correct
26 Correct 85 ms 60036 KB Output is correct
27 Correct 34 ms 36460 KB Output is correct
28 Correct 128 ms 83308 KB Output is correct
29 Correct 119 ms 83388 KB Output is correct
30 Correct 34 ms 38764 KB Output is correct
31 Correct 30 ms 35564 KB Output is correct
32 Correct 151 ms 83308 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 28 ms 36460 KB Output is correct
35 Correct 123 ms 83436 KB Output is correct
36 Correct 30 ms 36460 KB Output is correct
37 Correct 88 ms 60012 KB Output is correct
38 Correct 25 ms 35692 KB Output is correct
39 Correct 74 ms 59884 KB Output is correct
40 Correct 26 ms 35820 KB Output is correct
41 Correct 27 ms 35948 KB Output is correct
42 Correct 72 ms 59884 KB Output is correct
43 Correct 29 ms 36076 KB Output is correct
44 Correct 27 ms 35692 KB Output is correct
45 Correct 137 ms 81900 KB Output is correct
46 Correct 74 ms 59628 KB Output is correct
47 Correct 26 ms 35564 KB Output is correct
48 Correct 50 ms 47852 KB Output is correct
49 Correct 130 ms 83308 KB Output is correct
50 Correct 151 ms 83436 KB Output is correct
51 Correct 50 ms 47852 KB Output is correct
52 Correct 28 ms 36460 KB Output is correct
53 Correct 73 ms 59884 KB Output is correct
54 Correct 29 ms 36460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35692 KB Output is correct
2 Correct 27 ms 35564 KB Output is correct
3 Correct 25 ms 35564 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 29 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 28 ms 35564 KB Output is correct
13 Correct 25 ms 35564 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 28 ms 35564 KB Output is correct
16 Correct 25 ms 35564 KB Output is correct
17 Correct 29 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 26 ms 35564 KB Output is correct
22 Correct 25 ms 35564 KB Output is correct
23 Correct 26 ms 35692 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 27 ms 35564 KB Output is correct
26 Correct 204 ms 66532 KB Output is correct
27 Correct 248 ms 66540 KB Output is correct
28 Correct 29 ms 36460 KB Output is correct
29 Correct 25 ms 35564 KB Output is correct
30 Correct 25 ms 35564 KB Output is correct
31 Correct 241 ms 66284 KB Output is correct
32 Correct 29 ms 36588 KB Output is correct
33 Correct 278 ms 74860 KB Output is correct
34 Correct 272 ms 67564 KB Output is correct
35 Correct 30 ms 36588 KB Output is correct
36 Correct 273 ms 68588 KB Output is correct
37 Correct 27 ms 35820 KB Output is correct
38 Correct 27 ms 35948 KB Output is correct
39 Correct 212 ms 68068 KB Output is correct
40 Correct 28 ms 36204 KB Output is correct
41 Correct 138 ms 44136 KB Output is correct
42 Correct 276 ms 70636 KB Output is correct
43 Correct 29 ms 35564 KB Output is correct
44 Correct 184 ms 53212 KB Output is correct
45 Correct 282 ms 72188 KB Output is correct
46 Correct 29 ms 36588 KB Output is correct
47 Correct 29 ms 36588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 66532 KB Output is correct
2 Correct 246 ms 48500 KB Output is correct
3 Correct 86 ms 60012 KB Output is correct
4 Correct 29 ms 36460 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 28 ms 36460 KB Output is correct
8 Correct 343 ms 46316 KB Output is correct
9 Correct 29 ms 36460 KB Output is correct
10 Correct 275 ms 45448 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 583 ms 45220 KB Output is correct
13 Correct 259 ms 46316 KB Output is correct
14 Correct 221 ms 48236 KB Output is correct
15 Correct 202 ms 66532 KB Output is correct
16 Correct 74 ms 59884 KB Output is correct
17 Correct 25 ms 35692 KB Output is correct
18 Correct 195 ms 48100 KB Output is correct
19 Correct 246 ms 52972 KB Output is correct
20 Correct 75 ms 59628 KB Output is correct
21 Correct 25 ms 35564 KB Output is correct
22 Correct 257 ms 46820 KB Output is correct
23 Correct 50 ms 47852 KB Output is correct
24 Correct 284 ms 45660 KB Output is correct
25 Correct 227 ms 51436 KB Output is correct
26 Correct 118 ms 83412 KB Output is correct
27 Correct 125 ms 83436 KB Output is correct
28 Correct 49 ms 47852 KB Output is correct
29 Correct 28 ms 36460 KB Output is correct
30 Correct 74 ms 59884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35692 KB Output is correct
2 Correct 27 ms 35564 KB Output is correct
3 Correct 25 ms 35564 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 29 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 28 ms 35564 KB Output is correct
13 Correct 25 ms 35564 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 28 ms 35564 KB Output is correct
16 Correct 25 ms 35564 KB Output is correct
17 Correct 29 ms 35564 KB Output is correct
18 Correct 25 ms 35564 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 26 ms 35564 KB Output is correct
22 Correct 25 ms 35564 KB Output is correct
23 Correct 26 ms 35692 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 25 ms 35692 KB Output is correct
26 Correct 85 ms 60036 KB Output is correct
27 Correct 34 ms 36460 KB Output is correct
28 Correct 128 ms 83308 KB Output is correct
29 Correct 119 ms 83388 KB Output is correct
30 Correct 34 ms 38764 KB Output is correct
31 Correct 30 ms 35564 KB Output is correct
32 Correct 151 ms 83308 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 28 ms 36460 KB Output is correct
35 Correct 123 ms 83436 KB Output is correct
36 Correct 30 ms 36460 KB Output is correct
37 Correct 88 ms 60012 KB Output is correct
38 Correct 25 ms 35692 KB Output is correct
39 Correct 74 ms 59884 KB Output is correct
40 Correct 26 ms 35820 KB Output is correct
41 Correct 27 ms 35948 KB Output is correct
42 Correct 72 ms 59884 KB Output is correct
43 Correct 29 ms 36076 KB Output is correct
44 Correct 27 ms 35692 KB Output is correct
45 Correct 137 ms 81900 KB Output is correct
46 Correct 74 ms 59628 KB Output is correct
47 Correct 26 ms 35564 KB Output is correct
48 Correct 50 ms 47852 KB Output is correct
49 Correct 130 ms 83308 KB Output is correct
50 Correct 151 ms 83436 KB Output is correct
51 Correct 50 ms 47852 KB Output is correct
52 Correct 28 ms 36460 KB Output is correct
53 Correct 73 ms 59884 KB Output is correct
54 Correct 29 ms 36460 KB Output is correct
55 Correct 27 ms 35564 KB Output is correct
56 Correct 204 ms 66532 KB Output is correct
57 Correct 248 ms 66540 KB Output is correct
58 Correct 29 ms 36460 KB Output is correct
59 Correct 25 ms 35564 KB Output is correct
60 Correct 25 ms 35564 KB Output is correct
61 Correct 241 ms 66284 KB Output is correct
62 Correct 29 ms 36588 KB Output is correct
63 Correct 278 ms 74860 KB Output is correct
64 Correct 272 ms 67564 KB Output is correct
65 Correct 30 ms 36588 KB Output is correct
66 Correct 273 ms 68588 KB Output is correct
67 Correct 27 ms 35820 KB Output is correct
68 Correct 27 ms 35948 KB Output is correct
69 Correct 212 ms 68068 KB Output is correct
70 Correct 28 ms 36204 KB Output is correct
71 Correct 138 ms 44136 KB Output is correct
72 Correct 276 ms 70636 KB Output is correct
73 Correct 29 ms 35564 KB Output is correct
74 Correct 184 ms 53212 KB Output is correct
75 Correct 282 ms 72188 KB Output is correct
76 Correct 29 ms 36588 KB Output is correct
77 Correct 29 ms 36588 KB Output is correct
78 Correct 207 ms 66532 KB Output is correct
79 Correct 246 ms 48500 KB Output is correct
80 Correct 86 ms 60012 KB Output is correct
81 Correct 29 ms 36460 KB Output is correct
82 Correct 24 ms 35564 KB Output is correct
83 Correct 24 ms 35564 KB Output is correct
84 Correct 28 ms 36460 KB Output is correct
85 Correct 343 ms 46316 KB Output is correct
86 Correct 29 ms 36460 KB Output is correct
87 Correct 275 ms 45448 KB Output is correct
88 Correct 25 ms 35692 KB Output is correct
89 Correct 583 ms 45220 KB Output is correct
90 Correct 259 ms 46316 KB Output is correct
91 Correct 221 ms 48236 KB Output is correct
92 Correct 202 ms 66532 KB Output is correct
93 Correct 74 ms 59884 KB Output is correct
94 Correct 25 ms 35692 KB Output is correct
95 Correct 195 ms 48100 KB Output is correct
96 Correct 246 ms 52972 KB Output is correct
97 Correct 75 ms 59628 KB Output is correct
98 Correct 25 ms 35564 KB Output is correct
99 Correct 257 ms 46820 KB Output is correct
100 Correct 50 ms 47852 KB Output is correct
101 Correct 284 ms 45660 KB Output is correct
102 Correct 227 ms 51436 KB Output is correct
103 Correct 118 ms 83412 KB Output is correct
104 Correct 125 ms 83436 KB Output is correct
105 Correct 49 ms 47852 KB Output is correct
106 Correct 28 ms 36460 KB Output is correct
107 Correct 74 ms 59884 KB Output is correct
108 Correct 2212 ms 93916 KB Output is correct
109 Correct 1070 ms 98500 KB Output is correct
110 Correct 1065 ms 112092 KB Output is correct
111 Correct 1890 ms 132048 KB Output is correct
112 Correct 2324 ms 119216 KB Output is correct
113 Correct 1510 ms 103512 KB Output is correct
114 Correct 883 ms 76888 KB Output is correct
115 Correct 905 ms 76804 KB Output is correct
116 Execution timed out 3085 ms 91232 KB Time limit exceeded
117 Halted 0 ms 0 KB -