답안 #986001

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
986001 2024-05-19T15:00:48 Z andrei_boaca Mergers (JOI19_mergers) C++17
100 / 100
1694 ms 210528 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
int n,K,par[500005];
int nr[500005],dp[21][500005],niv[500005],in[500005],out[500005],timp;
vector<int> muchii[500005];
vector<int> culs[500005];
int grad[500005];
map<pii,int> ban;
bool isancestor(int a,int b)
{
    return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b)
{
    if(niv[a]>niv[b])
        swap(a,b);
    if(isancestor(a,b))
        return a;
    for(int i=20;i>=0;i--)
        if(dp[i][a]!=0&&!isancestor(dp[i][a],b))
            a=dp[i][a];
    return par[a];
}
void initdfs(int nod)
{
    timp++;
    in[nod]=timp;
    dp[0][nod]=par[nod];
    for(int i=1;i<=20;i++)
        dp[i][nod]=dp[i-1][dp[i-1][nod]];
    for(int i:muchii[nod])
        if(i!=par[nod])
        {
            par[i]=nod;
            niv[i]=niv[nod]+1;
            initdfs(i);
        }
    out[nod]=timp;
}
void calc(int nod)
{
    for(int i:muchii[nod])
        if(i!=par[nod])
        {
            calc(i);
            nr[nod]+=nr[i];
        }
    if(nr[nod]==0&&nod!=1)
    {
        ban[{nod,par[nod]}]=1;
        ban[{par[nod],nod}]=1;
    }
}
int comp[500005],nrcomp;
void dfs(int nod)
{
    comp[nod]=nrcomp;
    for(int i:muchii[nod])
        if(!comp[i])
            if(ban.count({nod,i})==0)
                dfs(i);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>K;
    for(int i=1;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
    }
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        culs[x].push_back(i);
    }
    initdfs(1);
    for(int i=1;i<=K;i++)
    {
        for(int j=1;j<culs[i].size();j++)
        {
            int a=culs[i][0];
            int b=culs[i][j];
            int lca=LCA(a,b);
            nr[a]++;
            nr[b]++;
            nr[lca]-=2;
        }
    }
    calc(1);
    if(ban.empty())
    {
        cout<<0;
        return 0;
    }
    for(int i=1;i<=n;i++)
        if(!comp[i])
        {
            nrcomp++;
            dfs(i);
        }
    for(auto z:ban)
    {
        pii p=z.first;
        if(p.first>p.second)
            continue;
        int a=comp[p.first];
        int b=comp[p.second];
        grad[a]++;
        grad[b]++;
    }
    int ans=0;
    for(int i=1;i<=nrcomp;i++)
        if(grad[i]==1)
            ans++;
    cout<<ans/2+ans%2;
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         for(int j=1;j<culs[i].size();j++)
      |                     ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 74324 KB Output is correct
