Submission #104615

# Submission time Handle Problem Language Result Execution time Memory
104615 2019-04-08T11:38:51 Z teomrn Tug of War (BOI15_tug) C++14
100 / 100
885 ms 11172 KB
#include <stdio.h>
#include <set>
#include <assert.h>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <iostream>
using namespace std;

const int NMAX = 60010;

namespace FindCycles {
    set <int> who_wants_me[NMAX];
    int i_want[NMAX][2];
    int power[NMAX];
    int n;
    int ans[2];
    bool choosen[NMAX];

    void make_him_choose(int id) {
        for (int c = 0; c <= 1; c++) {
            int where = i_want[id][c];
            if (who_wants_me[where].size() == 1) {
                /// eu sunt ala
//                cout << id << " m-am dus la " << where << '\n';
                choosen[id] = 1;
                ans[where > n] += power[id];
                who_wants_me[where].erase(id);

                where = i_want[id][c ^ 1];
                who_wants_me[where].erase(id);
                if (who_wants_me[where].size() == 1)
                    make_him_choose(*who_wants_me[where].begin());
            }
        }
    }

    void seek_cycles(int id, int from, int & s1, int & s2) {
        if (choosen[id])
            return;

        choosen[id] = 1;
        s1 += power[id];
        swap(s1, s2);

        int urm = i_want[id][0] + i_want[id][1] - from;
        assert(who_wants_me[urm].size() == 2);

        int nextid = *who_wants_me[urm].begin() + *who_wants_me[urm].rbegin() - id;
        seek_cycles(nextid, urm, s1, s2);
    }

    vector <pair <int, int>> solve(bool & found, int & s_left, int & s_right) {
    /// suma din stanga, suma din dreapta, perechi de genu (a, b)

        for (int i = 1; i <= 2 * n; i++) {
            who_wants_me[i_want[i][0]].insert(i);
            who_wants_me[i_want[i][1]].insert(i);
        }

        found = 1;
        for (int i = 1; i <= 2 * n; i++)
            if (!choosen[i])
                make_him_choose(i);

        for (int i = 1; i <= 2 * n; i++)
            if (who_wants_me[i].size() > 2)
                found = 0;

        if (found == 0)
            return vector <pair <int, int>> ();

        map <int, int> nr;
        vector <pair <int, int>> r;

        s_left = ans[0], s_right = ans[1];

//        for (int i = 1; i <= 2 * n; i++) {
//            cout << "tipu i vrea " << i_want[i][0] << " si " << i_want[i][1] << "   setul i are ";
//            for (auto j : who_wants_me[i])
//                cout << i << ' ';
//            cout << '\n';
//        }

        for (int i = 1; i <= 2 * n; i++) {
            if (!choosen[i]) {
                int a = 0, b = 0;
                seek_cycles(i, i_want[i][0], a, b);
                if (a < b)
                    swap(a, b);
                s_left += a, s_right += b;
                nr[2 * (a - b)]++;
                //cout << "Pot sa pun setul " << a << ' ' << b << '\n';
            }
        }

        for (auto i : nr)
            r.push_back(i);

        return r;
    }

}

bool can[600010];

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);

    FindCycles::n = n;

    for (int i = 1; i <= 2 * n; i++) {
        scanf("%d%d%d", &FindCycles::i_want[i][0], &FindCycles::i_want[i][1], &FindCycles::power[i]);
        FindCycles::i_want[i][1] += n;
    }

    bool ok;
    int s_left, s_right;

    auto vec = FindCycles::solve(ok, s_left, s_right);


    if (!ok) {
        printf("NO\n");
        return 0;
    }
    if (abs(s_left - s_right) <= k) {
        printf("YES\n");
        return 0;
    }
    if (s_left < s_right) {
        printf("NO\n");
        return 0;
    }

    int smin = s_left - s_right - k;
    int smax = s_left - s_right + k;

    //cout << s_left << ' ' << s_right << ' ' << smin << ' ' << smax << '\n';

    can[0] = 1;

    for (auto ob : vec) {
        int l = ob.first, nr = ob.second;
        //cout << l << ' ' << nr << '\n';
        for (int start = 0; start < l; start++) {
            int mana = -1;
            for (int poz = start; poz <= smax; poz += l, mana--) {
                if (can[poz])
                    mana = nr;
                else if (mana >= 0)
                    can[poz] = 1;
            }
        }
    }

    ok = 0;
    for (int i = smin; i <= smax; i++)
        if (can[i])
            ok = 1;

    if (ok)
        printf("YES\n");
    else
        printf("NO\n");

    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:111:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~
