Submission #677215

# Submission time Handle Problem Language Result Execution time Memory
677215 2023-01-02T15:01:07 Z ajab_01 Tug of War (BOI15_tug) C++17
41 / 100
2516 ms 4388 KB
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
vector<int> g[N];
int l[N] , r[N] , p[N] , cnt[2] , n , k;
bool mark[N] , ch , flg = 1;

void dfs(int ver , int sit){
	mark[ver] = 1;
	cnt[sit]++;
	for(int i : g[ver]){
		if(mark[i]) continue;
		dfs(i , sit ^ 1);
	}
}

bool check(int mask){
	bool vis[N];
	memset(vis , 0 , sizeof(vis));
	int sum1 = 0 , sum2 = 0;
	for(int i = 0 ; i < 2 * n ; i++){
		if(mask & (1 << i)){
			if(vis[r[i]])
				return 0;
			vis[r[i]] = 1;
			sum1 += p[i];
		}
		else{
			if(vis[l[i]])
				return 0;
			vis[l[i]] = 1;
			sum2 += p[i];
		}
	}

	return abs(sum1 - sum2) <= k;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> k;
	if(n <= 10){
		for(int i = 0 ; i < 2 * n ; i++){
			cin >> l[i] >> r[i] >> p[i];
			r[i] += n;
		}

		for(int mask = 0 ; mask < (1 << 2 * n) ; mask++)
			if(check(mask))
				ch = 1;

		if(ch)
			cout << "YES" << '\n';
		else
			cout << "NO" << '\n';
		return 0;
	}

	for(int i = 1 ; i <= 2 * n ; i++){
		cin >> l[i] >> r[i] >> p[i];
		g[i].push_back(l[i] + 2 * n);
		g[l[i] + 2 * n].push_back(i);
		g[i].push_back(r[i] + 3 * n);
		g[r[i] + 3 * n].push_back(i);
	}

	for(int i = 1 ; i <= 4 * n ; i++){
		if(!mark[i]){
			cnt[0] = cnt[1] = 0;
			dfs(i , (i > 2 * n ? 1 : 0));
			if(cnt[0] != cnt[1]) flg = 0;
		}
	}

	if(flg)
		cout << "YES" << '\n';
	else
		cout << "NO" << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2430 ms 2772 KB Output is correct
2 Correct 2437 ms 2772 KB Output is correct
3 Correct 2469 ms 2772 KB Output is correct
4 Correct 2516 ms 2772 KB Output is correct
5 Correct 2516 ms 2772 KB Output is correct
6 Correct 2453 ms 2772 KB Output is correct
7 Correct 2472 ms 2772 KB Output is correct
8 Correct 2432 ms 2772 KB Output is correct
9 Correct 2452 ms 2772 KB Output is correct
10 Correct 2449 ms 2756 KB Output is correct
11 Correct 2418 ms 2772 KB Output is correct
12 Correct 2405 ms 2772 KB Output is correct
13 Correct 2442 ms 2772 KB Output is correct
14 Correct 2421 ms 2772 KB Output is correct
15 Correct 2464 ms 2760 KB Output is correct
16 Correct 2414 ms 2772 KB Output is correct
17 Correct 2458 ms 2772 KB Output is correct
18 Correct 2427 ms 2772 KB Output is correct
19 Correct 2408 ms 2772 KB Output is correct
20 Correct 2417 ms 2772 KB Output is correct
21 Correct 2420 ms 2892 KB Output is correct
22 Correct 2372 ms 2772 KB Output is correct
23 Correct 2424 ms 2772 KB Output is correct
24 Correct 2420 ms 2772 KB Output is correct
25 Correct 2436 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2430 ms 2772 KB Output is correct
2 Correct 2437 ms 2772 KB Output is correct
3 Correct 2469 ms 2772 KB Output is correct
4 Correct 2516 ms 2772 KB Output is correct
5 Correct 2516 ms 2772 KB Output is correct
6 Correct 2453 ms 2772 KB Output is correct
7 Correct 2472 ms 2772 KB Output is correct
8 Correct 2432 ms 2772 KB Output is correct
9 Correct 2452 ms 2772 KB Output is correct
10 Correct 2449 ms 2756 KB Output is correct
11 Correct 2418 ms 2772 KB Output is correct
12 Correct 2405 ms 2772 KB Output is correct
13 Correct 2442 ms 2772 KB Output is correct
14 Correct 2421 ms 2772 KB Output is correct
15 Correct 2464 ms 2760 KB Output is correct
16 Correct 2414 ms 2772 KB Output is correct
17 Correct 2458 ms 2772 KB Output is correct
18 Correct 2427 ms 2772 KB Output is correct
19 Correct 2408 ms 2772 KB Output is correct
20 Correct 2417 ms 2772 KB Output is correct
21 Correct 2420 ms 2892 KB Output is correct
22 Correct 2372 ms 2772 KB Output is correct
23 Correct 2424 ms 2772 KB Output is correct
24 Correct 2420 ms 2772 KB Output is correct
25 Correct 2436 ms 2772 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Incorrect 3 ms 3028 KB Output isn't correct
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4172 KB Output is correct
2 Correct 11 ms 4168 KB Output is correct
3 Correct 10 ms 4140 KB Output is correct
4 Correct 10 ms 4200 KB Output is correct
5 Correct 9 ms 4208 KB Output is correct
6 Correct 11 ms 4180 KB Output is correct
7 Correct 10 ms 4180 KB Output is correct
8 Correct 11 ms 4180 KB Output is correct
9 Correct 11 ms 4192 KB Output is correct
10 Correct 13 ms 4180 KB Output is correct
11 Correct 10 ms 4180 KB Output is correct
12 Correct 15 ms 4160 KB Output is correct
13 Correct 9 ms 4096 KB Output is correct
14 Correct 10 ms 4120 KB Output is correct
15 Correct 12 ms 4180 KB Output is correct
16 Correct 10 ms 4180 KB Output is correct
17 Correct 10 ms 4196 KB Output is correct
18 Correct 10 ms 4092 KB Output is correct
19 Correct 10 ms 4180 KB Output is correct
20 Correct 9 ms 4180 KB Output is correct
21 Correct 10 ms 4388 KB Output is correct
22 Correct 10 ms 4112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2430 ms 2772 KB Output is correct
2 Correct 2437 ms 2772 KB Output is correct
3 Correct 2469 ms 2772 KB Output is correct
4 Correct 2516 ms 2772 KB Output is correct
5 Correct 2516 ms 2772 KB Output is correct
6 Correct 2453 ms 2772 KB Output is correct
7 Correct 2472 ms 2772 KB Output is correct
8 Correct 2432 ms 2772 KB Output is correct
9 Correct 2452 ms 2772 KB Output is correct
10 Correct 2449 ms 2756 KB Output is correct
11 Correct 2418 ms 2772 KB Output is correct
12 Correct 2405 ms 2772 KB Output is correct
13 Correct 2442 ms 2772 KB Output is correct
14 Correct 2421 ms 2772 KB Output is correct
15 Correct 2464 ms 2760 KB Output is correct
16 Correct 2414 ms 2772 KB Output is correct
17 Correct 2458 ms 2772 KB Output is correct
18 Correct 2427 ms 2772 KB Output is correct
19 Correct 2408 ms 2772 KB Output is correct
20 Correct 2417 ms 2772 KB Output is correct
21 Correct 2420 ms 2892 KB Output is correct
22 Correct 2372 ms 2772 KB Output is correct
23 Correct 2424 ms 2772 KB Output is correct
24 Correct 2420 ms 2772 KB Output is correct
25 Correct 2436 ms 2772 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Incorrect 3 ms 3028 KB Output isn't correct
28 Halted 0 ms 0 KB -