2 Correct 11 ms 74332 KB Output is correct
3 Correct 11 ms 74332 KB Output is correct
4 Correct 11 ms 74332 KB Output is correct
5 Correct 11 ms 74296 KB Output is correct
6 Correct 11 ms 74332 KB Output is correct
7 Correct 11 ms 72284 KB Output is correct
8 Correct 12 ms 74580 KB Output is correct
9 Correct 11 ms 74584 KB Output is correct
10 Correct 11 ms 74332 KB Output is correct
11 Correct 12 ms 74344 KB Output is correct
12 Correct 12 ms 74252 KB Output is correct
13 Correct 11 ms 74332 KB Output is correct
14 Correct 11 ms 74364 KB Output is correct
15 Correct 11 ms 74332 KB Output is correct
16 Correct 11 ms 72140 KB Output is correct
17 Correct 11 ms 74332 KB Output is correct
18 Correct 12 ms 74332 KB Output is correct
19 Correct 11 ms 72280 KB Output is correct
20 Correct 11 ms 72284 KB Output is correct
21 Correct 12 ms 74364 KB Output is correct
22 Correct 11 ms 74332 KB Output is correct
23 Correct 11 ms 74248 KB Output is correct
24 Correct 11 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 74324 KB Output is correct
2 Correct 11 ms 74332 KB Output is correct
3 Correct 11 ms 74332 KB Output is correct
4 Correct 11 ms 74332 KB Output is correct
5 Correct 11 ms 74296 KB Output is correct
6 Correct 11 ms 74332 KB Output is correct
7 Correct 11 ms 72284 KB Output is correct
8 Correct 12 ms 74580 KB Output is correct
9 Correct 11 ms 74584 KB Output is correct
10 Correct 11 ms 74332 KB Output is correct
11 Correct 12 ms 74344 KB Output is correct
12 Correct 12 ms 74252 KB Output is correct
13 Correct 11 ms 74332 KB Output is correct
14 Correct 11 ms 74364 KB Output is correct
15 Correct 11 ms 74332 KB Output is correct
16 Correct 11 ms 72140 KB Output is correct
17 Correct 11 ms 74332 KB Output is correct
18 Correct 12 ms 74332 KB Output is correct
19 Correct 11 ms 72280 KB Output is correct
20 Correct 11 ms 72284 KB Output is correct
21 Correct 12 ms 74364 KB Output is correct
22 Correct 11 ms 74332 KB Output is correct
23 Correct 11 ms 74248 KB Output is correct
24 Correct 11 ms 74332 KB Output is correct
25 Correct 11 ms 74364 KB Output is correct
26 Correct 14 ms 74696 KB Output is correct
27 Correct 13 ms 74332 KB Output is correct
28 Correct 15 ms 74996 KB Output is correct
29 Correct 14 ms 74844 KB Output is correct
30 Correct 13 ms 74332 KB Output is correct
31 Correct 13 ms 74176 KB Output is correct
32 Correct 15 ms 74808 KB Output is correct
33 Correct 12 ms 74508 KB Output is correct
34 Correct 13 ms 74328 KB Output is correct
35 Correct 16 ms 72784 KB Output is correct
36 Correct 13 ms 74444 KB Output is correct
37 Correct 14 ms 74588 KB Output is correct
38 Correct 12 ms 74328 KB Output is correct
39 Correct 14 ms 74556 KB Output is correct
40 Correct 13 ms 74332 KB Output is correct
41 Correct 14 ms 74332 KB Output is correct
42 Correct 14 ms 74588 KB Output is correct
43 Correct 13 ms 74844 KB Output is correct
44 Correct 20 ms 74308 KB Output is correct
45 Correct 19 ms 74580 KB Output is correct
46 Correct 14 ms 74588 KB Output is correct
47 Correct 12 ms 74248 KB Output is correct
48 Correct 15 ms 74424 KB Output is correct
49 Correct 14 ms 72796 KB Output is correct
50 Correct 14 ms 73052 KB Output is correct
51 Correct 13 ms 74588 KB Output is correct
52 Correct 15 ms 74428 KB Output is correct
53 Correct 13 ms 74588 KB Output is correct
54 Correct 13 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 74324 KB Output is correct
2 Correct 11 ms 74332 KB Output is correct
3 Correct 11 ms 74332 KB Output is correct
4 Correct 11 ms 74332 KB Output is correct
5 Correct 11 ms 74296 KB Output is correct
6 Correct 11 ms 74332 KB Output is correct
7 Correct 11 ms 72284 KB Output is correct
8 Correct 12 ms 74580 KB Output is correct
9 Correct 11 ms 74584 KB Output is correct
10 Correct 11 ms 74332 KB Output is correct
11 Correct 12 ms 74344 KB Output is correct
12 Correct 12 ms 74252 KB Output is correct
13 Correct 11 ms 74332 KB Output is correct
14 Correct 11 ms 74364 KB Output is correct
15 Correct 11 ms 74332 KB Output is correct
16 Correct 11 ms 72140 KB Output is correct
17 Correct 11 ms 74332 KB Output is correct
18 Correct 12 ms 74332 KB Output is correct
19 Correct 11 ms 72280 KB Output is correct
20 Correct 11 ms 72284 KB Output is correct
21 Correct 12 ms 74364 KB Output is correct
22 Correct 11 ms 74332 KB Output is correct
23 Correct 11 ms 74248 KB Output is correct
24 Correct 11 ms 74332 KB Output is correct
25 Correct 12 ms 74316 KB Output is correct
26 Correct 53 ms 80708 KB Output is correct
27 Correct 70 ms 80072 KB Output is correct
28 Correct 13 ms 74332 KB Output is correct
29 Correct 11 ms 74328 KB Output is correct
30 Correct 12 ms 74332 KB Output is correct
31 Correct 81 ms 80212 KB Output is correct
32 Correct 12 ms 74328 KB Output is correct
33 Correct 88 ms 90348 KB Output is correct
34 Correct 76 ms 80472 KB Output is correct
35 Correct 12 ms 74332 KB Output is correct
36 Correct 96 ms 81508 KB Output is correct
37 Correct 13 ms 74328 KB Output is correct
38 Correct 12 ms 74332 KB Output is correct
39 Correct 54 ms 80528 KB Output is correct
40 Correct 12 ms 74844 KB Output is correct
41 Correct 64 ms 80176 KB Output is correct
42 Correct 90 ms 83796 KB Output is correct
43 Correct 11 ms 74328 KB Output is correct
44 Correct 79 ms 90572 KB Output is correct
45 Correct 91 ms 85588 KB Output is correct
46 Correct 13 ms 74328 KB Output is correct
47 Correct 12 ms 74332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 80612 KB Output is correct
2 Correct 148 ms 94156 KB Output is correct
3 Correct 14 ms 74584 KB Output is correct
4 Correct 13 ms 74332 KB Output is correct
5 Correct 11 ms 74332 KB Output is correct
6 Correct 12 ms 74328 KB Output is correct
7 Correct 12 ms 74328 KB Output is correct
8 Correct 107 ms 83028 KB Output is correct
9 Correct 14 ms 74328 KB Output is correct
10 Correct 87 ms 80580 KB Output is correct
11 Correct 12 ms 74328 KB Output is correct
12 Correct 88 ms 80468 KB Output is correct
13 Correct 111 ms 84140 KB Output is correct
14 Correct 165 ms 94036 KB Output is correct
15 Correct 56 ms 80584 KB Output is correct
16 Correct 13 ms 74584 KB Output is correct
17 Correct 11 ms 74332 KB Output is correct
18 Correct 137 ms 93476 KB Output is correct
19 Correct 168 ms 99692 KB Output is correct
20 Correct 13 ms 74584 KB Output is correct
21 Correct 12 ms 74332 KB Output is correct
22 Correct 88 ms 84136 KB Output is correct
23 Correct 13 ms 74588 KB Output is correct
24 Correct 106 ms 82096 KB Output is correct
25 Correct 192 ms 97984 KB Output is correct
26 Correct 13 ms 72792 KB Output is correct
27 Correct 14 ms 73052 KB Output is correct
28 Correct 13 ms 74584 KB Output is correct
29 Correct 12 ms 74332 KB Output is correct
30 Correct 14 ms 74620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 74324 KB Output is correct
2 Correct 11 ms 74332 KB Output is correct
3 Correct 11 ms 74332 KB Output is correct
4 Correct 11 ms 74332 KB Output is correct
5 Correct 11 ms 74296 KB Output is correct
6 Correct 11 ms 74332 KB Output is correct
7 Correct 11 ms 72284 KB Output is correct
8 Correct 12 ms 74580 KB Output is correct
9 Correct 11 ms 74584 KB Output is correct
10 Correct 11 ms 74332 KB Output is correct
11 Correct 12 ms 74344 KB Output is correct
12 Correct 12 ms 74252 KB Output is correct
13 Correct 11 ms 74332 KB Output is correct
14 Correct 11 ms 74364 KB Output is correct
15 Correct 11 ms 74332 KB Output is correct
16 Correct 11 ms 72140 KB Output is correct
17 Correct 11 ms 74332 KB Output is correct
18 Correct 12 ms 74332 KB Output is correct
19 Correct 11 ms 72280 KB Output is correct
20 Correct 11 ms 72284 KB Output is correct
21 Correct 12 ms 74364 KB Output is correct
22 Correct 11 ms 74332 KB Output is correct
23 Correct 11 ms 74248 KB Output is correct
24 Correct 11 ms 74332 KB Output is correct
25 Correct 11 ms 74364 KB Output is correct
26 Correct 14 ms 74696 KB Output is correct
27 Correct 13 ms 74332 KB Output is correct
28 Correct 15 ms 74996 KB Output is correct
29 Correct 14 ms 74844 KB Output is correct
30 Correct 13 ms 74332 KB Output is correct
31 Correct 13 ms 74176 KB Output is correct
32 Correct 15 ms 74808 KB Output is correct
33 Correct 12 ms 74508 KB Output is correct
34 Correct 13 ms 74328 KB Output is correct
35 Correct 16 ms 72784 KB Output is correct
36 Correct 13 ms 74444 KB Output is correct
37 Correct 14 ms 74588 KB Output is correct
38 Correct 12 ms 74328 KB Output is correct
39 Correct 14 ms 74556 KB Output is correct
40 Correct 13 ms 74332 KB Output is correct
41 Correct 14 ms 74332 KB Output is correct
42 Correct 14 ms 74588 KB Output is correct
43 Correct 13 ms 74844 KB Output is correct
44 Correct 20 ms 74308 KB Output is correct
45 Correct 19 ms 74580 KB Output is correct
46 Correct 14 ms 74588 KB Output is correct
47 Correct 12 ms 74248 KB Output is correct
48 Correct 15 ms 74424 KB Output is correct
49 Correct 14 ms 72796 KB Output is correct
50 Correct 14 ms 73052 KB Output is correct
51 Correct 13 ms 74588 KB Output is correct
52 Correct 15 ms 74428 KB Output is correct
53 Correct 13 ms 74588 KB Output is correct
54 Correct 13 ms 74332 KB Output is correct
55 Correct 12 ms 74316 KB Output is correct
56 Correct 53 ms 80708 KB Output is correct
57 Correct 70 ms 80072 KB Output is correct
58 Correct 13 ms 74332 KB Output is correct
59 Correct 11 ms 74328 KB Output is correct
60 Correct 12 ms 74332 KB Output is correct
61 Correct 81 ms 80212 KB Output is correct
62 Correct 12 ms 74328 KB Output is correct
63 Correct 88 ms 90348 KB Output is correct
64 Correct 76 ms 80472 KB Output is correct
65 Correct 12 ms 74332 KB Output is correct
66 Correct 96 ms 81508 KB Output is correct
67 Correct 13 ms 74328 KB Output is correct
68 Correct 12 ms 74332 KB Output is correct
69 Correct 54 ms 80528 KB Output is correct
70 Correct 12 ms 74844 KB Output is correct
71 Correct 64 ms 80176 KB Output is correct
72 Correct 90 ms 83796 KB Output is correct
73 Correct 11 ms 74328 KB Output is correct
74 Correct 79 ms 90572 KB Output is correct
75 Correct 91 ms 85588 KB Output is correct
76 Correct 13 ms 74328 KB Output is correct
77 Correct 12 ms 74332 KB Output is correct
78 Correct 52 ms 80612 KB Output is correct
79 Correct 148 ms 94156 KB Output is correct
80 Correct 14 ms 74584 KB Output is correct
81 Correct 13 ms 74332 KB Output is correct
82 Correct 11 ms 74332 KB Output is correct
83 Correct 12 ms 74328 KB Output is correct
84 Correct 12 ms 74328 KB Output is correct
85 Correct 107 ms 83028 KB Output is correct
86 Correct 14 ms 74328 KB Output is correct
87 Correct 87 ms 80580 KB Output is correct
88 Correct 12 ms 74328 KB Output is correct
89 Correct 88 ms 80468 KB Output is correct
90 Correct 111 ms 84140 KB Output is correct
91 Correct 165 ms 94036 KB Output is correct
92 Correct 56 ms 80584 KB Output is correct
93 Correct 13 ms 74584 KB Output is correct
94 Correct 11 ms 74332 KB Output is correct
95 Correct 137 ms 93476 KB Output is correct
96 Correct 168 ms 99692 KB Output is correct
97 Correct 13 ms 74584 KB Output is correct
98 Correct 12 ms 74332 KB Output is correct
99 Correct 88 ms 84136 KB Output is correct
100 Correct 13 ms 74588 KB Output is correct
101 Correct 106 ms 82096 KB Output is correct
102 Correct 192 ms 97984 KB Output is correct
103 Correct 13 ms 72792 KB Output is correct
104 Correct 14 ms 73052 KB Output is correct
105 Correct 13 ms 74584 KB Output is correct
106 Correct 12 ms 74332 KB Output is correct
107 Correct 14 ms 74620 KB Output is correct
108 Correct 632 ms 118204 KB Output is correct
109 Correct 864 ms 129976 KB Output is correct
110 Correct 692 ms 145596 KB Output is correct
111 Correct 1548 ms 210528 KB Output is correct
112 Correct 1538 ms 172952 KB Output is correct
113 Correct 1363 ms 171100 KB Output is correct
114 Correct 622 ms 104556 KB Output is correct
115 Correct 515 ms 104396 KB Output is correct
116 Correct 902 ms 109432 KB Output is correct
117 Correct 1410 ms 183384 KB Output is correct
118 Correct 773 ms 105992 KB Output is correct
119 Correct 1444 ms 183216 KB Output is correct
120 Correct 1694 ms 203124 KB Output is correct
121 Correct 1442 ms 182804 KB Output is correct
122 Correct 935 ms 125008 KB Output is correct
123 Correct 1409 ms 184412 KB Output is correct
124 Correct 1266 ms 159632 KB Output is correct