Submission #130076

# Submission time Handle Problem Language Result Execution time Memory
130076 2019-07-13T20:45:00 Z nikolapesic2802 Tug of War (BOI15_tug) C++14
100 / 100
1645 ms 9852 KB
/*
    -Make a graph where the nodes are the spots and edges connect the two spots someone is willing to take and have a weight equal to the strength of that person.
    -While there are nodes that have degrees less than 2, either print NO (if the degree is 0) or assign the person to that node.
    -Now we are left with a graph containing only cycles. For each cycle there are two options for the difference between teams.
    -Do knapsack with dp on these values to get AC.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

const int N=6e4+1,M=3e5+1;
vector<int> degree(N),visited(N);
vector<multiset<pair<int,int> > > graf(N);
int dif,n,k,a,b,c;
void dfs(int tr)
{
    if(graf[tr].size()==0)
        return;
    int sl=(*graf[tr].begin()).f;
    int t=(*graf[tr].begin()).s;
    graf[tr].erase(graf[tr].begin());
    graf[sl].erase(graf[sl].find({tr,t}));
    if(tr<n)
        dif+=t;
    else
        dif-=t;
    dfs(sl);
}
bitset<3*M/2> moze;
int main()
{
	scanf("%i %i",&n,&k);
	for(int i=0;i<2*n;i++)
        scanf("%i %i %i",&a,&b,&c),a--,b+=n-1,graf[a].insert({b,c}),graf[b].insert({a,c}),degree[a]++,degree[b]++;
    queue<int> q;
    for(int i=0;i<2*n;i++)
        if(degree[i]==1)
            q.push(i);
    while(q.size())
    {
        int tr=q.front();
        q.pop();
        visited[tr]=1;
        if(graf[tr].size()==0)
            continue;
        int sl=(*graf[tr].begin()).f;
        int t=(*graf[tr].begin()).s;
        graf[tr].erase(graf[tr].begin());
        degree[tr]--;
        degree[sl]--;
        graf[sl].erase(graf[sl].find({tr,t}));
        if(degree[sl]==1)
            q.push(sl);
        if(tr<n)
            dif+=t;
        else
            dif-=t;
    }
    vector<int> dp={abs(dif)};
    dif=0;
    for(int i=0;i<2*n;i++)
    {
        if(!visited[i])
        {
            if(degree[i]!=2)
            {
                printf("NO\n");
                return 0;
            }
            dfs(i);
            dp.pb(abs(dif));
            dif=0;
        }
    }
    sort(dp.rbegin(),dp.rend());
    while(dp.size()&&dp.back()==0)
        dp.pop_back();
    if(dp.empty())
    {
        printf("YES\n");
        return 0;
    }
    moze[M/2]=1;
    for(auto p:dp)
        moze=(moze<<p)|(moze>>p);
    bool ima=false;
    for(int i=M/2;i<=M/2+k;i++)
        if(moze[i])
            ima=true;
    if(ima)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&k);
  ~~~~~^~~~~~~~~~~~~~~
tug.cpp:56:102: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i %i %i",&a,&b,&c),a--,b+=n-1,graf[a].insert({b,c}),graf[b].insert({a,c}),degree[a]++,degree[b]++;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3960 KB Output is correct
2 Correct 5 ms 3960 KB Output is correct
3 Correct 6 ms 3832 KB Output is correct
4 Correct 6 ms 3832 KB Output is correct
5 Correct 5 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 7 ms 3832 KB Output is correct
8 Correct 5 ms 3960 KB Output is correct
9 Correct 5 ms 3960 KB Output is correct
10 Correct 5 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 5 ms 3960 KB Output is correct
13 Correct 5 ms 3832 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 5 ms 3960 KB Output is correct
16 Correct 5 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 5 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 5 ms 3960 KB Output is correct
21 Correct 5 ms 3576 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 5 ms 3832 KB Output is correct
24 Correct 5 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3960 KB Output is correct
2 Correct 5 ms 3960 KB Output is correct
3 Correct 6 ms 3832 KB Output is correct
4 Correct 6 ms 3832 KB Output is correct
5 Correct 5 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 7 ms 3832 KB Output is correct
8 Correct 5 ms 3960 KB Output is correct
9 Correct 5 ms 3960 KB Output is correct
10 Correct 5 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 5 ms 3960 KB Output is correct
13 Correct 5 ms 3832 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 5 ms 3960 KB Output is correct
16 Correct 5 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 5 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 5 ms 3960 KB Output is correct
21 Correct 5 ms 3576 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 5 ms 3832 KB Output is correct
24 Correct 5 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
26 Correct 106 ms 4316 KB Output is correct
27 Correct 22 ms 4344 KB Output is correct
28 Correct 107 ms 4348 KB Output is correct
29 Correct 22 ms 4324 KB Output is correct
30 Correct 106 ms 4216 KB Output is correct
31 Correct 22 ms 4216 KB Output is correct
32 Correct 107 ms 4316 KB Output is correct
33 Correct 22 ms 4216 KB Output is correct
34 Correct 15 ms 4264 KB Output is correct
35 Correct 22 ms 4344 KB Output is correct
36 Correct 108 ms 4336 KB Output is correct
37 Correct 22 ms 4216 KB Output is correct
38 Correct 108 ms 4344 KB Output is correct
39 Correct 22 ms 4344 KB Output is correct
40 Correct 107 ms 4488 KB Output is correct
41 Correct 21 ms 4216 KB Output is correct
42 Correct 108 ms 4344 KB Output is correct
43 Correct 22 ms 4216 KB Output is correct
44 Correct 106 ms 4344 KB Output is correct
45 Correct 22 ms 4444 KB Output is correct
46 Correct 107 ms 4268 KB Output is correct
47 Correct 7 ms 3960 KB Output is correct
48 Correct 58 ms 4216 KB Output is correct
49 Correct 58 ms 4344 KB Output is correct
50 Correct 112 ms 4216 KB Output is correct
51 Correct 111 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 5752 KB Output is correct
2 Correct 18 ms 5500 KB Output is correct
3 Correct 21 ms 5624 KB Output is correct
4 Correct 18 ms 5496 KB Output is correct
5 Correct 19 ms 5624 KB Output is correct
6 Correct 19 ms 5496 KB Output is correct
7 Correct 22 ms 5624 KB Output is correct
8 Correct 19 ms 5496 KB Output is correct
9 Correct 18 ms 5624 KB Output is correct
10 Correct 19 ms 5496 KB Output is correct
11 Correct 18 ms 5624 KB Output is correct
12 Correct 19 ms 5496 KB Output is correct
13 Correct 18 ms 5624 KB Output is correct
14 Correct 18 ms 5628 KB Output is correct
15 Correct 19 ms 5496 KB Output is correct
16 Correct 22 ms 5612 KB Output is correct
17 Correct 19 ms 5496 KB Output is correct
18 Correct 18 ms 5624 KB Output is correct
19 Correct 18 ms 5496 KB Output is correct
20 Correct 18 ms 5624 KB Output is correct
21 Correct 19 ms 5496 KB Output is correct
22 Correct 21 ms 5596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3960 KB Output is correct
2 Correct 5 ms 3960 KB Output is correct
3 Correct 6 ms 3832 KB Output is correct
4 Correct 6 ms 3832 KB Output is correct
5 Correct 5 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 7 ms 3832 KB Output is correct
8 Correct 5 ms 3960 KB Output is correct
9 Correct 5 ms 3960 KB Output is correct
10 Correct 5 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 5 ms 3960 KB Output is correct
13 Correct 5 ms 3832 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 5 ms 3960 KB Output is correct
16 Correct 5 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 5 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 5 ms 3960 KB Output is correct
21 Correct 5 ms 3576 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 5 ms 3832 KB Output is correct
24 Correct 5 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
26 Correct 106 ms 4316 KB Output is correct
27 Correct 22 ms 4344 KB Output is correct
28 Correct 107 ms 4348 KB Output is correct
29 Correct 22 ms 4324 KB Output is correct
30 Correct 106 ms 4216 KB Output is correct
31 Correct 22 ms 4216 KB Output is correct
32 Correct 107 ms 4316 KB Output is correct
33 Correct 22 ms 4216 KB Output is correct
34 Correct 15 ms 4264 KB Output is correct
35 Correct 22 ms 4344 KB Output is correct
36 Correct 108 ms 4336 KB Output is correct
37 Correct 22 ms 4216 KB Output is correct
38 Correct 108 ms 4344 KB Output is correct
39 Correct 22 ms 4344 KB Output is correct
40 Correct 107 ms 4488 KB Output is correct
41 Correct 21 ms 4216 KB Output is correct
42 Correct 108 ms 4344 KB Output is correct
43 Correct 22 ms 4216 KB Output is correct
44 Correct 106 ms 4344 KB Output is correct
45 Correct 22 ms 4444 KB Output is correct
46 Correct 107 ms 4268 KB Output is correct
47 Correct 7 ms 3960 KB Output is correct
48 Correct 58 ms 4216 KB Output is correct
49 Correct 58 ms 4344 KB Output is correct
50 Correct 112 ms 4216 KB Output is correct
51 Correct 111 ms 4332 KB Output is correct
52 Correct 18 ms 5752 KB Output is correct
53 Correct 18 ms 5500 KB Output is correct
54 Correct 21 ms 5624 KB Output is correct
55 Correct 18 ms 5496 KB Output is correct
56 Correct 19 ms 5624 KB Output is correct
57 Correct 19 ms 5496 KB Output is correct
58 Correct 22 ms 5624 KB Output is correct
59 Correct 19 ms 5496 KB Output is correct
60 Correct 18 ms 5624 KB Output is correct
61 Correct 19 ms 5496 KB Output is correct
62 Correct 18 ms 5624 KB Output is correct
63 Correct 19 ms 5496 KB Output is correct
64 Correct 18 ms 5624 KB Output is correct
65 Correct 18 ms 5628 KB Output is correct
66 Correct 19 ms 5496 KB Output is correct
67 Correct 22 ms 5612 KB Output is correct
68 Correct 19 ms 5496 KB Output is correct
69 Correct 18 ms 5624 KB Output is correct
70 Correct 18 ms 5496 KB Output is correct
71 Correct 18 ms 5624 KB Output is correct
72 Correct 19 ms 5496 KB Output is correct
73 Correct 21 ms 5596 KB Output is correct
74 Correct 1563 ms 9848 KB Output is correct
75 Correct 123 ms 9592 KB Output is correct
76 Correct 1555 ms 9720 KB Output is correct
77 Correct 153 ms 9592 KB Output is correct
78 Correct 1542 ms 9720 KB Output is correct
79 Correct 1571 ms 9708 KB Output is correct
80 Correct 124 ms 9592 KB Output is correct
81 Correct 148 ms 9592 KB Output is correct
82 Correct 1602 ms 9576 KB Output is correct
83 Correct 1567 ms 9704 KB Output is correct
84 Correct 118 ms 9592 KB Output is correct
85 Correct 1550 ms 9720 KB Output is correct
86 Correct 115 ms 9592 KB Output is correct
87 Correct 1541 ms 9720 KB Output is correct
88 Correct 121 ms 9592 KB Output is correct
89 Correct 1558 ms 9592 KB Output is correct
90 Correct 137 ms 9596 KB Output is correct
91 Correct 1558 ms 9720 KB Output is correct
92 Correct 112 ms 9592 KB Output is correct
93 Correct 1539 ms 9852 KB Output is correct
94 Correct 80 ms 9340 KB Output is correct
95 Correct 834 ms 9768 KB Output is correct
96 Correct 814 ms 9848 KB Output is correct
97 Correct 1628 ms 9716 KB Output is correct
98 Correct 1645 ms 9744 KB Output is correct