QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401592#72. MergersRafi220 51ms52724kbC++142.0kb2024-04-28 23:17:482024-04-28 23:17:48

Judging History

你现在查看的是最新测评结果

  • [2024-04-28 23:17:48]
  • 评测
  • 测评结果:0
  • 用时:51ms
  • 内存:52724kb
  • [2024-04-28 23:17:48]
  • 提交

answer

#include <bits/stdc++.h>

#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
int inf=1000000000000000007;
int mod=1000000007;
int mod1=998244353;

const bool multi=1;

const int N=500007,L=19;

int pre[N],post[N],c;
int skok[N][L];
vector<int>G[N];

void dfs(int v,int o)
{
    skok[v][0]=o;
    for(int i=1;i<L;i++) skok[v][i]=skok[skok[v][i-1]][i-1];
    pre[v]=c++;
    for(auto u:G[v]) if(u!=o) dfs(u,v);
    post[v]=c++;
}

bool anc(int u,int v)
{
    return pre[u]<=pre[v]&&post[u]>=post[v];
}

int lca(int u,int v)
{
    if(anc(u,v)) return u;
    if(anc(v,u)) return v;
    for(int i=19;i>=0;i--) if(!anc(skok[u][i],v)) u=skok[u][i];
    return skok[u][0];
}
vector<int>V[N];
int ile[N];

void dfs1(int v,int o)
{
    for(auto u:G[v])
    {
        if(u==o) continue;
        dfs1(u,v);
        ile[v]+=ile[u];
    }
}

int id[N],it;
int deg[N];

void dfs2(int v,int o)
{
    for(auto u:G[v])
    {
        if(u==o) continue;
        if(ile[u]==0)
        {
            id[u]=++it;
            deg[id[v]]++;
            deg[id[u]]++;
        }
        else id[u]=id[v];
        dfs2(u,v);
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k,a,b;
    cin>>n>>k;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(1,1);
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        V[a].pb(i);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<sz(V[i]);j++)
        {
            int l=lca(V[i][j],V[i][j-1]);
            ile[V[i][j]]++;
            ile[V[i][j-1]]++;
            ile[l]--;
        }
    }
    dfs1(1,0);
    id[1]=++it;
    dfs2(1,0);
    int ans=0;
    for(int i=1;i<=it;i++) if(deg[i]==1) ans++;
    cout<<max(0LL,ans-1);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 4ms
memory: 27172kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Test #2:

score: 10
Accepted
time: 0ms
memory: 27056kb

input:

3 2
2 1
3 1
2
2
1

output:

1

result:

ok single line: '1'

Test #3:

score: 10
Accepted
time: 3ms
memory: 27104kb

input:

5 2
1 2
4 5
4 2
3 4
2
1
2
1
2

output:

0

result:

ok single line: '0'

Test #4:

score: 10
Accepted
time: 4ms
memory: 27112kb

input:

7 7
5 7
4 7
6 4
1 3
2 6
3 4
6
7
2
4
5
3
1

output:

2

result:

ok single line: '2'

Test #5:

score: 10
Accepted
time: 0ms
memory: 27108kb

input:

10 5
1 3
9 6
8 6
3 10
3 2
1 6
8 7
2 5
9 4
1
1
3
2
2
5
3
1
4
2

output:

0

result:

ok single line: '0'

Test #6:

score: 10
Accepted
time: 0ms
memory: 27080kb

input:

100 1
7 97
63 10
65 17
26 85
50 92
92 20
28 83
92 51
5 4
56 2
18 27
16 73
24 78
73 10
35 6
49 10
20 11
42 23
30 7
24 69
38 87
53 45
25 3
93 57
64 47
84 73
20 91
97 31
99 45
20 38
76 9
98 94
40 72
77 38
37 7
88 8
37 78
73 8
90 61
45 68
32 29
55 37
46 88
17 14
46 12
83 100
35 40
71 20
32 92
57 88
92 6...

output:

0

result:

ok single line: '0'

Test #7:

score: 10
Accepted
time: 3ms
memory: 27048kb

input:

100 7
53 48
26 44
28 93
71 74
7 58
76 79
8 89
44 71
80 6
31 67
76 33
90 24
55 1
62 41
95 35
44 68
29 24
18 56
60 85
71 42
71 1
50 78
12 46
67 50
86 50
71 18
17 51
49 13
63 41
2 25
19 93
74 43
74 39
51 43
2 3
61 49
40 61
48 84
99 62
98 43
80 92
58 76
22 43
58 10
50 14
5 26
75 55
19 51
45 38
3 8
23 52...

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Wrong Answer
time: 3ms
memory: 27044kb

input:

9 6
7 6
9 1
2 4
4 5
9 2
8 6
9 3
8 1
3
1
6
4
4
2
5
5
6

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #66:

score: 0
Wrong Answer
time: 51ms
memory: 52724kb

input:

100000 100000
14957 4585
67467 70858
61843 47396
50630 17382
61027 39858
94990 21698
10240 22940
23505 67581
91432 14182
22040 40125
24556 60351
75822 41519
82801 23601
90653 29138
85096 34582
99587 59109
8932 45189
18235 36632
43160 14939
67600 76675
60175 65542
99294 53955
46429 66931
16822 48854
...

output:

50082

result:

wrong answer 1st lines differ - expected: '25042', found: '50082'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%