tug.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &FindCycles::i_want[i][0], &FindCycles::i_want[i][1], &FindCycles::power[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3328 KB Output is correct
2 Correct 6 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 4 ms 3200 KB Output is correct
5 Correct 5 ms 3200 KB Output is correct
6 Correct 6 ms 3328 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 4 ms 3200 KB Output is correct
9 Correct 5 ms 3200 KB Output is correct
10 Correct 5 ms 3200 KB Output is correct
11 Correct 5 ms 3200 KB Output is correct
12 Correct 5 ms 3200 KB Output is correct
13 Correct 6 ms 3200 KB Output is correct
14 Correct 5 ms 3200 KB Output is correct
15 Correct 4 ms 3200 KB Output is correct
16 Correct 5 ms 3200 KB Output is correct
17 Correct 4 ms 3200 KB Output is correct
18 Correct 6 ms 3200 KB Output is correct
19 Correct 5 ms 3200 KB Output is correct
20 Correct 4 ms 3200 KB Output is correct
21 Correct 5 ms 3200 KB Output is correct
22 Correct 6 ms 3200 KB Output is correct
23 Correct 5 ms 3200 KB Output is correct
24 Correct 5 ms 3200 KB Output is correct
25 Correct 6 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3328 KB Output is correct
2 Correct 6 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 4 ms 3200 KB Output is correct
5 Correct 5 ms 3200 KB Output is correct
6 Correct 6 ms 3328 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 4 ms 3200 KB Output is correct
9 Correct 5 ms 3200 KB Output is correct
10 Correct 5 ms 3200 KB Output is correct
11 Correct 5 ms 3200 KB Output is correct
12 Correct 5 ms 3200 KB Output is correct
13 Correct 6 ms 3200 KB Output is correct
14 Correct 5 ms 3200 KB Output is correct
15 Correct 4 ms 3200 KB Output is correct
16 Correct 5 ms 3200 KB Output is correct
17 Correct 4 ms 3200 KB Output is correct
18 Correct 6 ms 3200 KB Output is correct
19 Correct 5 ms 3200 KB Output is correct
20 Correct 4 ms 3200 KB Output is correct
21 Correct 5 ms 3200 KB Output is correct
22 Correct 6 ms 3200 KB Output is correct
23 Correct 5 ms 3200 KB Output is correct
24 Correct 5 ms 3200 KB Output is correct
25 Correct 6 ms 3200 KB Output is correct
26 Correct 8 ms 3712 KB Output is correct
27 Correct 18 ms 3712 KB Output is correct
28 Correct 8 ms 3712 KB Output is correct
29 Correct 18 ms 3712 KB Output is correct
30 Correct 8 ms 3712 KB Output is correct
31 Correct 18 ms 3712 KB Output is correct
32 Correct 9 ms 3712 KB Output is correct
33 Correct 19 ms 3712 KB Output is correct
34 Correct 8 ms 3584 KB Output is correct
35 Correct 21 ms 3712 KB Output is correct
36 Correct 8 ms 3584 KB Output is correct
37 Correct 25 ms 3712 KB Output is correct
38 Correct 9 ms 3740 KB Output is correct
39 Correct 17 ms 3712 KB Output is correct
40 Correct 9 ms 3712 KB Output is correct
41 Correct 22 ms 3704 KB Output is correct
42 Correct 9 ms 3712 KB Output is correct
43 Correct 19 ms 3712 KB Output is correct
44 Correct 9 ms 3584 KB Output is correct
45 Correct 20 ms 3712 KB Output is correct
46 Correct 7 ms 3584 KB Output is correct
47 Correct 9 ms 3712 KB Output is correct
48 Correct 10 ms 3704 KB Output is correct
49 Correct 11 ms 3584 KB Output is correct
50 Correct 11 ms 3712 KB Output is correct
51 Correct 9 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 5552 KB Output is correct
2 Correct 23 ms 5504 KB Output is correct
3 Correct 26 ms 5504 KB Output is correct
4 Correct 25 ms 5496 KB Output is correct
5 Correct 38 ms 5504 KB Output is correct
6 Correct 32 ms 5496 KB Output is correct
7 Correct 27 ms 5504 KB Output is correct
8 Correct 27 ms 5504 KB Output is correct
9 Correct 29 ms 5576 KB Output is correct
10 Correct 38 ms 5496 KB Output is correct
11 Correct 39 ms 5624 KB Output is correct
12 Correct 35 ms 5648 KB Output is correct
13 Correct 41 ms 5496 KB Output is correct
14 Correct 26 ms 5476 KB Output is correct
15 Correct 24 ms 5504 KB Output is correct
16 Correct 26 ms 5632 KB Output is correct
17 Correct 21 ms 5504 KB Output is correct
18 Correct 34 ms 5516 KB Output is correct
19 Correct 23 ms 5504 KB Output is correct
20 Correct 25 ms 5504 KB Output is correct
21 Correct 21 ms 5504 KB Output is correct
22 Correct 21 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3328 KB Output is correct
2 Correct 6 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 4 ms 3200 KB Output is correct
5 Correct 5 ms 3200 KB Output is correct
6 Correct 6 ms 3328 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 4 ms 3200 KB Output is correct
9 Correct 5 ms 3200 KB Output is correct
10 Correct 5 ms 3200 KB Output is correct
11 Correct 5 ms 3200 KB Output is correct
12 Correct 5 ms 3200 KB Output is correct
13 Correct 6 ms 3200 KB Output is correct
14 Correct 5 ms 3200 KB Output is correct
15 Correct 4 ms 3200 KB Output is correct
16 Correct 5 ms 3200 KB Output is correct
17 Correct 4 ms 3200 KB Output is correct
18 Correct 6 ms 3200 KB Output is correct
19 Correct 5 ms 3200 KB Output is correct
20 Correct 4 ms 3200 KB Output is correct
21 Correct 5 ms 3200 KB Output is correct
22 Correct 6 ms 3200 KB Output is correct
23 Correct 5 ms 3200 KB Output is correct
24 Correct 5 ms 3200 KB Output is correct
25 Correct 6 ms 3200 KB Output is correct
26 Correct 8 ms 3712 KB Output is correct
27 Correct 18 ms 3712 KB Output is correct
28 Correct 8 ms 3712 KB Output is correct
29 Correct 18 ms 3712 KB Output is correct
30 Correct 8 ms 3712 KB Output is correct
31 Correct 18 ms 3712 KB Output is correct
32 Correct 9 ms 3712 KB Output is correct
33 Correct 19 ms 3712 KB Output is correct
34 Correct 8 ms 3584 KB Output is correct
35 Correct 21 ms 3712 KB Output is correct
36 Correct 8 ms 3584 KB Output is correct
37 Correct 25 ms 3712 KB Output is correct
38 Correct 9 ms 3740 KB Output is correct
39 Correct 17 ms 3712 KB Output is correct
40 Correct 9 ms 3712 KB Output is correct
41 Correct 22 ms 3704 KB Output is correct
42 Correct 9 ms 3712 KB Output is correct
43 Correct 19 ms 3712 KB Output is correct
44 Correct 9 ms 3584 KB Output is correct
45 Correct 20 ms 3712 KB Output is correct
46 Correct 7 ms 3584 KB Output is correct
47 Correct 9 ms 3712 KB Output is correct
48 Correct 10 ms 3704 KB Output is correct
49 Correct 11 ms 3584 KB Output is correct
50 Correct 11 ms 3712 KB Output is correct
51 Correct 9 ms 3712 KB Output is correct
52 Correct 29 ms 5552 KB Output is correct
53 Correct 23 ms 5504 KB Output is correct
54 Correct 26 ms 5504 KB Output is correct
55 Correct 25 ms 5496 KB Output is correct
56 Correct 38 ms 5504 KB Output is correct
57 Correct 32 ms 5496 KB Output is correct
58 Correct 27 ms 5504 KB Output is correct
59 Correct 27 ms 5504 KB Output is correct
60 Correct 29 ms 5576 KB Output is correct
61 Correct 38 ms 5496 KB Output is correct
62 Correct 39 ms 5624 KB Output is correct
63 Correct 35 ms 5648 KB Output is correct
64 Correct 41 ms 5496 KB Output is correct
65 Correct 26 ms 5476 KB Output is correct
66 Correct 24 ms 5504 KB Output is correct
67 Correct 26 ms 5632 KB Output is correct
68 Correct 21 ms 5504 KB Output is correct
69 Correct 34 ms 5516 KB Output is correct
70 Correct 23 ms 5504 KB Output is correct
71 Correct 25 ms 5504 KB Output is correct
72 Correct 21 ms 5504 KB Output is correct
73 Correct 21 ms 5496 KB Output is correct
74 Correct 90 ms 10604 KB Output is correct
75 Correct 639 ms 11128 KB Output is correct
76 Correct 96 ms 10620 KB Output is correct
77 Correct 785 ms 11000 KB Output is correct
78 Correct 81 ms 10488 KB Output is correct
79 Correct 82 ms 10516 KB Output is correct
80 Correct 749 ms 11128 KB Output is correct
81 Correct 105 ms 10744 KB Output is correct
82 Correct 47 ms 10872 KB Output is correct
83 Correct 81 ms 10616 KB Output is correct
84 Correct 698 ms 11172 KB Output is correct
85 Correct 94 ms 10648 KB Output is correct
86 Correct 764 ms 10952 KB Output is correct
87 Correct 84 ms 10616 KB Output is correct
88 Correct 646 ms 11088 KB Output is correct
89 Correct 127 ms 10488 KB Output is correct
90 Correct 729 ms 10996 KB Output is correct
91 Correct 88 ms 10744 KB Output is correct
92 Correct 885 ms 11076 KB Output is correct
93 Correct 80 ms 10616 KB Output is correct
94 Correct 74 ms 10360 KB Output is correct
95 Correct 83 ms 10388 KB Output is correct
96 Correct 93 ms 10488 KB Output is correct
97 Correct 99 ms 10872 KB Output is correct
98 Correct 120 ms 10872 KB Output is correct