답안 #677245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
677245 2023-01-02T16:13:02 Z amirhoseinfar1385 Tug of War (BOI15_tug) C++17
100 / 100
2212 ms 11368 KB
#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<pair<pair<int,int>,int>>all;
vector<set<int>>fa,fb;
int res=0,now=0;
int unnow=0;
vector<int>vis;
vector<int>allp;
vector<int>bf;
set<int>alla,allb;
void del(int u,int z){
	if(u==-1||vis[u]==1){
		return ;
	}
	vis[u]=1;
	int next=-1;
	if(z==1){
		allb.insert(all[u].first.second);
		unnow-=all[u].second;
		fb[all[u].first.second].erase(u);
		fa[all[u].first.first].erase(u);
		if((int)fa[all[u].first.first].size()==1){
			next=*(fa[all[u].first.first].begin());
		}
	}
	else{
		unnow+=all[u].second;
		alla.insert(all[u].first.first);
		fb[all[u].first.second].erase(u);
		fa[all[u].first.first].erase(u);
		if((int)fb[all[u].first.second].size()==1){
			next=*(fb[all[u].first.second].begin());
		}
	}
	del(next,(z^1));
}

void bfs(int z){
	if(bf.size()==0){
		return ;
	}
//	cout<<bf[0]<<" "<<z<<" "<<allp.size()<<endl;
	vis[bf[0]]=1;
	for(auto x:bf){
		if(z==1){
			res-=all[x].second;
			allb.insert(all[x].first.second);
			fb[all[x].first.second].erase(x);
		}
		else{
			res+=all[x].second;
			alla.insert(all[x].first.first);
			fa[all[x].first.first].erase(x);
		}
	}
	vector<int>fake;
	for(auto x:bf){
		if(z==1){
			for(auto y:fb[all[x].first.second]){
				int v=y;
				if(vis[v]==0){
					vis[v]=1;
					fake.push_back(v);
				}
			}
		}
		else{
			for(auto y:fa[all[x].first.first]){
				int v=y;
				if(vis[v]==0){
					vis[v]=1;
					fake.push_back(v);
				}
			}
		}
	}
	bf.swap(fake);
	bfs((z^1));
}
const int mid=20*30000+10;
void solve(){
	bitset<mid*2>bt;
	bt[mid]=1;
	for(int i=0;i<(int)allp.size();i++){
		int x=allp[i];
		bitset<mid*2>fake,fakea;
		fake=(bt>>x);
		fakea=(bt<<x);
		bt=(fake|fakea);
	}
	for(int i=mid-k+unnow;i<=mid+k+unnow;i++){
		if(bt[i]==1){
			cout<<"YES\n";
			exit(0);
		}
	}
	cout<<"NO\n";
	exit(0);
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	vis.resize(n*2+1);
	fa.resize(n+1),fb.resize(n+1);
	all.resize(n*2);
	for(int i=0;i<n*2;i++){
		cin>>all[i].first.first>>all[i].first.second>>all[i].second;
	}
	for(int i=0;i<2*n;i++){
		fa[all[i].first.first].insert(i);
		fb[all[i].first.second].insert(i);
	}
	for(int i=1;i<=n;i++){
		if((int)fa[i].size()==1){
			auto x=*(fa[i].begin());
			del(x,0);
		}
		if((int)fb[i].size()==1){
			auto x=*(fb[i].begin());
			del(x,1);
		}
	}
	for(int i=0;i<n*2;i++){
		if(vis[i]==0){
			bf.push_back(i);
			res=0;
			bfs(1);
			allp.push_back(abs(res));
		}
	}
	if((int)alla.size()!=n||(int)allb.size()!=n){
		cout<<"NO\n";
		exit(0);
	}
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 852 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 852 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 145 ms 1520 KB Output is correct
27 Correct 20 ms 1492 KB Output is correct
28 Correct 125 ms 1576 KB Output is correct
29 Correct 20 ms 1488 KB Output is correct
30 Correct 131 ms 1588 KB Output is correct
31 Correct 20 ms 1492 KB Output is correct
32 Correct 125 ms 1572 KB Output is correct
33 Correct 20 ms 1492 KB Output is correct
34 Correct 12 ms 1556 KB Output is correct
35 Correct 22 ms 1576 KB Output is correct
36 Correct 135 ms 1576 KB Output is correct
37 Correct 21 ms 1492 KB Output is correct
38 Correct 136 ms 1568 KB Output is correct
39 Correct 20 ms 1492 KB Output is correct
40 Correct 132 ms 1572 KB Output is correct
41 Correct 22 ms 1516 KB Output is correct
42 Correct 128 ms 1492 KB Output is correct
43 Correct 21 ms 1492 KB Output is correct
44 Correct 133 ms 1572 KB Output is correct
45 Correct 20 ms 1568 KB Output is correct
46 Correct 148 ms 1572 KB Output is correct
47 Correct 3 ms 980 KB Output is correct
48 Correct 71 ms 1576 KB Output is correct
49 Correct 66 ms 1580 KB Output is correct
50 Correct 128 ms 1504 KB Output is correct
51 Correct 128 ms 1580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 418 ms 4156 KB Output is correct
2 Correct 14 ms 3620 KB Output is correct
3 Correct 399 ms 4388 KB Output is correct
4 Correct 20 ms 3660 KB Output is correct
5 Correct 362 ms 4388 KB Output is correct
6 Correct 17 ms 3616 KB Output is correct
7 Correct 389 ms 4376 KB Output is correct
8 Correct 14 ms 3668 KB Output is correct
9 Correct 423 ms 4376 KB Output is correct
10 Correct 15 ms 3668 KB Output is correct
11 Correct 395 ms 4380 KB Output is correct
12 Correct 15 ms 3668 KB Output is correct
13 Correct 360 ms 4316 KB Output is correct
14 Correct 364 ms 4372 KB Output is correct
15 Correct 16 ms 3720 KB Output is correct
16 Correct 361 ms 4376 KB Output is correct
17 Correct 18 ms 3636 KB Output is correct
18 Correct 375 ms 4428 KB Output is correct
19 Correct 14 ms 3668 KB Output is correct
20 Correct 398 ms 4384 KB Output is correct
21 Correct 17 ms 3788 KB Output is correct
22 Correct 196 ms 4320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 2 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 2 ms 852 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 2 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 852 KB Output is correct
24 Correct 1 ms 852 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 145 ms 1520 KB Output is correct
27 Correct 20 ms 1492 KB Output is correct
28 Correct 125 ms 1576 KB Output is correct
29 Correct 20 ms 1488 KB Output is correct
30 Correct 131 ms 1588 KB Output is correct
31 Correct 20 ms 1492 KB Output is correct
32 Correct 125 ms 1572 KB Output is correct
33 Correct 20 ms 1492 KB Output is correct
34 Correct 12 ms 1556 KB Output is correct
35 Correct 22 ms 1576 KB Output is correct
36 Correct 135 ms 1576 KB Output is correct
37 Correct 21 ms 1492 KB Output is correct
38 Correct 136 ms 1568 KB Output is correct
39 Correct 20 ms 1492 KB Output is correct
40 Correct 132 ms 1572 KB Output is correct
41 Correct 22 ms 1516 KB Output is correct
42 Correct 128 ms 1492 KB Output is correct
43 Correct 21 ms 1492 KB Output is correct
44 Correct 133 ms 1572 KB Output is correct
45 Correct 20 ms 1568 KB Output is correct
46 Correct 148 ms 1572 KB Output is correct
47 Correct 3 ms 980 KB Output is correct
48 Correct 71 ms 1576 KB Output is correct
49 Correct 66 ms 1580 KB Output is correct
50 Correct 128 ms 1504 KB Output is correct
51 Correct 128 ms 1580 KB Output is correct
52 Correct 418 ms 4156 KB Output is correct
53 Correct 14 ms 3620 KB Output is correct
54 Correct 399 ms 4388 KB Output is correct
55 Correct 20 ms 3660 KB Output is correct
56 Correct 362 ms 4388 KB Output is correct
57 Correct 17 ms 3616 KB Output is correct
58 Correct 389 ms 4376 KB Output is correct
59 Correct 14 ms 3668 KB Output is correct
60 Correct 423 ms 4376 KB Output is correct
61 Correct 15 ms 3668 KB Output is correct
62 Correct 395 ms 4380 KB Output is correct
63 Correct 15 ms 3668 KB Output is correct
64 Correct 360 ms 4316 KB Output is correct
65 Correct 364 ms 4372 KB Output is correct
66 Correct 16 ms 3720 KB Output is correct
67 Correct 361 ms 4376 KB Output is correct
68 Correct 18 ms 3636 KB Output is correct
69 Correct 375 ms 4428 KB Output is correct
70 Correct 14 ms 3668 KB Output is correct
71 Correct 398 ms 4384 KB Output is correct
72 Correct 17 ms 3788 KB Output is correct
73 Correct 196 ms 4320 KB Output is correct
74 Correct 1928 ms 11356 KB Output is correct
75 Correct 136 ms 11052 KB Output is correct
76 Correct 1981 ms 11352 KB Output is correct
77 Correct 149 ms 11072 KB Output is correct
78 Correct 2064 ms 11360 KB Output is correct
79 Correct 2212 ms 11364 KB Output is correct
80 Correct 163 ms 11128 KB Output is correct
81 Correct 196 ms 11076 KB Output is correct
82 Correct 1996 ms 11368 KB Output is correct
83 Correct 1924 ms 11352 KB Output is correct
84 Correct 155 ms 11212 KB Output is correct
85 Correct 1949 ms 11364 KB Output is correct
86 Correct 152 ms 11060 KB Output is correct
87 Correct 1967 ms 11364 KB Output is correct
88 Correct 133 ms 11084 KB Output is correct
89 Correct 1874 ms 11352 KB Output is correct
90 Correct 143 ms 11100 KB Output is correct
91 Correct 1931 ms 11360 KB Output is correct
92 Correct 142 ms 11128 KB Output is correct
93 Correct 1927 ms 11364 KB Output is correct
94 Correct 73 ms 10624 KB Output is correct
95 Correct 1010 ms 11284 KB Output is correct
96 Correct 1046 ms 11228 KB Output is correct
97 Correct 2019 ms 11368 KB Output is correct
98 Correct 2013 ms 11356 KB Output is correct