Submission #86079

# Submission time Handle Problem Language Result Execution time Memory
86079 2018-11-24T16:59:55 Z Flying_dragon_02 Tug of War (BOI15_tug) C++14
0 / 100
66 ms 21580 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int N = 60005;

void bye() {
    cout << "NO";
    exit(0);
}

int n, lmao;

int l[N], r[N], s[N], sum, incycle[N], par[N], used[20 * N], dp[2][20 * N], total;

vector<int> graph[3 * N];

bool vis[3 * N];

vector<int> can;

vector<vector<int>> vec;

void bfs(int u, int p) {
    vis[u] = 1;
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i];
        if(v == p) continue;
        if(vis[v]) {
            if(incycle[v]) continue;
            vector<int> temp;
            int z = u;
            while(z != v) {
                incycle[z] = 1;
                temp.pb(z);
                z = par[z];
            }
            incycle[v] = 1;
            temp.pb(v);
            vec.pb(temp);
            continue;
        }
        par[v] = u;
        bfs(v, u);
    }
}

void check() {
    for(int i = 2 * n + 1; i <= 4 * n; i++) {
        if(graph[i].size() >= 3)
            bye();
    }
}

int main() {
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> n >> lmao;
    for(int i = 1; i <= 2 * n; i++) {
        cin >> l[i] >> r[i] >> s[i];
        total += s[i];
        graph[i].pb(2 * n + l[i]);
        graph[2 * n + l[i]].pb(i);
        graph[i].pb(3 * n + r[i]);
        graph[3 * n + r[i]].pb(i);
    }
    check();
    for(int i = 1; i <= 4 * n; i++) {
        if(!vis[i])
            bfs(i, i);
    }
    memset(vis, 0, sizeof(vis));
    for(int i = 2 * n + 1; i <= 4 * n; i++) {
        if(incycle[i] == 0 && vis[i] == 0 && graph[i].size() == 1) {
            int z = i;
            while(incycle[z] == 0 && vis[z] == 0) {
                vis[z] = 1;
                if(z >= 2 * n + 1 && z <= 3 * n)
                    sum += s[par[z]];
                z = par[z];
            }
        }
    }
    for(int i = 0; i < vec.size(); i++) {
        vector<int> temp = vec[i];
        int sum1 = 0, sum2 = 0, m = temp.size();
        for(int j = 0; j < m; j++) {
            if(temp[j] <= 2 * n && temp[(j + 1) % m] >= 2 * n + 1 && temp[(j + 1) % m] <= 3 * n)
                sum1 += s[temp[j]];
        }
        for(int j = 0; j < m; j++) {
            if(temp[j] <= 2 * n && temp[(j - 1 + m) % m] >= 2 * n + 1 && temp[(j - 1 + m) % m] <= 3 * n)
                sum2 += s[temp[j]];
        }
        sum += min(sum1, sum2);
        sum1 -= min(sum1, sum2); sum2 -= min(sum1, sum2);
        if(max(sum1, sum2) == 0) continue;
        if(used[max(sum1, sum2)] == 0)
            can.pb(max(sum1, sum2));
        used[max(sum1, sum2)]++;
    }
    sort(can.begin(), can.end());
    int lol = can.size();
    for(int i = 0; i <= 1; i++) {
        for(int j = 0; j < 20 * N; j++)
            dp[i][j] = 20 * N;
    }
    dp[0][0] = 0;
    for(int i = 0; i < lol; i++) {
        for(int j = 0; j < 20 * N; j++) {
            if(dp[0][j] > used[can[i]]) continue;
            dp[1][j] = 0;
            if(dp[0][j] < used[can[i]])
                dp[1][j + can[i]] = min(dp[1][j + can[i]], dp[0][j] + 1);
        }
        for(int j = 0; j < 20 * N; j++) {
            dp[0][j] = dp[1][j];
            dp[1][j] = 20 * N;
        }
    }
    for(int i = 0; i < 20 * N; i++) {
        if(dp[0][i] < 20 * N) {
            int temp = i + sum;
            int lef = total - temp;
            if(abs(lef - temp) <= lmao) {
                cout << "YES";
                exit(0);
            }
        }
    }
    bye();
}

