Submission #705392

# Submission time Handle Problem Language Result Execution time Memory
705392 2023-03-04T09:31:52 Z alvingogo Tug of War (BOI15_tug) C++14
100 / 100
2139 ms 9208 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

/* ok, bitset!!!


wait, is it possible that their's only one contestent that wants one kind of graph?

ok, let's just eliminate these cases, because sum_deg = 8n and we have4n points to consider if we make a graph from people i to left l_i and right r_i.
if we consider all the people and make a graph

if someone's deg = 1 then we know that we should take it and its neighbor, and wow, that's a matching problem!

first: find out if it has a perfect matching
iterate all the nodes deg = 1
we know people's deg = 2 so that L and R can do this

SAMITorz 

second: bitsetorz

rings, choose the odd edges or the even edges, and do knapsack to find out if we can make the difference less then k
i want to make it like "b |= b<<k" or else

maybe the right side should have strength in [a,b];
we can find out how much we add to the right side

given an array A, an int x, for all elements C, you can add C to x or subtract C to x, find out if you can make abs(x) less then or equal to k 
isn't it knapsack?

30000*30000*2*20/64 = 9e8 :EEEEE:
CF 1e9/s la
or greedy
greedy?

fine, since it's the only method i can figure out
let's gooooooooooo :TLEthinking:
*/


bitset<1200007> bt;

