QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#57825#2514. Cable ProtectionAs3b_team_f_masr#AC ✓73ms35412kbC++2.6kb2022-10-23 01:10:242022-10-23 01:10:27

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-23 01:10:27]
  • Judged
  • Verdict: AC
  • Time: 73ms
  • Memory: 35412kb
  • [2022-10-23 01:10:24]
  • Submitted

answer

#include <bits/stdc++.h>

typedef long double ld;
typedef long long ll;
using namespace std;
int di[] = {1, 0, -1, 0, -1, 1, -1, 1};
int dj[] = {0, 1, 0, -1, -1, 1, 1, -1};
const ll oo = 1e18, MOD = 998244353;
const int N = 2e5 + 5, M = 1e6 + 5;
const ld PI = acos(-1.0), EPS = 1e-9;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int n, m, dp1[N][2], dp2[N][2][2], vis[N];
vector<int> v1[N], v2[N], v;

int solve1(int node, int par, int num) {
    int &ret = dp1[node][num];
    if (~ret) return ret;
    if (v1[node].size() == 1 && node >= n) return dp1[node][num] = 0;
    if (v1[node].empty()) return dp1[node][num] = 0;
    ret = 0;
    for (auto x:v1[node]) {
        if (x != par && x >= n) {
            int tmp = 1 + solve1(x, node, 1);
            if (num) tmp = min(tmp, solve1(x, node, 0));
            ret += tmp;
        }
    }
    return ret;
}

void dfs(int node) {
    vis[node] = 1;
    v.push_back(node);
    for (auto x:v2[node]) {
        if (!vis[x]) dfs(x);
    }
}

int solve2(int idx, int pre, int lst) {
    int &ret = dp2[idx][pre][lst];
    if (~ret) return ret;
    ret = 1e9;
    if (idx == v.size() - 1) {
        ret = min(ret, solve1(v[idx], v[idx], 1));
        if (pre && lst) ret = min(ret, solve1(v[idx], v[idx], 0));
            //cout<<idx<<" "<<pre<<" "<<ret<<" "<<lst<<endl;

        return ret;
    }
    ret = min(ret, solve1(v[idx], v[idx], 1) + solve2(idx + 1, 1, lst));
    if (pre) ret = min(ret, solve1(v[idx], v[idx], 0) + solve2(idx + 1, 0, lst));
    //cout<<idx<<" "<<pre<<" "<<ret<<" "<<lst<<endl;
    return ret;
}

//#define endl '\n'
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("farm.in", "r", stdin);
    memset(dp1, -1, sizeof dp1);
    memset(dp2, -1, sizeof dp2);
    cin >> n >> m;
    for (int i = 1; i <= n + m; i++) {
        int x, y;
        cin >> x >> y;
        if (x < n && y < n) {
            v2[x].push_back(y);
            v2[y].push_back(x);
        } else {
            v1[x].push_back(y);
            v1[y].push_back(x);
        }
    }
    for (int i = 0; i < n; i++) {
        solve1(i, i, 0);
        solve1(i, i, 1);
        dp1[i][1]++;
        //swap(dp1[i][0], dp1[i][1]);
        //cout<<i<<" "<<dp1[i][0]<<" "<<dp1[i][1]<<endl;
    }
    dfs(0);
    int c1 = solve2(1, 0, 0) + dp1[v[0]][0];
    int c2 = solve2(1, 1, 1) + dp1[v[0]][1];
    //cout<<solve1(5,5,1)<<endl;
    cout << min(c1, c2);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 17680kb

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: 5ms
memory: 17768kb

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: 4ms
memory: 17932kb

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: 6ms
memory: 18480kb

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: 22ms
memory: 20944kb

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: 1ms
memory: 17716kb

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: 0ms
memory: 17656kb

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: 2ms
memory: 17596kb

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: 1ms
memory: 17660kb

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: 73ms
memory: 35412kb

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: 17780kb

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: 4ms
memory: 17768kb

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: 3ms
memory: 17740kb

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: 21ms
memory: 23056kb

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: 1ms
memory: 17816kb

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: 17824kb

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'