Submission #30937

# Submission time Handle Problem Language Result Execution time Memory
30937 2017-08-01T09:36:10 Z Navick Tug of War (BOI15_tug) C++14
100 / 100
209 ms 18320 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 3e4 + 10, K = 20;

int n, k, deg[2 * N], suml, sumr, sum, p[4 * N];
vector<int> adj[4 * N];
bool mark[4 * N];
set<pii> s;
pii vl[4 * N];
bitset<2 * N * K> dp;
int d[N], num[N * K], kol[N * K];

void solve(int m){
	for(int i=0; i<m; i++)
		num[d[i]]++;// cout << d[i] << " KI F" << endl;
	
	int sz = 0;
	for(int i=1; i<N*K; i++){
		if(num[i] == 0)continue;
		int pw = 1;
		while(pw*2 <= num[i]){
			kol[sz++] = pw * i;
			pw *= 2;
		}
		pw --;
		kol[sz++] = i *(num[i] - pw);
	}

	dp[0] = 1;
	for(int i=0; i<sz; i++)
		dp |= dp << kol[i];

}


inline int pe(int i){
	return 2 * n + i;
}

inline int rg(int i){
	return i + n;
}

inline int lf(int i){
	return i;
}

void dfs(int v, int c, int h = 0){
	mark[v] = true;
	if(v>=2*n){
		if(h % 4 == 1)
			vl[c].F += p[v];
		else
			vl[c].S += p[v];
	}
	for(auto u : adj[v]){
		if(mark[u])continue;
		dfs(u, c, h + 1);
	}
}

int main(){
	scanf("%d %d", &n, &k);
	for(int i=0; i<2*n; i++){
		int l, r; scanf("%d %d", &l, &r);
		--l; --r;

		scanf("%d", &p[pe(i)]); sum += p[pe(i)];

		adj[pe(i)].pb(lf(l));
		adj[pe(i)].pb(rg(r));
		adj[rg(r)].pb(pe(i));
		adj[lf(l)].pb(pe(i));
	}

	for(int i=0; i<2*n; i++){
		deg[i] = (int)adj[i].size();
		s.insert({deg[i], i});
	}

	while(!s.empty()){
		int v = (*s.begin()).S;
		s.erase(s.begin());

		if(deg[v] <= 0)
			return printf("NO\n"), 0;

		if(deg[v] == 1){
			mark[v] = true;
			for(auto u : adj[v]){
				if(mark[u])continue;
				mark[u] = true;
				for(auto x : adj[u]){
					if(mark[x])continue;
					s.erase({deg[x], x});
					deg[x]--;
					s.insert({deg[x], x});
				}
				if(v < n)
					suml += p[u];
				else sumr += p[u];
			}
		}
		else break;
	}

	int cnt = 0, mn = 0;

	for(int i=0; i<2*n; i++){
		if(mark[i])continue;
		dfs(i, cnt);
		if(vl[cnt].F > vl[cnt].S)
			swap(vl[cnt].F, vl[cnt].S);
		mn += vl[cnt].F;
		d[cnt] = vl[cnt].S - vl[cnt].F;
		cnt++;
	
	}

	mn += suml;
	solve(cnt);
	
	for(int i=0; i<N*K - mn; i++){
		if(i + mn >= (sum-k+1)/2 && i+mn <= (sum+k)/2)
			if(dp[i])return printf("YES\n"), 0;
	}
	return printf("NO\n"), 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:72:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
                        ^
tug.cpp:74:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int l, r; scanf("%d %d", &l, &r);
                                   ^
tug.cpp:77:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &p[pe(i)]); sum += p[pe(i)];
                         ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 11716 KB Output is correct
