QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#528228#2514. Cable ProtectionNYCU_CartesianTree#AC ✓32ms19424kbC++141.6kb2024-08-23 11:48:182024-08-23 11:48:19

Judging History

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

  • [2024-08-23 11:48:19]
  • 评测
  • 测评结果:AC
  • 用时:32ms
  • 内存:19424kb
  • [2024-08-23 11:48:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long 
#define pb push_back
#define F first 
#define S second 

const int mol=1e9+7;

vector<int>node[200005];
int dp[200005][2], f[200005][2];
int n, m;

void ff(int v, int pre){
    f[v][1]=1;
    for(int k: node[v]){
        if(k==pre) continue;
        ff(k, v);
        f[v][0]+=f[k][1];
        f[v][1]+=min(f[k][1], f[k][0]);
    }
}

void solve(){
    cin>>n>>m;
    for(int i=1;i<=n+m;i++){
        int g, h;
        cin>>g>>h;
        if(g<n&&h<n) continue;
        node[g].pb(h), node[h].pb(g);
    }
    for(int i=0;i<n;i++) {
        ff(i, i);
    }
    int ans=1e18;
    for(int i=0;i<n;i++){
        if(i==0){
            dp[i][0]=f[i][0], dp[i][1]=1e18;
        }
        else if(i<n-1){
            dp[i][0]=f[i][0]+dp[i-1][1], dp[i][1]=f[i][1]+min(dp[i-1][1], dp[i-1][0]);
        }
        else{
            dp[i][1]=f[i][1]+min(dp[i-1][0], dp[i-1][1]);
            ans=dp[i][1];
        }
    }
    for(int i=0;i<n;i++){
        if(i==0){
            dp[i][0]=1e18, dp[i][1]=f[i][1];
        }
        else if(i<n-1){
            dp[i][0]=f[i][0]+dp[i-1][1], dp[i][1]=f[i][1]+min(dp[i-1][1], dp[i-1][0]);
        }
        else{
            dp[i][1]=f[i][1]+min(dp[i-1][0], dp[i-1][1]);
            dp[i][0]=f[i][0]+dp[i-1][1];
            ans=min({ans, dp[i][1], dp[i][0]});
        }
    }
    cout<<ans<<"\n";
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    // int t;
    // cin>>t;
    // while(t--)
    solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 10468kb

input:

3 2
0 1
1 2
0 2
1 3
2 4

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 2ms
memory: 12284kb

input:

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

output:

5

result:

ok single line: '5'

Test #3:

score: 0
Accepted
time: 0ms
memory: 12072kb

input:

700 300
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

446

result:

ok single line: '446'

Test #4:

score: 0
Accepted
time: 0ms
memory: 11948kb

input:

4000 6000
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

4129

result:

ok single line: '4129'

Test #5:

score: 0
Accepted
time: 21ms
memory: 18284kb

input:

100 99900
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

40317

result:

ok single line: '40317'

Test #6:

score: 0
Accepted
time: 2ms
memory: 11016kb

input:

4 11
0 1
0 3
0 4
0 6
1 2
1 9
2 10
2 3
4 5
6 7
7 8
10 11
10 13
11 12
13 14

output:

7

result:

ok single line: '7'

Test #7:

score: 0
Accepted
time: 2ms
memory: 10992kb

input:

4 11
0 1
0 3
0 4
0 5
1 2
1 6
2 3
2 9
3 12
6 7
6 8
9 10
10 11
12 13
12 14

output:

5

result:

ok single line: '5'

Test #8:

score: 0
Accepted
time: 0ms
memory: 11080kb

input:

4 11
0 1
0 3
0 4
0 6
1 2
1 9
2 10
2 3
4 5
6 7
7 8
10 11
10 13
11 12
13 14

output:

7

result:

ok single line: '7'

Test #9:

score: 0
Accepted
time: 0ms
memory: 10424kb

input:

4 11
0 1
0 3
0 4
0 5
1 2
1 6
2 3
2 9
3 12
6 7
6 8
9 10
10 11
12 13
12 14

output:

5

result:

ok single line: '5'

Test #10:

score: 0
Accepted
time: 32ms
memory: 19424kb

input:

100000 100000
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51...

output:

84571

result:

ok single line: '84571'

Test #11:

score: 0
Accepted
time: 2ms
memory: 12400kb

input:

125 375
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

205

result:

ok single line: '205'

Test #12:

score: 0
Accepted
time: 2ms
memory: 11848kb

input:

3 2
0 1
1 2
0 2
1 3
2 4

output:

2

result:

ok single line: '2'

Test #13:

score: 0
Accepted
time: 0ms
memory: 11532kb

input:

4 1
0 1
1 2
2 3
0 3
1 4

output:

2

result:

ok single line: '2'

Test #14:

score: 0
Accepted
time: 3ms
memory: 15588kb

input:

30001 30000
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
5...

output:

25437

result:

ok single line: '25437'

Test #15:

score: 0
Accepted
time: 2ms
memory: 10988kb

input:

6 1
0 1
1 2
2 3
3 4
4 5
0 5
0 6

output:

3

result:

ok single line: '3'

Test #16:

score: 0
Accepted
time: 0ms
memory: 11096kb

input:

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

output:

4

result:

ok single line: '4'