답안 #94068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94068 2019-01-15T15:45:02 Z updown1 Tug of War (BOI15_tug) C++17
100 / 100
1148 ms 10232 KB
/*
nodes are spots (0-N-1:left, N - 2*N-1: right)
edges are people
weight of edge is strength
if there is only one edge in a node, remove and repeat
we are not left with cycles so all edges should have exactly two edges in it
find the possibility for each cycle
use bitset for quick dp
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, 2*N)
#define ffj For(j, 0, M)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e "\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
const int MAXN=60000, INF=1000000000;
///500,000,000
int N, K, s1, s2, tot;
bool vis[MAXN];
vector<int> vals;
multiset<pair<int, int> > adj[MAXN];
multiset<pair<int, int> >::iterator it;
queue<int> emp;
bitset<10*MAXN+1> dp; /// 10*MAXN bc MAXN = 2*30,000

void go(int at, int diff, int put) {
    if (vis[at]) {vals.pb(abs(diff)); return;}
    vis[at] = true;
    it = adj[at].begin();
    int x = (*it).a, d = (*it).b;
    if (put == 0) diff += d;
    else diff -= d;
    adj[x].erase(adj[x].find(mp(at, d)));
    adj[at].erase(it);
    go(x, diff, 1-put);
}

main() {
    //ifstream cin("test.in");
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	ffi {
        int a, b, d; cin >> a >> b >> d;
        a--; b = b-1+N;
        adj[a].insert(mp(b, d)); adj[b].insert(mp(a, d));
        //w<< a s b<<e;
        tot += d;
	}
	if (K == 0 && tot%2 != 0) {
        w<< "NO"<<e;
        exit(0);
	}
	ffi if (adj[i].size() == 1) emp.push(i);
	while (!emp.empty()) {
        int x = emp.front(); emp.pop();
        if (adj[x].empty()) {
            w<< "NO"<<e;
            return 0;
        }
        it = adj[x].begin();
        if (x < N) s1 += (*it).b;
        else s2 += (*it).b;
        adj[(*it).a].erase(adj[(*it).a].find(mp(x, (*it).b)));
        if (adj[(*it).a].size() == 1) emp.push((*it).a);
        adj[x].erase(it);
	}
	ffi if (adj[i].size() != 2 && adj[i].size() != 0) {
        w<< "NO"<<e;
        exit(0);
	}
	ffi if (!vis[i] && adj[i].size() == 2) {
        go(i, 0, 0);
	}
	tot = 0;
	//for (int i: vals) w<< i<< " "; w<<e;
	dp[abs(s1-s2)] = 1; /// check if this is correct
	for (int i: vals) {
        tot += i;
        dp |= dp << (2*i);
	}
	For (i, 0, 10*MAXN+1) if (dp[i] == 1) {
	    //w<< i s tot<<e;
        if (abs(tot-i) <= K) {
            w<< "YES"<<e;
            return 0;
        }
	}
	w<< "NO"<<e;
}

Compilation message

tug.cpp:48:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3192 KB Output is correct
2 Correct 4 ms 3448 KB Output is correct
3 Correct 4 ms 3192 KB Output is correct
4 Correct 4 ms 3320 KB Output is correct
5 Correct 4 ms 3192 KB Output is correct
6 Correct 4 ms 3192 KB Output is correct
7 Correct 4 ms 3320 KB Output is correct
8 Correct 4 ms 3448 KB Output is correct
9 Correct 4 ms 3496 KB Output is correct
10 Correct 4 ms 3192 KB Output is correct
11 Correct 4 ms 3448 KB Output is correct
12 Correct 4 ms 3320 KB Output is correct
13 Correct 4 ms 3320 KB Output is correct
14 Correct 5 ms 3320 KB Output is correct
15 Correct 4 ms 3448 KB Output is correct
16 Correct 4 ms 3192 KB Output is correct
17 Correct 4 ms 3320 KB Output is correct
18 Correct 4 ms 3448 KB Output is correct
19 Correct 4 ms 3316 KB Output is correct
20 Correct 4 ms 3320 KB Output is correct
21 Correct 4 ms 3192 KB Output is correct
22 Correct 4 ms 3448 KB Output is correct
23 Correct 5 ms 3448 KB Output is correct
24 Correct 4 ms 3448 KB Output is correct
25 Correct 4 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3192 KB Output is correct
2 Correct 4 ms 3448 KB Output is correct
3 Correct 4 ms 3192 KB Output is correct
4 Correct 4 ms 3320 KB Output is correct
5 Correct 4 ms 3192 KB Output is correct
6 Correct 4 ms 3192 KB Output is correct
7 Correct 4 ms 3320 KB Output is correct
8 Correct 4 ms 3448 KB Output is correct
9 Correct 4 ms 3496 KB Output is correct
10 Correct 4 ms 3192 KB Output is correct
11 Correct 4 ms 3448 KB Output is correct
12 Correct 4 ms 3320 KB Output is correct
13 Correct 4 ms 3320 KB Output is correct
14 Correct 5 ms 3320 KB Output is correct
15 Correct 4 ms 3448 KB Output is correct
16 Correct 4 ms 3192 KB Output is correct
17 Correct 4 ms 3320 KB Output is correct
18 Correct 4 ms 3448 KB Output is correct
19 Correct 4 ms 3316 KB Output is correct
20 Correct 4 ms 3320 KB Output is correct
21 Correct 4 ms 3192 KB Output is correct
22 Correct 4 ms 3448 KB Output is correct
23 Correct 5 ms 3448 KB Output is correct
24 Correct 4 ms 3448 KB Output is correct
25 Correct 4 ms 3192 KB Output is correct
26 Correct 62 ms 3832 KB Output is correct
27 Correct 6 ms 3576 KB Output is correct
28 Correct 64 ms 3832 KB Output is correct
29 Correct 6 ms 3576 KB Output is correct
30 Correct 5 ms 3576 KB Output is correct
31 Correct 6 ms 3576 KB Output is correct
32 Correct 65 ms 3812 KB Output is correct
33 Correct 6 ms 3576 KB Output is correct
34 Correct 11 ms 3832 KB Output is correct
35 Correct 6 ms 3576 KB Output is correct
36 Correct 59 ms 3860 KB Output is correct
37 Correct 6 ms 3576 KB Output is correct
38 Correct 6 ms 3576 KB Output is correct
39 Correct 6 ms 3576 KB Output is correct
40 Correct 64 ms 3832 KB Output is correct
41 Correct 6 ms 3576 KB Output is correct
42 Correct 6 ms 3576 KB Output is correct
43 Correct 6 ms 3576 KB Output is correct
44 Correct 62 ms 3864 KB Output is correct
45 Correct 6 ms 3576 KB Output is correct
46 Correct 62 ms 3832 KB Output is correct
47 Correct 6 ms 3576 KB Output is correct
48 Correct 34 ms 3960 KB Output is correct
49 Correct 37 ms 3832 KB Output is correct
50 Correct 63 ms 3868 KB Output is correct
51 Correct 6 ms 3704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 5624 KB Output is correct
2 Correct 15 ms 5240 KB Output is correct
3 Correct 178 ms 5664 KB Output is correct
4 Correct 15 ms 5240 KB Output is correct
5 Correct 171 ms 5724 KB Output is correct
6 Correct 14 ms 5240 KB Output is correct
7 Correct 170 ms 5628 KB Output is correct
8 Correct 16 ms 5244 KB Output is correct
9 Correct 171 ms 5596 KB Output is correct
10 Correct 15 ms 5368 KB Output is correct
11 Correct 172 ms 5752 KB Output is correct
12 Correct 15 ms 5240 KB Output is correct
13 Correct 174 ms 5624 KB Output is correct
14 Correct 174 ms 5624 KB Output is correct
15 Correct 16 ms 5368 KB Output is correct
16 Correct 171 ms 5624 KB Output is correct
17 Correct 17 ms 5240 KB Output is correct
18 Correct 178 ms 5624 KB Output is correct
19 Correct 16 ms 5368 KB Output is correct
20 Correct 167 ms 5668 KB Output is correct
21 Correct 15 ms 5372 KB Output is correct
22 Correct 94 ms 5700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3192 KB Output is correct
2 Correct 4 ms 3448 KB Output is correct
3 Correct 4 ms 3192 KB Output is correct
4 Correct 4 ms 3320 KB Output is correct
5 Correct 4 ms 3192 KB Output is correct
6 Correct 4 ms 3192 KB Output is correct
7 Correct 4 ms 3320 KB Output is correct
8 Correct 4 ms 3448 KB Output is correct
9 Correct 4 ms 3496 KB Output is correct
10 Correct 4 ms 3192 KB Output is correct
11 Correct 4 ms 3448 KB Output is correct
12 Correct 4 ms 3320 KB Output is correct
13 Correct 4 ms 3320 KB Output is correct
14 Correct 5 ms 3320 KB Output is correct
15 Correct 4 ms 3448 KB Output is correct
16 Correct 4 ms 3192 KB Output is correct
17 Correct 4 ms 3320 KB Output is correct
18 Correct 4 ms 3448 KB Output is correct
19 Correct 4 ms 3316 KB Output is correct
20 Correct 4 ms 3320 KB Output is correct
21 Correct 4 ms 3192 KB Output is correct
22 Correct 4 ms 3448 KB Output is correct
23 Correct 5 ms 3448 KB Output is correct
24 Correct 4 ms 3448 KB Output is correct
25 Correct 4 ms 3192 KB Output is correct
26 Correct 62 ms 3832 KB Output is correct
27 Correct 6 ms 3576 KB Output is correct
28 Correct 64 ms 3832 KB Output is correct
29 Correct 6 ms 3576 KB Output is correct
30 Correct 5 ms 3576 KB Output is correct
31 Correct 6 ms 3576 KB Output is correct
32 Correct 65 ms 3812 KB Output is correct
33 Correct 6 ms 3576 KB Output is correct
34 Correct 11 ms 3832 KB Output is correct
35 Correct 6 ms 3576 KB Output is correct
36 Correct 59 ms 3860 KB Output is correct
37 Correct 6 ms 3576 KB Output is correct
38 Correct 6 ms 3576 KB Output is correct
39 Correct 6 ms 3576 KB Output is correct
40 Correct 64 ms 3832 KB Output is correct
41 Correct 6 ms 3576 KB Output is correct
42 Correct 6 ms 3576 KB Output is correct
43 Correct 6 ms 3576 KB Output is correct
44 Correct 62 ms 3864 KB Output is correct
45 Correct 6 ms 3576 KB Output is correct
46 Correct 62 ms 3832 KB Output is correct
47 Correct 6 ms 3576 KB Output is correct
48 Correct 34 ms 3960 KB Output is correct
49 Correct 37 ms 3832 KB Output is correct
50 Correct 63 ms 3868 KB Output is correct
51 Correct 6 ms 3704 KB Output is correct
52 Correct 168 ms 5624 KB Output is correct
53 Correct 15 ms 5240 KB Output is correct
54 Correct 178 ms 5664 KB Output is correct
55 Correct 15 ms 5240 KB Output is correct
56 Correct 171 ms 5724 KB Output is correct
57 Correct 14 ms 5240 KB Output is correct
58 Correct 170 ms 5628 KB Output is correct
59 Correct 16 ms 5244 KB Output is correct
60 Correct 171 ms 5596 KB Output is correct
61 Correct 15 ms 5368 KB Output is correct
62 Correct 172 ms 5752 KB Output is correct
63 Correct 15 ms 5240 KB Output is correct
64 Correct 174 ms 5624 KB Output is correct
65 Correct 174 ms 5624 KB Output is correct
66 Correct 16 ms 5368 KB Output is correct
67 Correct 171 ms 5624 KB Output is correct
68 Correct 17 ms 5240 KB Output is correct
69 Correct 178 ms 5624 KB Output is correct
70 Correct 16 ms 5368 KB Output is correct
71 Correct 167 ms 5668 KB Output is correct
72 Correct 15 ms 5372 KB Output is correct
73 Correct 94 ms 5700 KB Output is correct
74 Correct 35 ms 9592 KB Output is correct
75 Correct 36 ms 9592 KB Output is correct
76 Correct 38 ms 9668 KB Output is correct
77 Correct 42 ms 9592 KB Output is correct
78 Correct 905 ms 10232 KB Output is correct
79 Correct 866 ms 10104 KB Output is correct
80 Correct 38 ms 9592 KB Output is correct
81 Correct 80 ms 9820 KB Output is correct
82 Correct 867 ms 9976 KB Output is correct
83 Correct 41 ms 9724 KB Output is correct
84 Correct 40 ms 9592 KB Output is correct
85 Correct 36 ms 9592 KB Output is correct
86 Correct 60 ms 9592 KB Output is correct
87 Correct 1148 ms 10104 KB Output is correct
88 Correct 53 ms 9592 KB Output is correct
89 Correct 848 ms 10136 KB Output is correct
90 Correct 46 ms 9720 KB Output is correct
91 Correct 117 ms 9592 KB Output is correct
92 Correct 44 ms 9592 KB Output is correct
93 Correct 908 ms 10104 KB Output is correct
94 Correct 43 ms 9720 KB Output is correct
95 Correct 453 ms 10104 KB Output is correct
96 Correct 475 ms 9976 KB Output is correct
97 Correct 865 ms 10104 KB Output is correct
98 Correct 47 ms 9592 KB Output is correct