Submission #952571

# Submission time Handle Problem Language Result Execution time Memory
952571 2024-03-24T09:17:21 Z PM1 Tug of War (BOI15_tug) C++17
100 / 100
1749 ms 14168 KB
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
const int mxn=3e4+5;
int n,k,f[mxn*2][2],ans=0,st[mxn*2],res=0;
set<int>s[mxn*2];
set<pair<int,int> >g;
bool mark[mxn*2];
vector<int>nk;
void dfs(int z,int dis){
	mark[z]=1;
	int x=*s[z].begin();
	res+=(dis&1)?-st[x]:st[x];
	s[z].erase(x);
	int y=z^f[x][0]^f[x][1];
	s[y].erase(x);
	if(!mark[y])
		dfs(y,dis+1);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n*2;i++){
		int x,y;
		cin>>x>>y>>st[i];
		s[x].insert(i);
		s[y+mxn].insert(i);
		f[i][0]=x;
		f[i][1]=y+mxn;
	}
	for(int i=1;i<=n;i++){
		g.insert({s[i].size(),i});
		g.insert({s[i+mxn].size(),i+mxn});
	}
	while(g.size() && g.begin()->fr<2){
		if(g.begin()->fr<=0){
			cout<<"NO";
			return 0;
		}
		int x=g.begin()->sc;
		int y=*s[x].begin();
		s[x].erase(y);
		int z=x^f[y][0]^f[y][1];
		s[z].erase(y);
		g.erase({1,x});
		g.erase({s[z].size()+1,z});
		g.insert({s[z].size(),z});
		if(x>mxn)ans+=st[y];
		else ans-=st[y];
	}
	for(int i=1;i<=n;i++){
		if(!s[i].size() || mark[i])continue;
		res=0;
		dfs(i,0);
		if(res<0)res*=-1;
		nk.push_back(res);
	}
	for(int i=1+mxn;i<=n+mxn;i++){
		if(!s[i].size() || mark[i])continue;
		res=0;
		dfs(i,0);
		if(res<0)res*=-1;
		nk.push_back(res);
	}
	const int gg=20*30000+5,ggg=gg*2;
	bitset<ggg>b;
	b[gg]=1;
	for(int i=1;i<=nk.size();i++){
		int x=nk[i-1];
		b=((b<<x)|(b>>x));
	}
	for(int i=gg-k+ans;i<=gg+k+ans;i++){
		if(b[i]){
			cout<<"YES";
			return 0;
		}
	}
	cout<<"NO";
	return 0;

}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:71:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for(int i=1;i<=nk.size();i++){
      |              ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3672 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 2 ms 3932 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 2 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3864 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3672 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3672 KB Output is correct
20 Correct 2 ms 3672 KB Output is correct
21 Correct 1 ms 3864 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3676 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3672 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 2 ms 3932 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 2 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3864 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3672 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3672 KB Output is correct
20 Correct 2 ms 3672 KB Output is correct
21 Correct 1 ms 3864 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3676 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3676 KB Output is correct
26 Correct 112 ms 4532 KB Output is correct
27 Correct 18 ms 4440 KB Output is correct
28 Correct 112 ms 4444 KB Output is correct
29 Correct 24 ms 4952 KB Output is correct
30 Correct 113 ms 4536 KB Output is correct
31 Correct 18 ms 4444 KB Output is correct
32 Correct 112 ms 4528 KB Output is correct
33 Correct 18 ms 4444 KB Output is correct
34 Correct 11 ms 4444 KB Output is correct
35 Correct 18 ms 4548 KB Output is correct
36 Correct 114 ms 4536 KB Output is correct
37 Correct 19 ms 4440 KB Output is correct
38 Correct 114 ms 4444 KB Output is correct
39 Correct 18 ms 4440 KB Output is correct
40 Correct 112 ms 4444 KB Output is correct
41 Correct 18 ms 4440 KB Output is correct
42 Correct 113 ms 4528 KB Output is correct
43 Correct 18 ms 4444 KB Output is correct
44 Correct 111 ms 4440 KB Output is correct
45 Correct 18 ms 4444 KB Output is correct
46 Correct 113 ms 4444 KB Output is correct
47 Correct 3 ms 4440 KB Output is correct
48 Correct 64 ms 4524 KB Output is correct
49 Correct 60 ms 4444 KB Output is correct
50 Correct 121 ms 4532 KB Output is correct
51 Correct 117 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 7040 KB Output is correct
2 Correct 9 ms 6748 KB Output is correct
3 Correct 307 ms 7004 KB Output is correct
4 Correct 10 ms 6744 KB Output is correct
5 Correct 317 ms 7024 KB Output is correct
6 Correct 10 ms 6840 KB Output is correct
7 Correct 318 ms 7032 KB Output is correct
8 Correct 9 ms 6748 KB Output is correct
9 Correct 323 ms 7024 KB Output is correct
10 Correct 9 ms 6744 KB Output is correct
11 Correct 315 ms 7252 KB Output is correct
12 Correct 9 ms 6748 KB Output is correct
13 Correct 309 ms 7040 KB Output is correct
14 Correct 310 ms 7004 KB Output is correct
15 Correct 12 ms 7000 KB Output is correct
16 Correct 307 ms 7256 KB Output is correct
17 Correct 9 ms 6744 KB Output is correct
18 Correct 312 ms 7028 KB Output is correct
19 Correct 9 ms 6748 KB Output is correct
20 Correct 322 ms 7004 KB Output is correct
21 Correct 10 ms 6748 KB Output is correct
22 Correct 174 ms 6976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3672 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 2 ms 3932 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 2 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3864 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3672 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3672 KB Output is correct
20 Correct 2 ms 3672 KB Output is correct
21 Correct 1 ms 3864 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3676 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3676 KB Output is correct
26 Correct 112 ms 4532 KB Output is correct
27 Correct 18 ms 4440 KB Output is correct
28 Correct 112 ms 4444 KB Output is correct
29 Correct 24 ms 4952 KB Output is correct
30 Correct 113 ms 4536 KB Output is correct
31 Correct 18 ms 4444 KB Output is correct
32 Correct 112 ms 4528 KB Output is correct
33 Correct 18 ms 4444 KB Output is correct
34 Correct 11 ms 4444 KB Output is correct
35 Correct 18 ms 4548 KB Output is correct
36 Correct 114 ms 4536 KB Output is correct
37 Correct 19 ms 4440 KB Output is correct
38 Correct 114 ms 4444 KB Output is correct
39 Correct 18 ms 4440 KB Output is correct
40 Correct 112 ms 4444 KB Output is correct
41 Correct 18 ms 4440 KB Output is correct
42 Correct 113 ms 4528 KB Output is correct
43 Correct 18 ms 4444 KB Output is correct
44 Correct 111 ms 4440 KB Output is correct
45 Correct 18 ms 4444 KB Output is correct
46 Correct 113 ms 4444 KB Output is correct
47 Correct 3 ms 4440 KB Output is correct
48 Correct 64 ms 4524 KB Output is correct
49 Correct 60 ms 4444 KB Output is correct
50 Correct 121 ms 4532 KB Output is correct
51 Correct 117 ms 4440 KB Output is correct
52 Correct 330 ms 7040 KB Output is correct
53 Correct 9 ms 6748 KB Output is correct
54 Correct 307 ms 7004 KB Output is correct
55 Correct 10 ms 6744 KB Output is correct
56 Correct 317 ms 7024 KB Output is correct
57 Correct 10 ms 6840 KB Output is correct
58 Correct 318 ms 7032 KB Output is correct
59 Correct 9 ms 6748 KB Output is correct
60 Correct 323 ms 7024 KB Output is correct
61 Correct 9 ms 6744 KB Output is correct
62 Correct 315 ms 7252 KB Output is correct
63 Correct 9 ms 6748 KB Output is correct
64 Correct 309 ms 7040 KB Output is correct
65 Correct 310 ms 7004 KB Output is correct
66 Correct 12 ms 7000 KB Output is correct
67 Correct 307 ms 7256 KB Output is correct
68 Correct 9 ms 6744 KB Output is correct
69 Correct 312 ms 7028 KB Output is correct
70 Correct 9 ms 6748 KB Output is correct
71 Correct 322 ms 7004 KB Output is correct
72 Correct 10 ms 6748 KB Output is correct
73 Correct 174 ms 6976 KB Output is correct
74 Correct 1673 ms 14128 KB Output is correct
75 Correct 95 ms 13716 KB Output is correct
76 Correct 1685 ms 14124 KB Output is correct
77 Correct 94 ms 13660 KB Output is correct
78 Correct 1675 ms 14116 KB Output is correct
79 Correct 1695 ms 14148 KB Output is correct
80 Correct 98 ms 13880 KB Output is correct
81 Correct 104 ms 13840 KB Output is correct
82 Correct 1713 ms 13852 KB Output is correct
83 Correct 1704 ms 14116 KB Output is correct
84 Correct 108 ms 13652 KB Output is correct
85 Correct 1687 ms 14124 KB Output is correct
86 Correct 99 ms 13648 KB Output is correct
87 Correct 1677 ms 14160 KB Output is correct
88 Correct 95 ms 13652 KB Output is correct
89 Correct 1684 ms 14120 KB Output is correct
90 Correct 93 ms 13656 KB Output is correct
91 Correct 1717 ms 14124 KB Output is correct
92 Correct 93 ms 13648 KB Output is correct
93 Correct 1681 ms 14120 KB Output is correct
94 Correct 31 ms 13660 KB Output is correct
95 Correct 880 ms 13992 KB Output is correct
96 Correct 891 ms 14168 KB Output is correct
97 Correct 1749 ms 14116 KB Output is correct
98 Correct 1733 ms 14120 KB Output is correct