vector<vector<pair<int,int> > > g;
int main(){
    AquA;
	bt[0]=1;
	int n,k;
	cin >> n >> k;
	g.resize(4*n);
	vector<int> deg(4*n),vis(4*n);
	int sum=0;
	for(int i=0;i<2*n;i++){
		int a,b,s;
		cin >> a >> b >> s;
		a--;
		b--;
		sum+=s;
		g[a+2*n].push_back({i,s});
		g[i].push_back({a+2*n,s});
		g[b+3*n].push_back({i,s});
		g[i].push_back({b+3*n,s});
		deg[i]=2;
		deg[a+2*n]++;
		deg[b+3*n]++;
	}
	queue<int> q;
	for(int i=2*n;i<4*n;i++){
		if(deg[i]==1){
			q.push(i);
		}
		if(deg[i]==0){
			cout << "NO\n";
			return 0;
		}
	}
	while(q.size()){
		auto h=q.front();
		q.pop();
		int flag=0;
		for(auto y:g[h]){
			if(!vis[y.fs]){
				flag=1;
				vis[y.fs]=1;
				if(h>=3*n){
					bt<<=y.sc;
				}
				for(auto z:g[y.fs]){
					deg[z.fs]--;
					if(deg[z.fs]==1){
						q.push(z.fs);
					}
				}
			}
		}
		if(!flag){
			cout << "NO\n";
			return 0;
		}
	}
	for(int i=0;i<2*n;i++){
		if(!vis[i]){
			//dfs time :happy_mention:
			int a=0,b=0;
			for(int x=i,cnt=0,e=-1;cnt==0 || x!=i;cnt=1){
				vis[x]=1;
				for(auto f:g[x]){
					if(f.fs==e){
						continue;
					}
					if(f.fs>=3*n){
						a+=f.sc;
					}
					for(auto y:g[f.fs]){
						if(vis[y.fs] && (y.fs!=i || cnt==0)){

						}
						else{
							if(f.fs>=3*n){
								b+=y.sc;
							}
							x=y.fs;
							e=f.fs;
							break;
						}
					}
					break;
				}
			}
			//cout << a << " " << b << '\n';
			bt=(bt<<a)|(bt<<b);
		}
	}
	for(int i=0;i<=sum;i++){
		if(bt[i]){
			if(abs(i-(sum-i))<=k){
				cout << "YES\n";
				return 0;
			}
		}
	}
	cout << "NO\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1016 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 980 KB Output is correct
5 Correct 2 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 2 ms 1020 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 2 ms 1020 KB Output is correct
19 Correct 2 ms 980 KB Output is correct
20 Correct 2 ms 1056 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 1020 KB Output is correct
23 Correct 2 ms 980 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 2 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1016 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 980 KB Output is correct
5 Correct 2 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 2 ms 1020 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 2 ms 1020 KB Output is correct
19 Correct 2 ms 980 KB Output is correct
20 Correct 2 ms 1056 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 1020 KB Output is correct
23 Correct 2 ms 980 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 2 ms 980 KB Output is correct
26 Correct 138 ms 1492 KB Output is correct
27 Correct 25 ms 1492 KB Output is correct
28 Correct 148 ms 1548 KB Output is correct
29 Correct 22 ms 1492 KB Output is correct
30 Correct 137 ms 1492 KB Output is correct
31 Correct 19 ms 1492 KB Output is correct
32 Correct 126 ms 1492 KB Output is correct
33 Correct 20 ms 1492 KB Output is correct
34 Correct 11 ms 1540 KB Output is correct
35 Correct 24 ms 1492 KB Output is correct
36 Correct 124 ms 1492 KB Output is correct
37 Correct 19 ms 1492 KB Output is correct
38 Correct 125 ms 1492 KB Output is correct
39 Correct 21 ms 1492 KB Output is correct
40 Correct 134 ms 1492 KB Output is correct
41 Correct 24 ms 1492 KB Output is correct
42 Correct 124 ms 1492 KB Output is correct
43 Correct 19 ms 1492 KB Output is correct
44 Correct 139 ms 1492 KB Output is correct
45 Correct 19 ms 1536 KB Output is correct
46 Correct 139 ms 1492 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 88 ms 1616 KB Output is correct
49 Correct 79 ms 1620 KB Output is correct
50 Correct 134 ms 1492 KB Output is correct
51 Correct 132 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 631 ms 3700 KB Output is correct
2 Correct 9 ms 3412 KB Output is correct
3 Correct 654 ms 3708 KB Output is correct
4 Correct 10 ms 3412 KB Output is correct
5 Correct 653 ms 3712 KB Output is correct
6 Correct 9 ms 3412 KB Output is correct
7 Correct 668 ms 3700 KB Output is correct
8 Correct 9 ms 3412 KB Output is correct
9 Correct 647 ms 3704 KB Output is correct
10 Correct 9 ms 3412 KB Output is correct
11 Correct 711 ms 3704 KB Output is correct
12 Correct 9 ms 3396 KB Output is correct
13 Correct 656 ms 3716 KB Output is correct
14 Correct 680 ms 3704 KB Output is correct
15 Correct 9 ms 3412 KB Output is correct
16 Correct 645 ms 3700 KB Output is correct
17 Correct 11 ms 3452 KB Output is correct
18 Correct 653 ms 3668 KB Output is correct
19 Correct 9 ms 3460 KB Output is correct
20 Correct 684 ms 3592 KB Output is correct
21 Correct 10 ms 3424 KB Output is correct
22 Correct 396 ms 3824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1016 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 980 KB Output is correct
5 Correct 2 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 2 ms 1020 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 2 ms 1020 KB Output is correct
19 Correct 2 ms 980 KB Output is correct
20 Correct 2 ms 1056 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 1020 KB Output is correct
23 Correct 2 ms 980 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 2 ms 980 KB Output is correct
26 Correct 138 ms 1492 KB Output is correct
27 Correct 25 ms 1492 KB Output is correct
28 Correct 148 ms 1548 KB Output is correct
29 Correct 22 ms 1492 KB Output is correct
30 Correct 137 ms 1492 KB Output is correct
31 Correct 19 ms 1492 KB Output is correct
32 Correct 126 ms 1492 KB Output is correct
33 Correct 20 ms 1492 KB Output is correct
34 Correct 11 ms 1540 KB Output is correct
35 Correct 24 ms 1492 KB Output is correct
36 Correct 124 ms 1492 KB Output is correct
37 Correct 19 ms 1492 KB Output is correct
38 Correct 125 ms 1492 KB Output is correct
39 Correct 21 ms 1492 KB Output is correct
40 Correct 134 ms 1492 KB Output is correct
41 Correct 24 ms 1492 KB Output is correct
42 Correct 124 ms 1492 KB Output is correct
43 Correct 19 ms 1492 KB Output is correct
44 Correct 139 ms 1492 KB Output is correct
45 Correct 19 ms 1536 KB Output is correct
46 Correct 139 ms 1492 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 88 ms 1616 KB Output is correct
49 Correct 79 ms 1620 KB Output is correct
50 Correct 134 ms 1492 KB Output is correct
51 Correct 132 ms 1492 KB Output is correct
52 Correct 631 ms 3700 KB Output is correct
53 Correct 9 ms 3412 KB Output is correct
54 Correct 654 ms 3708 KB Output is correct
55 Correct 10 ms 3412 KB Output is correct
56 Correct 653 ms 3712 KB Output is correct
57 Correct 9 ms 3412 KB Output is correct
58 Correct 668 ms 3700 KB Output is correct
59 Correct 9 ms 3412 KB Output is correct
60 Correct 647 ms 3704 KB Output is correct
61 Correct 9 ms 3412 KB Output is correct
62 Correct 711 ms 3704 KB Output is correct
63 Correct 9 ms 3396 KB Output is correct
64 Correct 656 ms 3716 KB Output is correct
65 Correct 680 ms 3704 KB Output is correct
66 Correct 9 ms 3412 KB Output is correct
67 Correct 645 ms 3700 KB Output is correct
68 Correct 11 ms 3452 KB Output is correct
69 Correct 653 ms 3668 KB Output is correct
70 Correct 9 ms 3460 KB Output is correct
71 Correct 684 ms 3592 KB Output is correct
72 Correct 10 ms 3424 KB Output is correct
73 Correct 396 ms 3824 KB Output is correct
74 Correct 2126 ms 8788 KB Output is correct
75 Correct 109 ms 8652 KB Output is correct
76 Correct 1977 ms 8716 KB Output is correct
77 Correct 107 ms 8672 KB Output is correct
78 Correct 2114 ms 8712 KB Output is correct
79 Correct 1924 ms 8712 KB Output is correct
80 Correct 124 ms 8712 KB Output is correct
81 Correct 105 ms 8660 KB Output is correct
82 Correct 1870 ms 8716 KB Output is correct
83 Correct 1864 ms 8780 KB Output is correct
84 Correct 113 ms 8716 KB Output is correct
85 Correct 1912 ms 8708 KB Output is correct
86 Correct 104 ms 8716 KB Output is correct
87 Correct 2005 ms 8720 KB Output is correct
88 Correct 116 ms 8660 KB Output is correct
89 Correct 1895 ms 8712 KB Output is correct
90 Correct 110 ms 8708 KB Output is correct
91 Correct 2041 ms 8712 KB Output is correct
92 Correct 121 ms 8708 KB Output is correct
93 Correct 1977 ms 8708 KB Output is correct
94 Correct 29 ms 8596 KB Output is correct
95 Correct 1273 ms 9096 KB Output is correct
96 Correct 1270 ms 9208 KB Output is correct
97 Correct 2040 ms 8716 KB Output is correct
98 Correct 2139 ms 8704 KB Output is correct