2 Correct 0 ms 11716 KB Output is correct
3 Correct 0 ms 11716 KB Output is correct
4 Correct 3 ms 11712 KB Output is correct
5 Correct 3 ms 11716 KB Output is correct
6 Correct 0 ms 11716 KB Output is correct
7 Correct 0 ms 11716 KB Output is correct
8 Correct 3 ms 11712 KB Output is correct
9 Correct 3 ms 11712 KB Output is correct
10 Correct 0 ms 11720 KB Output is correct
11 Correct 3 ms 11720 KB Output is correct
12 Correct 0 ms 11716 KB Output is correct
13 Correct 3 ms 11716 KB Output is correct
14 Correct 3 ms 11720 KB Output is correct
15 Correct 0 ms 11712 KB Output is correct
16 Correct 3 ms 11716 KB Output is correct
17 Correct 0 ms 11712 KB Output is correct
18 Correct 0 ms 11720 KB Output is correct
19 Correct 3 ms 11716 KB Output is correct
20 Correct 3 ms 11712 KB Output is correct
21 Correct 0 ms 11548 KB Output is correct
22 Correct 3 ms 11716 KB Output is correct
23 Correct 3 ms 11712 KB Output is correct
24 Correct 0 ms 11720 KB Output is correct
25 Correct 0 ms 11716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 11716 KB Output is correct
2 Correct 0 ms 11716 KB Output is correct
3 Correct 0 ms 11716 KB Output is correct
4 Correct 3 ms 11712 KB Output is correct
5 Correct 3 ms 11716 KB Output is correct
6 Correct 0 ms 11716 KB Output is correct
7 Correct 0 ms 11716 KB Output is correct
8 Correct 3 ms 11712 KB Output is correct
9 Correct 3 ms 11712 KB Output is correct
10 Correct 0 ms 11720 KB Output is correct
11 Correct 3 ms 11720 KB Output is correct
12 Correct 0 ms 11716 KB Output is correct
13 Correct 3 ms 11716 KB Output is correct
14 Correct 3 ms 11720 KB Output is correct
15 Correct 0 ms 11712 KB Output is correct
16 Correct 3 ms 11716 KB Output is correct
17 Correct 0 ms 11712 KB Output is correct
18 Correct 0 ms 11720 KB Output is correct
19 Correct 3 ms 11716 KB Output is correct
20 Correct 3 ms 11712 KB Output is correct
21 Correct 0 ms 11548 KB Output is correct
22 Correct 3 ms 11716 KB Output is correct
23 Correct 3 ms 11712 KB Output is correct
24 Correct 0 ms 11720 KB Output is correct
25 Correct 0 ms 11716 KB Output is correct
26 Correct 13 ms 12112 KB Output is correct
27 Correct 23 ms 12112 KB Output is correct
28 Correct 19 ms 12112 KB Output is correct
29 Correct 26 ms 12116 KB Output is correct
30 Correct 16 ms 12112 KB Output is correct
31 Correct 23 ms 12112 KB Output is correct
32 Correct 16 ms 12112 KB Output is correct
33 Correct 26 ms 12108 KB Output is correct
34 Correct 9 ms 12116 KB Output is correct
35 Correct 29 ms 12116 KB Output is correct
36 Correct 13 ms 12112 KB Output is correct
37 Correct 29 ms 12112 KB Output is correct
38 Correct 13 ms 12112 KB Output is correct
39 Correct 29 ms 12108 KB Output is correct
40 Correct 16 ms 12108 KB Output is correct
41 Correct 29 ms 12116 KB Output is correct
42 Correct 13 ms 12112 KB Output is correct
43 Correct 23 ms 12108 KB Output is correct
44 Correct 13 ms 12116 KB Output is correct
45 Correct 26 ms 12108 KB Output is correct
46 Correct 13 ms 12116 KB Output is correct
47 Correct 6 ms 11944 KB Output is correct
48 Correct 13 ms 12112 KB Output is correct
49 Correct 16 ms 12112 KB Output is correct
50 Correct 3 ms 12112 KB Output is correct
51 Correct 9 ms 12112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 13792 KB Output is correct
2 Correct 19 ms 13792 KB Output is correct
3 Correct 19 ms 13792 KB Output is correct
4 Correct 26 ms 13792 KB Output is correct
5 Correct 19 ms 13792 KB Output is correct
6 Correct 16 ms 13792 KB Output is correct
7 Correct 33 ms 13792 KB Output is correct
8 Correct 19 ms 13792 KB Output is correct
9 Correct 16 ms 13792 KB Output is correct
10 Correct 23 ms 13792 KB Output is correct
11 Correct 26 ms 13792 KB Output is correct
12 Correct 16 ms 13792 KB Output is correct
13 Correct 19 ms 13792 KB Output is correct
14 Correct 19 ms 13792 KB Output is correct
15 Correct 23 ms 13792 KB Output is correct
16 Correct 23 ms 13792 KB Output is correct
17 Correct 26 ms 13792 KB Output is correct
18 Correct 23 ms 13792 KB Output is correct
19 Correct 19 ms 13792 KB Output is correct
20 Correct 23 ms 13792 KB Output is correct
21 Correct 23 ms 13660 KB Output is correct
22 Correct 33 ms 13792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 11716 KB Output is correct
2 Correct 0 ms 11716 KB Output is correct
3 Correct 0 ms 11716 KB Output is correct
4 Correct 3 ms 11712 KB Output is correct
5 Correct 3 ms 11716 KB Output is correct
6 Correct 0 ms 11716 KB Output is correct
7 Correct 0 ms 11716 KB Output is correct
8 Correct 3 ms 11712 KB Output is correct
9 Correct 3 ms 11712 KB Output is correct
10 Correct 0 ms 11720 KB Output is correct
11 Correct 3 ms 11720 KB Output is correct
12 Correct 0 ms 11716 KB Output is correct
13 Correct 3 ms 11716 KB Output is correct
14 Correct 3 ms 11720 KB Output is correct
15 Correct 0 ms 11712 KB Output is correct
16 Correct 3 ms 11716 KB Output is correct
17 Correct 0 ms 11712 KB Output is correct
18 Correct 0 ms 11720 KB Output is correct
19 Correct 3 ms 11716 KB Output is correct
20 Correct 3 ms 11712 KB Output is correct
21 Correct 0 ms 11548 KB Output is correct
22 Correct 3 ms 11716 KB Output is correct
23 Correct 3 ms 11712 KB Output is correct
24 Correct 0 ms 11720 KB Output is correct
25 Correct 0 ms 11716 KB Output is correct
26 Correct 13 ms 12112 KB Output is correct
27 Correct 23 ms 12112 KB Output is correct
28 Correct 19 ms 12112 KB Output is correct
29 Correct 26 ms 12116 KB Output is correct
30 Correct 16 ms 12112 KB Output is correct
31 Correct 23 ms 12112 KB Output is correct
32 Correct 16 ms 12112 KB Output is correct
33 Correct 26 ms 12108 KB Output is correct
34 Correct 9 ms 12116 KB Output is correct
35 Correct 29 ms 12116 KB Output is correct
36 Correct 13 ms 12112 KB Output is correct
37 Correct 29 ms 12112 KB Output is correct
38 Correct 13 ms 12112 KB Output is correct
39 Correct 29 ms 12108 KB Output is correct
40 Correct 16 ms 12108 KB Output is correct
41 Correct 29 ms 12116 KB Output is correct
42 Correct 13 ms 12112 KB Output is correct
43 Correct 23 ms 12108 KB Output is correct
44 Correct 13 ms 12116 KB Output is correct
45 Correct 26 ms 12108 KB Output is correct
46 Correct 13 ms 12116 KB Output is correct
47 Correct 6 ms 11944 KB Output is correct
48 Correct 13 ms 12112 KB Output is correct
49 Correct 16 ms 12112 KB Output is correct
50 Correct 3 ms 12112 KB Output is correct
51 Correct 9 ms 12112 KB Output is correct
52 Correct 19 ms 13792 KB Output is correct
53 Correct 19 ms 13792 KB Output is correct
54 Correct 19 ms 13792 KB Output is correct
55 Correct 26 ms 13792 KB Output is correct
56 Correct 19 ms 13792 KB Output is correct
57 Correct 16 ms 13792 KB Output is correct
58 Correct 33 ms 13792 KB Output is correct
59 Correct 19 ms 13792 KB Output is correct
60 Correct 16 ms 13792 KB Output is correct
61 Correct 23 ms 13792 KB Output is correct
62 Correct 26 ms 13792 KB Output is correct
63 Correct 16 ms 13792 KB Output is correct
64 Correct 19 ms 13792 KB Output is correct
65 Correct 19 ms 13792 KB Output is correct
66 Correct 23 ms 13792 KB Output is correct
67 Correct 23 ms 13792 KB Output is correct
68 Correct 26 ms 13792 KB Output is correct
69 Correct 23 ms 13792 KB Output is correct
70 Correct 19 ms 13792 KB Output is correct
71 Correct 23 ms 13792 KB Output is correct
72 Correct 23 ms 13660 KB Output is correct
73 Correct 33 ms 13792 KB Output is correct
74 Correct 113 ms 18316 KB Output is correct
75 Correct 206 ms 18320 KB Output is correct
76 Correct 113 ms 18316 KB Output is correct
77 Correct 193 ms 18316 KB Output is correct
78 Correct 109 ms 18312 KB Output is correct
79 Correct 99 ms 18316 KB Output is correct
80 Correct 199 ms 18316 KB Output is correct
81 Correct 129 ms 18316 KB Output is correct
82 Correct 59 ms 18316 KB Output is correct
83 Correct 109 ms 18316 KB Output is correct
84 Correct 209 ms 18316 KB Output is correct
85 Correct 129 ms 18320 KB Output is correct
86 Correct 176 ms 18316 KB Output is correct
87 Correct 99 ms 18316 KB Output is correct
88 Correct 209 ms 18320 KB Output is correct
89 Correct 113 ms 18316 KB Output is correct
90 Correct 196 ms 18312 KB Output is correct
91 Correct 119 ms 18316 KB Output is correct
92 Correct 199 ms 18316 KB Output is correct
93 Correct 116 ms 18312 KB Output is correct
94 Correct 73 ms 18016 KB Output is correct
95 Correct 143 ms 18316 KB Output is correct
96 Correct 139 ms 18316 KB Output is correct
97 Correct 106 ms 18320 KB Output is correct
98 Correct 99 ms 18316 KB Output is correct