Submission #198461

# Submission time Handle Problem Language Result Execution time Memory
198461 2020-01-26T06:32:30 Z dndhk Tug of War (BOI15_tug) C++14
100 / 100
2397 ms 5780 KB
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

const int MAX_N = 60000;

typedef pair<int, int> pii;

int N, K;
bool fin = false;
int c[MAX_N+1];
vector<pii> gp[MAX_N+1];

bool chk[MAX_N+1];
bool vst[MAX_N+1];

int S;
vector<int> vt;
vector<pii> v;

bitset<MAX_N*20+1> bt;

bool dfs(int x){
	vst[x] = true;
	for(pii i : gp[x]){
		if(chk[i.second])	continue;
		chk[i.second] = true;
		if(vst[i.first]){
			int sum = 0;
			if(x>N) sum+=c[i.second];
			else sum-=c[i.second];
			while(!v.empty()){
				if(v.back().first>N){
					sum+=v.back().second;
				}else{
					sum-=v.back().second;
				}
				if(v.back().first==i.first){
					vt.pb((sum>0?sum:-sum));
					sum = 0;
				}
				v.pop_back();
			}
			S+=sum;
			return true;
		}else{
			v.pb({x, c[i.second]});
			if(dfs(i.first)){
				return true;
			}
		}
	}
	if(!v.empty()){
		if(x>N){
			S += v.back().second;
		}else{
			S -= v.back().second;
		}
		v.pop_back();
		return false;
	}
	fin = true;
	return true;
}

