답안 #79617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
79617 2018-10-15T06:15:56 Z minhtung0404 Tug of War (BOI15_tug) C++17
100 / 100
2879 ms 6116 KB
//https://oj.uz/problem/view/BOI15_tug

#include<bits/stdc++.h>
const int N = 30005;
using namespace std;

typedef pair <int, int> ii;
vector <ii> adj[2*N];
vector <int> mv;

int n, k, cnt[2*N], d[2*N], C[2], sum;
bool dead[2*N], dp[40*N];

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> k;

    for (int i = 1; i <= 2*n; i++){
        int l, r, c;
        cin >> l >> r >> c;
        adj[l].push_back(ii(r+n, c)); cnt[l]++;
        adj[r+n].push_back(ii(l, c)); cnt[r+n]++;
        d[i] = -1;
    }

    queue <int> mq;
    for (int i = 1; i <= 2*n; i++) if (cnt[i] == 1) mq.push(i);

    while (mq.size()){
        int u = mq.front(); mq.pop();
        dead[u] = true;
        for (auto p : adj[u]){
            if (dead[p.first]) continue;
            cnt[p.first]--;
            C[(u <= n)] += p.second;
            if (cnt[p.first] == 1) mq.push(p.first);
        }
    }

    for (int i = 1; i <= 2*n; i++) {
        if (!dead[i]) {
            if (cnt[i] != 2) return cout << "NO", 0;
            int u = i, num = 0, ck = 1;

            for (auto p : adj[u]){
                int v = p.first, cost = p.second;
                if (dead[v]) continue;
                num = cost;
            }

            while (ck){
                dead[u] = true; ck = 0;
                for (auto p : adj[u]){
                    int v = p.first, cost = p.second;
                    if (dead[v]) continue;
                    u = v; num = cost - num; ck = 1;
                    break;
                }
            }

            mv.push_back(abs(num));
            sum -= abs(num);
        }
    }
    for (int i = 0; i < 40*N; i++) dp[i] = 0;
    sort(mv.begin(), mv.end());
    dp[0] = 1;

    int cnt = 0;
    for (; cnt < mv.size();) {
        int val = mv[cnt], num = 0;
        while (cnt < mv.size() && mv[cnt] == val) cnt++, num += val;

        for (int i = 0; i < val; i++){
            int last = -1;
            for (int j = i; j < 40*N; j += val){
                if (dp[j]) {
                    last = j;
                }
                else {
                    if (last != -1 && (j - last) <= num) dp[j] = 1;
                }
            }
        }
    }

    for (int i = 0; i < 40*N; i++){
        if (abs(C[0] - C[1] + sum + i*2) <= k && dp[i]) return cout << "YES", 0;
    }
    cout << "NO";
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:70:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (; cnt < mv.size();) {
            ~~~~^~~~~~~~~~~
tug.cpp:72:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (cnt < mv.size() && mv[cnt] == val) cnt++, num += val;
                ~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 3064 KB Output is correct
2 Correct 20 ms 3092 KB Output is correct
3 Correct 20 ms 3092 KB Output is correct
4 Correct 20 ms 3204 KB Output is correct
5 Correct 13 ms 3204 KB Output is correct
6 Correct 27 ms 3204 KB Output is correct
7 Correct 19 ms 3204 KB Output is correct
8 Correct 16 ms 3204 KB Output is correct
9 Correct 21 ms 3204 KB Output is correct
10 Correct 16 ms 3204 KB Output is correct
11 Correct 19 ms 3344 KB Output is correct
12 Correct 14 ms 3344 KB Output is correct
13 Correct 19 ms 3344 KB Output is correct
14 Correct 18 ms 3344 KB Output is correct
15 Correct 19 ms 3344 KB Output is correct
16 Correct 19 ms 3344 KB Output is correct
17 Correct 24 ms 3344 KB Output is correct
18 Correct 13 ms 3344 KB Output is correct
19 Correct 19 ms 3344 KB Output is correct
20 Correct 15 ms 3344 KB Output is correct
21 Correct 3 ms 3344 KB Output is correct
22 Correct 12 ms 3344 KB Output is correct
23 Correct 13 ms 3344 KB Output is correct
24 Correct 18 ms 3344 KB Output is correct
25 Correct 30 ms 3344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 3064 KB Output is correct
2 Correct 20 ms 3092 KB Output is correct
3 Correct 20 ms 3092 KB Output is correct
4 Correct 20 ms 3204 KB Output is correct
5 Correct 13 ms 3204 KB Output is correct
6 Correct 27 ms 3204 KB Output is correct
7 Correct 19 ms 3204 KB Output is correct
8 Correct 16 ms 3204 KB Output is correct
9 Correct 21 ms 3204 KB Output is correct
10 Correct 16 ms 3204 KB Output is correct
11 Correct 19 ms 3344 KB Output is correct
12 Correct 14 ms 3344 KB Output is correct
13 Correct 19 ms 3344 KB Output is correct
14 Correct 18 ms 3344 KB Output is correct
15 Correct 19 ms 3344 KB Output is correct
16 Correct 19 ms 3344 KB Output is correct
17 Correct 24 ms 3344 KB Output is correct
18 Correct 13 ms 3344 KB Output is correct
19 Correct 19 ms 3344 KB Output is correct
20 Correct 15 ms 3344 KB Output is correct
21 Correct 3 ms 3344 KB Output is correct
22 Correct 12 ms 3344 KB Output is correct
23 Correct 13 ms 3344 KB Output is correct
24 Correct 18 ms 3344 KB Output is correct
25 Correct 30 ms 3344 KB Output is correct
26 Correct 38 ms 3344 KB Output is correct
27 Correct 916 ms 3400 KB Output is correct
28 Correct 40 ms 3480 KB Output is correct
29 Correct 867 ms 3480 KB Output is correct
30 Correct 37 ms 3480 KB Output is correct
31 Correct 924 ms 3480 KB Output is correct
32 Correct 33 ms 3480 KB Output is correct
33 Correct 851 ms 3480 KB Output is correct
34 Correct 46 ms 3480 KB Output is correct
35 Correct 945 ms 3480 KB Output is correct
36 Correct 35 ms 3480 KB Output is correct
37 Correct 915 ms 3480 KB Output is correct
38 Correct 49 ms 3480 KB Output is correct
39 Correct 882 ms 3480 KB Output is correct
40 Correct 34 ms 3480 KB Output is correct
41 Correct 878 ms 3480 KB Output is correct
42 Correct 37 ms 3480 KB Output is correct
43 Correct 875 ms 3480 KB Output is correct
44 Correct 34 ms 3480 KB Output is correct
45 Correct 1016 ms 3480 KB Output is correct
46 Correct 34 ms 3480 KB Output is correct
47 Correct 5 ms 3480 KB Output is correct
48 Correct 34 ms 3480 KB Output is correct
49 Correct 35 ms 3480 KB Output is correct
50 Correct 32 ms 3480 KB Output is correct
51 Correct 36 ms 3480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 4092 KB Output is correct
2 Correct 13 ms 4092 KB Output is correct
3 Correct 13 ms 4092 KB Output is correct
4 Correct 12 ms 4092 KB Output is correct
5 Correct 14 ms 4092 KB Output is correct
6 Correct 13 ms 4092 KB Output is correct
7 Correct 15 ms 4112 KB Output is correct
8 Correct 16 ms 4112 KB Output is correct
9 Correct 14 ms 4112 KB Output is correct
10 Correct 13 ms 4112 KB Output is correct
11 Correct 15 ms 4112 KB Output is correct
12 Correct 14 ms 4112 KB Output is correct
13 Correct 14 ms 4112 KB Output is correct
14 Correct 13 ms 4112 KB Output is correct
15 Correct 13 ms 4112 KB Output is correct
16 Correct 15 ms 4112 KB Output is correct
17 Correct 12 ms 4112 KB Output is correct
18 Correct 14 ms 4112 KB Output is correct
19 Correct 14 ms 4112 KB Output is correct
20 Correct 13 ms 4148 KB Output is correct
21 Correct 14 ms 4148 KB Output is correct
22 Correct 14 ms 4192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 3064 KB Output is correct
2 Correct 20 ms 3092 KB Output is correct
3 Correct 20 ms 3092 KB Output is correct
4 Correct 20 ms 3204 KB Output is correct
5 Correct 13 ms 3204 KB Output is correct
6 Correct 27 ms 3204 KB Output is correct
7 Correct 19 ms 3204 KB Output is correct
8 Correct 16 ms 3204 KB Output is correct
9 Correct 21 ms 3204 KB Output is correct
10 Correct 16 ms 3204 KB Output is correct
11 Correct 19 ms 3344 KB Output is correct
12 Correct 14 ms 3344 KB Output is correct
13 Correct 19 ms 3344 KB Output is correct
14 Correct 18 ms 3344 KB Output is correct
15 Correct 19 ms 3344 KB Output is correct
16 Correct 19 ms 3344 KB Output is correct
17 Correct 24 ms 3344 KB Output is correct
18 Correct 13 ms 3344 KB Output is correct
19 Correct 19 ms 3344 KB Output is correct
20 Correct 15 ms 3344 KB Output is correct
21 Correct 3 ms 3344 KB Output is correct
22 Correct 12 ms 3344 KB Output is correct
23 Correct 13 ms 3344 KB Output is correct
24 Correct 18 ms 3344 KB Output is correct
25 Correct 30 ms 3344 KB Output is correct
26 Correct 38 ms 3344 KB Output is correct
27 Correct 916 ms 3400 KB Output is correct
28 Correct 40 ms 3480 KB Output is correct
29 Correct 867 ms 3480 KB Output is correct
30 Correct 37 ms 3480 KB Output is correct
31 Correct 924 ms 3480 KB Output is correct
32 Correct 33 ms 3480 KB Output is correct
33 Correct 851 ms 3480 KB Output is correct
34 Correct 46 ms 3480 KB Output is correct
35 Correct 945 ms 3480 KB Output is correct
36 Correct 35 ms 3480 KB Output is correct
37 Correct 915 ms 3480 KB Output is correct
38 Correct 49 ms 3480 KB Output is correct
39 Correct 882 ms 3480 KB Output is correct
40 Correct 34 ms 3480 KB Output is correct
41 Correct 878 ms 3480 KB Output is correct
42 Correct 37 ms 3480 KB Output is correct
43 Correct 875 ms 3480 KB Output is correct
44 Correct 34 ms 3480 KB Output is correct
45 Correct 1016 ms 3480 KB Output is correct
46 Correct 34 ms 3480 KB Output is correct
47 Correct 5 ms 3480 KB Output is correct
48 Correct 34 ms 3480 KB Output is correct
49 Correct 35 ms 3480 KB Output is correct
50 Correct 32 ms 3480 KB Output is correct
51 Correct 36 ms 3480 KB Output is correct
52 Correct 14 ms 4092 KB Output is correct
53 Correct 13 ms 4092 KB Output is correct
54 Correct 13 ms 4092 KB Output is correct
55 Correct 12 ms 4092 KB Output is correct
56 Correct 14 ms 4092 KB Output is correct
57 Correct 13 ms 4092 KB Output is correct
58 Correct 15 ms 4112 KB Output is correct
59 Correct 16 ms 4112 KB Output is correct
60 Correct 14 ms 4112 KB Output is correct
61 Correct 13 ms 4112 KB Output is correct
62 Correct 15 ms 4112 KB Output is correct
63 Correct 14 ms 4112 KB Output is correct
64 Correct 14 ms 4112 KB Output is correct
65 Correct 13 ms 4112 KB Output is correct
66 Correct 13 ms 4112 KB Output is correct
67 Correct 15 ms 4112 KB Output is correct
68 Correct 12 ms 4112 KB Output is correct
69 Correct 14 ms 4112 KB Output is correct
70 Correct 14 ms 4112 KB Output is correct
71 Correct 13 ms 4148 KB Output is correct
72 Correct 14 ms 4148 KB Output is correct
73 Correct 14 ms 4192 KB Output is correct
74 Correct 70 ms 5764 KB Output is correct
75 Correct 2804 ms 5764 KB Output is correct
76 Correct 68 ms 5884 KB Output is correct
77 Correct 2702 ms 5884 KB Output is correct
78 Correct 67 ms 5884 KB Output is correct
79 Correct 68 ms 5884 KB Output is correct
80 Correct 2850 ms 5884 KB Output is correct
81 Correct 60 ms 5884 KB Output is correct
82 Correct 42 ms 5884 KB Output is correct
83 Correct 80 ms 5884 KB Output is correct
84 Correct 2842 ms 5884 KB Output is correct
85 Correct 69 ms 5884 KB Output is correct
86 Correct 2788 ms 5884 KB Output is correct
87 Correct 64 ms 5884 KB Output is correct
88 Correct 2744 ms 5884 KB Output is correct
89 Correct 66 ms 5884 KB Output is correct
90 Correct 2775 ms 5884 KB Output is correct
91 Correct 93 ms 5884 KB Output is correct
92 Correct 2879 ms 5884 KB Output is correct
93 Correct 108 ms 5884 KB Output is correct
94 Correct 36 ms 5884 KB Output is correct
95 Correct 74 ms 6116 KB Output is correct
96 Correct 74 ms 6116 KB Output is correct
97 Correct 97 ms 6116 KB Output is correct
98 Correct 77 ms 6116 KB Output is correct