답안 #130066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
130066 2019-07-13T20:31:12 Z nikolapesic2802 Tug of War (BOI15_tug) C++14
100 / 100
2211 ms 10744 KB
#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+5;
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<2*M> 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]=1;
    for(auto p:dp)
        moze=(moze<<p)|(moze>>p);
    bool ima=false;
    for(int i=M;i<=M+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:48: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:50: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]++;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3956 KB Output is correct
2 Correct 6 ms 3960 KB Output is correct
3 Correct 6 ms 3960 KB Output is correct
4 Correct 6 ms 3960 KB Output is correct
5 Correct 6 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 6 ms 3960 KB Output is correct
8 Correct 6 ms 3960 KB Output is correct
9 Correct 6 ms 3964 KB Output is correct
10 Correct 6 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 6 ms 3960 KB Output is correct
13 Correct 6 ms 3960 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 6 ms 3960 KB Output is correct
16 Correct 6 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 6 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 6 ms 3960 KB Output is correct
21 Correct 5 ms 3580 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 6 ms 3960 KB Output is correct
24 Correct 6 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3956 KB Output is correct
2 Correct 6 ms 3960 KB Output is correct
3 Correct 6 ms 3960 KB Output is correct
4 Correct 6 ms 3960 KB Output is correct
5 Correct 6 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 6 ms 3960 KB Output is correct
8 Correct 6 ms 3960 KB Output is correct
9 Correct 6 ms 3964 KB Output is correct
10 Correct 6 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 6 ms 3960 KB Output is correct
13 Correct 6 ms 3960 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 6 ms 3960 KB Output is correct
16 Correct 6 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 6 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 6 ms 3960 KB Output is correct
21 Correct 5 ms 3580 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 6 ms 3960 KB Output is correct
24 Correct 6 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
26 Correct 142 ms 4344 KB Output is correct
27 Correct 28 ms 4344 KB Output is correct
28 Correct 148 ms 4344 KB Output is correct
29 Correct 27 ms 4344 KB Output is correct
30 Correct 143 ms 4344 KB Output is correct
31 Correct 28 ms 4472 KB Output is correct
32 Correct 146 ms 4444 KB Output is correct
33 Correct 27 ms 4344 KB Output is correct
34 Correct 20 ms 4344 KB Output is correct
35 Correct 27 ms 4344 KB Output is correct
36 Correct 166 ms 4344 KB Output is correct
37 Correct 28 ms 4344 KB Output is correct
38 Correct 148 ms 4344 KB Output is correct
39 Correct 27 ms 4344 KB Output is correct
40 Correct 143 ms 4472 KB Output is correct
41 Correct 27 ms 4412 KB Output is correct
42 Correct 145 ms 4344 KB Output is correct
43 Correct 27 ms 4316 KB Output is correct
44 Correct 143 ms 4344 KB Output is correct
45 Correct 27 ms 4344 KB Output is correct
46 Correct 143 ms 4344 KB Output is correct
47 Correct 8 ms 4060 KB Output is correct
48 Correct 76 ms 4600 KB Output is correct
49 Correct 77 ms 4436 KB Output is correct
50 Correct 151 ms 4600 KB Output is correct
51 Correct 152 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 5624 KB Output is correct
2 Correct 18 ms 5496 KB Output is correct
3 Correct 18 ms 5624 KB Output is correct
4 Correct 19 ms 5496 KB Output is correct
5 Correct 18 ms 5624 KB Output is correct
6 Correct 18 ms 5496 KB Output is correct
7 Correct 18 ms 5624 KB Output is correct
8 Correct 19 ms 5496 KB Output is correct
9 Correct 18 ms 5752 KB Output is correct
10 Correct 20 ms 5496 KB Output is correct
11 Correct 18 ms 5624 KB Output is correct
12 Correct 20 ms 5624 KB Output is correct
13 Correct 18 ms 5624 KB Output is correct
14 Correct 20 ms 5596 KB Output is correct
15 Correct 19 ms 5500 KB Output is correct
16 Correct 20 ms 5624 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 20 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3956 KB Output is correct
2 Correct 6 ms 3960 KB Output is correct
3 Correct 6 ms 3960 KB Output is correct
4 Correct 6 ms 3960 KB Output is correct
5 Correct 6 ms 3960 KB Output is correct
6 Correct 6 ms 3960 KB Output is correct
7 Correct 6 ms 3960 KB Output is correct
8 Correct 6 ms 3960 KB Output is correct
9 Correct 6 ms 3964 KB Output is correct
10 Correct 6 ms 3960 KB Output is correct
11 Correct 6 ms 3960 KB Output is correct
12 Correct 6 ms 3960 KB Output is correct
13 Correct 6 ms 3960 KB Output is correct
14 Correct 6 ms 3960 KB Output is correct
15 Correct 6 ms 3960 KB Output is correct
16 Correct 6 ms 3960 KB Output is correct
17 Correct 6 ms 3960 KB Output is correct
18 Correct 6 ms 3960 KB Output is correct
19 Correct 6 ms 3960 KB Output is correct
20 Correct 6 ms 3960 KB Output is correct
21 Correct 5 ms 3580 KB Output is correct
22 Correct 5 ms 3960 KB Output is correct
23 Correct 6 ms 3960 KB Output is correct
24 Correct 6 ms 3960 KB Output is correct
25 Correct 6 ms 3960 KB Output is correct
26 Correct 142 ms 4344 KB Output is correct
27 Correct 28 ms 4344 KB Output is correct
28 Correct 148 ms 4344 KB Output is correct
29 Correct 27 ms 4344 KB Output is correct
30 Correct 143 ms 4344 KB Output is correct
31 Correct 28 ms 4472 KB Output is correct
32 Correct 146 ms 4444 KB Output is correct
33 Correct 27 ms 4344 KB Output is correct
34 Correct 20 ms 4344 KB Output is correct
35 Correct 27 ms 4344 KB Output is correct
36 Correct 166 ms 4344 KB Output is correct
37 Correct 28 ms 4344 KB Output is correct
38 Correct 148 ms 4344 KB Output is correct
39 Correct 27 ms 4344 KB Output is correct
40 Correct 143 ms 4472 KB Output is correct
41 Correct 27 ms 4412 KB Output is correct
42 Correct 145 ms 4344 KB Output is correct
43 Correct 27 ms 4316 KB Output is correct
44 Correct 143 ms 4344 KB Output is correct
45 Correct 27 ms 4344 KB Output is correct
46 Correct 143 ms 4344 KB Output is correct
47 Correct 8 ms 4060 KB Output is correct
48 Correct 76 ms 4600 KB Output is correct
49 Correct 77 ms 4436 KB Output is correct
50 Correct 151 ms 4600 KB Output is correct
51 Correct 152 ms 4444 KB Output is correct
52 Correct 18 ms 5624 KB Output is correct
53 Correct 18 ms 5496 KB Output is correct
54 Correct 18 ms 5624 KB Output is correct
55 Correct 19 ms 5496 KB Output is correct
56 Correct 18 ms 5624 KB Output is correct
57 Correct 18 ms 5496 KB Output is correct
58 Correct 18 ms 5624 KB Output is correct
59 Correct 19 ms 5496 KB Output is correct
60 Correct 18 ms 5752 KB Output is correct
61 Correct 20 ms 5496 KB Output is correct
62 Correct 18 ms 5624 KB Output is correct
63 Correct 20 ms 5624 KB Output is correct
64 Correct 18 ms 5624 KB Output is correct
65 Correct 20 ms 5596 KB Output is correct
66 Correct 19 ms 5500 KB Output is correct
67 Correct 20 ms 5624 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 20 ms 5624 KB Output is correct
74 Correct 2111 ms 10616 KB Output is correct
75 Correct 143 ms 10488 KB Output is correct
76 Correct 2101 ms 10644 KB Output is correct
77 Correct 141 ms 10488 KB Output is correct
78 Correct 2101 ms 10616 KB Output is correct
79 Correct 2131 ms 10744 KB Output is correct
80 Correct 138 ms 10488 KB Output is correct
81 Correct 144 ms 10488 KB Output is correct
82 Correct 2177 ms 10488 KB Output is correct
83 Correct 2080 ms 10616 KB Output is correct
84 Correct 138 ms 10616 KB Output is correct
85 Correct 2094 ms 10492 KB Output is correct
86 Correct 140 ms 10488 KB Output is correct
87 Correct 2087 ms 10528 KB Output is correct
88 Correct 143 ms 10488 KB Output is correct
89 Correct 2106 ms 10612 KB Output is correct
90 Correct 169 ms 10488 KB Output is correct
91 Correct 2091 ms 10616 KB Output is correct
92 Correct 130 ms 10488 KB Output is correct
93 Correct 2070 ms 10612 KB Output is correct
94 Correct 59 ms 10104 KB Output is correct
95 Correct 1100 ms 10688 KB Output is correct
96 Correct 1091 ms 10744 KB Output is correct
97 Correct 2207 ms 10616 KB Output is correct
98 Correct 2211 ms 10616 KB Output is correct