int main(){
	scanf("%d%d", &N, &K);
	//cout<<N<<" "<<K<<endl;
	for(int i=1; i<=N*2; i++){
		int l, r;
		scanf("%d%d%d", &l, &r, &c[i]);
		//cout<<l<<" "<<r<<endl;
		r+=N;
		gp[l].pb({r, i});
		gp[r].pb({l, i});
	}
	for(int i=1; i<=N*2; i++){
		if(!vst[i]){
			dfs(i);
			if(fin==true){
				printf("NO");
				return 0;
			}
		}
	}
	if(S<0)	S*=-1;
	int S2 = 0;
	for(int i : vt)	S2 += i;
	// for(int i : vt){
	// 	cout<<i<<endl;
	// }
	bt[0] = true;
	int s = (S2-K-S+1)/2, e = (S2-S+K)/2;
	if(S2-S+K<0){
		printf("NO");
		return 0;
	}
	//cout<<S<<" "<<S2<<" "<<K<<endl;
	s = max(s, 0);
	for(int i : vt){
		bt = (bt | (bt<<i));
	}
	for(int j=s; j<=e; j++){
		//cout<<j<<" "<<bt[j]<<endl;
		if(bt[j]){
			printf("YES");
			return 0;
		}
	}
	printf("NO");
	return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
tug.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &l, &r, &c[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2296 KB Output is correct
2 Correct 7 ms 2296 KB Output is correct
3 Correct 7 ms 2296 KB Output is correct
4 Correct 7 ms 2296 KB Output is correct
5 Correct 8 ms 2300 KB Output is correct
6 Correct 7 ms 2296 KB Output is correct
7 Correct 7 ms 2296 KB Output is correct
8 Correct 7 ms 2296 KB Output is correct
9 Correct 7 ms 2300 KB Output is correct
10 Correct 7 ms 2296 KB Output is correct
11 Correct 7 ms 2296 KB Output is correct
12 Correct 8 ms 2424 KB Output is correct
13 Correct 7 ms 2296 KB Output is correct
14 Correct 7 ms 2296 KB Output is correct
15 Correct 7 ms 2296 KB Output is correct
16 Correct 7 ms 2296 KB Output is correct
17 Correct 7 ms 2296 KB Output is correct
18 Correct 7 ms 2296 KB Output is correct
19 Correct 8 ms 2296 KB Output is correct
20 Correct 7 ms 2296 KB Output is correct
21 Correct 6 ms 1784 KB Output is correct
22 Correct 7 ms 2296 KB Output is correct
23 Correct 7 ms 2424 KB Output is correct
24 Correct 7 ms 2296 KB Output is correct
25 Correct 8 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2296 KB Output is correct
2 Correct 7 ms 2296 KB Output is correct
3 Correct 7 ms 2296 KB Output is correct
4 Correct 7 ms 2296 KB Output is correct
5 Correct 8 ms 2300 KB Output is correct
6 Correct 7 ms 2296 KB Output is correct
7 Correct 7 ms 2296 KB Output is correct
8 Correct 7 ms 2296 KB Output is correct
9 Correct 7 ms 2300 KB Output is correct
10 Correct 7 ms 2296 KB Output is correct
11 Correct 7 ms 2296 KB Output is correct
12 Correct 8 ms 2424 KB Output is correct
13 Correct 7 ms 2296 KB Output is correct
14 Correct 7 ms 2296 KB Output is correct
15 Correct 7 ms 2296 KB Output is correct
16 Correct 7 ms 2296 KB Output is correct
17 Correct 7 ms 2296 KB Output is correct
18 Correct 7 ms 2296 KB Output is correct
19 Correct 8 ms 2296 KB Output is correct
20 Correct 7 ms 2296 KB Output is correct
21 Correct 6 ms 1784 KB Output is correct
22 Correct 7 ms 2296 KB Output is correct
23 Correct 7 ms 2424 KB Output is correct
24 Correct 7 ms 2296 KB Output is correct
25 Correct 8 ms 2296 KB Output is correct
26 Correct 154 ms 2424 KB Output is correct
27 Correct 28 ms 2424 KB Output is correct
28 Correct 150 ms 2424 KB Output is correct
29 Correct 29 ms 2424 KB Output is correct
30 Correct 156 ms 2552 KB Output is correct
31 Correct 27 ms 2424 KB Output is correct
32 Correct 155 ms 2424 KB Output is correct
33 Correct 28 ms 2552 KB Output is correct
34 Correct 18 ms 2424 KB Output is correct
35 Correct 30 ms 2424 KB Output is correct
36 Correct 158 ms 2536 KB Output is correct
37 Correct 28 ms 2424 KB Output is correct
38 Correct 153 ms 2540 KB Output is correct
39 Correct 28 ms 2424 KB Output is correct
40 Correct 169 ms 2424 KB Output is correct
41 Correct 28 ms 2424 KB Output is correct
42 Correct 155 ms 2424 KB Output is correct
43 Correct 28 ms 2552 KB Output is correct
44 Correct 148 ms 2552 KB Output is correct
45 Correct 29 ms 2424 KB Output is correct
46 Correct 146 ms 2424 KB Output is correct
47 Correct 8 ms 1912 KB Output is correct
48 Correct 80 ms 2424 KB Output is correct
49 Correct 9 ms 1912 KB Output is correct
50 Correct 157 ms 2584 KB Output is correct
51 Correct 159 ms 2556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 516 ms 3248 KB Output is correct
2 Correct 17 ms 2808 KB Output is correct
3 Correct 532 ms 3220 KB Output is correct
4 Correct 17 ms 2552 KB Output is correct
5 Correct 555 ms 3216 KB Output is correct
6 Correct 16 ms 2552 KB Output is correct
7 Correct 587 ms 3216 KB Output is correct
8 Correct 17 ms 2552 KB Output is correct
9 Correct 537 ms 3192 KB Output is correct
10 Correct 17 ms 2552 KB Output is correct
11 Correct 549 ms 3224 KB Output is correct
12 Correct 16 ms 2680 KB Output is correct
13 Correct 513 ms 3320 KB Output is correct
14 Correct 569 ms 3320 KB Output is correct
15 Correct 16 ms 2552 KB Output is correct
16 Correct 530 ms 3188 KB Output is correct
17 Correct 16 ms 2552 KB Output is correct
18 Correct 537 ms 3320 KB Output is correct
19 Correct 17 ms 2552 KB Output is correct
20 Correct 570 ms 3320 KB Output is correct
21 Correct 16 ms 2552 KB Output is correct
22 Correct 256 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2296 KB Output is correct
2 Correct 7 ms 2296 KB Output is correct
3 Correct 7 ms 2296 KB Output is correct
4 Correct 7 ms 2296 KB Output is correct
5 Correct 8 ms 2300 KB Output is correct
6 Correct 7 ms 2296 KB Output is correct
7 Correct 7 ms 2296 KB Output is correct
8 Correct 7 ms 2296 KB Output is correct
9 Correct 7 ms 2300 KB Output is correct
10 Correct 7 ms 2296 KB Output is correct
11 Correct 7 ms 2296 KB Output is correct
12 Correct 8 ms 2424 KB Output is correct
13 Correct 7 ms 2296 KB Output is correct
14 Correct 7 ms 2296 KB Output is correct
15 Correct 7 ms 2296 KB Output is correct
16 Correct 7 ms 2296 KB Output is correct
17 Correct 7 ms 2296 KB Output is correct
18 Correct 7 ms 2296 KB Output is correct
19 Correct 8 ms 2296 KB Output is correct
20 Correct 7 ms 2296 KB Output is correct
21 Correct 6 ms 1784 KB Output is correct
22 Correct 7 ms 2296 KB Output is correct
23 Correct 7 ms 2424 KB Output is correct
24 Correct 7 ms 2296 KB Output is correct
25 Correct 8 ms 2296 KB Output is correct
26 Correct 154 ms 2424 KB Output is correct
27 Correct 28 ms 2424 KB Output is correct
28 Correct 150 ms 2424 KB Output is correct
29 Correct 29 ms 2424 KB Output is correct
30 Correct 156 ms 2552 KB Output is correct
31 Correct 27 ms 2424 KB Output is correct
32 Correct 155 ms 2424 KB Output is correct
33 Correct 28 ms 2552 KB Output is correct
34 Correct 18 ms 2424 KB Output is correct
35 Correct 30 ms 2424 KB Output is correct
36 Correct 158 ms 2536 KB Output is correct
37 Correct 28 ms 2424 KB Output is correct
38 Correct 153 ms 2540 KB Output is correct
39 Correct 28 ms 2424 KB Output is correct
40 Correct 169 ms 2424 KB Output is correct
41 Correct 28 ms 2424 KB Output is correct
42 Correct 155 ms 2424 KB Output is correct
43 Correct 28 ms 2552 KB Output is correct
44 Correct 148 ms 2552 KB Output is correct
45 Correct 29 ms 2424 KB Output is correct
46 Correct 146 ms 2424 KB Output is correct
47 Correct 8 ms 1912 KB Output is correct
48 Correct 80 ms 2424 KB Output is correct
49 Correct 9 ms 1912 KB Output is correct
50 Correct 157 ms 2584 KB Output is correct
51 Correct 159 ms 2556 KB Output is correct
52 Correct 516 ms 3248 KB Output is correct
53 Correct 17 ms 2808 KB Output is correct
54 Correct 532 ms 3220 KB Output is correct
55 Correct 17 ms 2552 KB Output is correct
56 Correct 555 ms 3216 KB Output is correct
57 Correct 16 ms 2552 KB Output is correct
58 Correct 587 ms 3216 KB Output is correct
59 Correct 17 ms 2552 KB Output is correct
60 Correct 537 ms 3192 KB Output is correct
61 Correct 17 ms 2552 KB Output is correct
62 Correct 549 ms 3224 KB Output is correct
63 Correct 16 ms 2680 KB Output is correct
64 Correct 513 ms 3320 KB Output is correct
65 Correct 569 ms 3320 KB Output is correct
66 Correct 16 ms 2552 KB Output is correct
67 Correct 530 ms 3188 KB Output is correct
68 Correct 16 ms 2552 KB Output is correct
69 Correct 537 ms 3320 KB Output is correct
70 Correct 17 ms 2552 KB Output is correct
71 Correct 570 ms 3320 KB Output is correct
72 Correct 16 ms 2552 KB Output is correct
73 Correct 256 ms 3320 KB Output is correct
74 Correct 2274 ms 5624 KB Output is correct
75 Correct 127 ms 5368 KB Output is correct
76 Correct 2233 ms 5624 KB Output is correct
77 Correct 152 ms 5496 KB Output is correct
78 Correct 2190 ms 5628 KB Output is correct
79 Correct 2282 ms 5624 KB Output is correct
80 Correct 135 ms 5368 KB Output is correct
81 Correct 128 ms 5496 KB Output is correct
82 Correct 2211 ms 5624 KB Output is correct
83 Correct 2397 ms 5752 KB Output is correct
84 Correct 132 ms 5496 KB Output is correct
85 Correct 2259 ms 5624 KB Output is correct
86 Correct 127 ms 5368 KB Output is correct
87 Correct 2224 ms 5624 KB Output is correct
88 Correct 135 ms 5496 KB Output is correct
89 Correct 2235 ms 5624 KB Output is correct
90 Correct 131 ms 5368 KB Output is correct
91 Correct 2236 ms 5624 KB Output is correct
92 Correct 136 ms 5368 KB Output is correct
93 Correct 2332 ms 5624 KB Output is correct
94 Correct 47 ms 4856 KB Output is correct
95 Correct 1101 ms 5780 KB Output is correct
96 Correct 47 ms 5240 KB Output is correct
97 Correct 2194 ms 5624 KB Output is correct
98 Correct 2224 ms 5624 KB Output is correct