Compilation message

tug.cpp: In function 'void bfs(int, int)':
tug.cpp:33:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < graph[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:90:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < vec.size(); i++) {
                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 39 ms 14200 KB Output is correct
2 Correct 40 ms 14316 KB Output is correct
3 Correct 36 ms 14416 KB Output is correct
4 Correct 40 ms 14416 KB Output is correct
5 Correct 36 ms 14416 KB Output is correct
6 Correct 36 ms 14416 KB Output is correct
7 Correct 47 ms 14544 KB Output is correct
8 Correct 41 ms 14544 KB Output is correct
9 Correct 35 ms 14544 KB Output is correct
10 Correct 45 ms 14544 KB Output is correct
11 Correct 44 ms 14544 KB Output is correct
12 Correct 40 ms 14544 KB Output is correct
13 Correct 38 ms 14544 KB Output is correct
14 Incorrect 40 ms 14740 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 39 ms 14200 KB Output is correct
2 Correct 40 ms 14316 KB Output is correct
3 Correct 36 ms 14416 KB Output is correct
4 Correct 40 ms 14416 KB Output is correct
5 Correct 36 ms 14416 KB Output is correct
6 Correct 36 ms 14416 KB Output is correct
7 Correct 47 ms 14544 KB Output is correct
8 Correct 41 ms 14544 KB Output is correct
9 Correct 35 ms 14544 KB Output is correct
10 Correct 45 ms 14544 KB Output is correct
11 Correct 44 ms 14544 KB Output is correct
12 Correct 40 ms 14544 KB Output is correct
13 Correct 38 ms 14544 KB Output is correct
14 Incorrect 40 ms 14740 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 17100 KB Output is correct
2 Correct 22 ms 17100 KB Output is correct
3 Correct 40 ms 17712 KB Output is correct
4 Correct 25 ms 17712 KB Output is correct
5 Correct 39 ms 18080 KB Output is correct
6 Correct 19 ms 18080 KB Output is correct
7 Correct 36 ms 18556 KB Output is correct
8 Correct 22 ms 18556 KB Output is correct
9 Correct 40 ms 19028 KB Output is correct
10 Correct 27 ms 19028 KB Output is correct
11 Correct 45 ms 19492 KB Output is correct
12 Correct 22 ms 19492 KB Output is correct
13 Correct 41 ms 19956 KB Output is correct
14 Correct 66 ms 20184 KB Output is correct
15 Correct 20 ms 20184 KB Output is correct
16 Correct 60 ms 20716 KB Output is correct
17 Correct 23 ms 20716 KB Output is correct
18 Correct 52 ms 21116 KB Output is correct
19 Correct 33 ms 21116 KB Output is correct
20 Correct 62 ms 21580 KB Output is correct
21 Correct 22 ms 21580 KB Output is correct
22 Incorrect 30 ms 21580 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 14200 KB Output is correct
2 Correct 40 ms 14316 KB Output is correct
3 Correct 36 ms 14416 KB Output is correct
4 Correct 40 ms 14416 KB Output is correct
5 Correct 36 ms 14416 KB Output is correct
6 Correct 36 ms 14416 KB Output is correct
7 Correct 47 ms 14544 KB Output is correct
8 Correct 41 ms 14544 KB Output is correct
9 Correct 35 ms 14544 KB Output is correct
10 Correct 45 ms 14544 KB Output is correct
11 Correct 44 ms 14544 KB Output is correct
12 Correct 40 ms 14544 KB Output is correct
13 Correct 38 ms 14544 KB Output is correct
14 Incorrect 40 ms 14740 KB Output isn't correct
15 Halted 0 ms 0 KB -