#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<600007> 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
672 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
676 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
672 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
676 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
61 ms |
1236 KB |
Output is correct |
27 |
Correct |
11 ms |
1172 KB |
Output is correct |
28 |
Correct |
61 ms |
1168 KB |
Output is correct |
29 |
Correct |
10 ms |
1236 KB |
Output is correct |
30 |
Correct |
62 ms |
1196 KB |
Output is correct |
31 |
Correct |
11 ms |
1236 KB |
Output is correct |
32 |
Correct |
64 ms |
1236 KB |
Output is correct |
33 |
Correct |
11 ms |
1196 KB |
Output is correct |
34 |
Correct |
10 ms |
1108 KB |
Output is correct |
35 |
Correct |
11 ms |
1236 KB |
Output is correct |
36 |
Correct |
64 ms |
1236 KB |
Output is correct |
37 |
Correct |
11 ms |
1236 KB |
Output is correct |
38 |
Correct |
61 ms |
1236 KB |
Output is correct |
39 |
Correct |
11 ms |
1236 KB |
Output is correct |
40 |
Correct |
70 ms |
1192 KB |
Output is correct |
41 |
Correct |
10 ms |
1236 KB |
Output is correct |
42 |
Correct |
65 ms |
1180 KB |
Output is correct |
43 |
Correct |
11 ms |
1236 KB |
Output is correct |
44 |
Correct |
75 ms |
1236 KB |
Output is correct |
45 |
Correct |
11 ms |
1236 KB |
Output is correct |
46 |
Correct |
75 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
980 KB |
Output is correct |
48 |
Correct |
39 ms |
1192 KB |
Output is correct |
49 |
Correct |
38 ms |
1236 KB |
Output is correct |
50 |
Correct |
61 ms |
1236 KB |
Output is correct |
51 |
Correct |
66 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
3436 KB |
Output is correct |
2 |
Correct |
9 ms |
3376 KB |
Output is correct |
3 |
Correct |
298 ms |
3404 KB |
Output is correct |
4 |
Correct |
9 ms |
3320 KB |
Output is correct |
5 |
Correct |
320 ms |
3424 KB |
Output is correct |
6 |
Correct |
10 ms |
3276 KB |
Output is correct |
7 |
Correct |
376 ms |
3432 KB |
Output is correct |
8 |
Correct |
9 ms |
3284 KB |
Output is correct |
9 |
Correct |
314 ms |
3424 KB |
Output is correct |
10 |
Correct |
9 ms |
3284 KB |
Output is correct |
11 |
Correct |
302 ms |
3428 KB |
Output is correct |
12 |
Correct |
12 ms |
3264 KB |
Output is correct |
13 |
Correct |
309 ms |
3436 KB |
Output is correct |
14 |
Correct |
336 ms |
3432 KB |
Output is correct |
15 |
Correct |
9 ms |
3276 KB |
Output is correct |
16 |
Correct |
311 ms |
3428 KB |
Output is correct |
17 |
Correct |
9 ms |
3284 KB |
Output is correct |
18 |
Correct |
361 ms |
3400 KB |
Output is correct |
19 |
Correct |
9 ms |
3268 KB |
Output is correct |
20 |
Correct |
306 ms |
3412 KB |
Output is correct |
21 |
Correct |
9 ms |
3284 KB |
Output is correct |
22 |
Correct |
198 ms |
3472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
668 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
672 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
676 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
61 ms |
1236 KB |
Output is correct |
27 |
Correct |
11 ms |
1172 KB |
Output is correct |
28 |
Correct |
61 ms |
1168 KB |
Output is correct |
29 |
Correct |
10 ms |
1236 KB |
Output is correct |
30 |
Correct |
62 ms |
1196 KB |
Output is correct |
31 |
Correct |
11 ms |
1236 KB |
Output is correct |
32 |
Correct |
64 ms |
1236 KB |
Output is correct |
33 |
Correct |
11 ms |
1196 KB |
Output is correct |
34 |
Correct |
10 ms |
1108 KB |
Output is correct |
35 |
Correct |
11 ms |
1236 KB |
Output is correct |
36 |
Correct |
64 ms |
1236 KB |
Output is correct |
37 |
Correct |
11 ms |
1236 KB |
Output is correct |
38 |
Correct |
61 ms |
1236 KB |
Output is correct |
39 |
Correct |
11 ms |
1236 KB |
Output is correct |
40 |
Correct |
70 ms |
1192 KB |
Output is correct |
41 |
Correct |
10 ms |
1236 KB |
Output is correct |
42 |
Correct |
65 ms |
1180 KB |
Output is correct |
43 |
Correct |
11 ms |
1236 KB |
Output is correct |
44 |
Correct |
75 ms |
1236 KB |
Output is correct |
45 |
Correct |
11 ms |
1236 KB |
Output is correct |
46 |
Correct |
75 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
980 KB |
Output is correct |
48 |
Correct |
39 ms |
1192 KB |
Output is correct |
49 |
Correct |
38 ms |
1236 KB |
Output is correct |
50 |
Correct |
61 ms |
1236 KB |
Output is correct |
51 |
Correct |
66 ms |
1236 KB |
Output is correct |
52 |
Correct |
308 ms |
3436 KB |
Output is correct |
53 |
Correct |
9 ms |
3376 KB |
Output is correct |
54 |
Correct |
298 ms |
3404 KB |
Output is correct |
55 |
Correct |
9 ms |
3320 KB |
Output is correct |
56 |
Correct |
320 ms |
3424 KB |
Output is correct |
57 |
Correct |
10 ms |
3276 KB |
Output is correct |
58 |
Correct |
376 ms |
3432 KB |
Output is correct |
59 |
Correct |
9 ms |
3284 KB |
Output is correct |
60 |
Correct |
314 ms |
3424 KB |
Output is correct |
61 |
Correct |
9 ms |
3284 KB |
Output is correct |
62 |
Correct |
302 ms |
3428 KB |
Output is correct |
63 |
Correct |
12 ms |
3264 KB |
Output is correct |
64 |
Correct |
309 ms |
3436 KB |
Output is correct |
65 |
Correct |
336 ms |
3432 KB |
Output is correct |
66 |
Correct |
9 ms |
3276 KB |
Output is correct |
67 |
Correct |
311 ms |
3428 KB |
Output is correct |
68 |
Correct |
9 ms |
3284 KB |
Output is correct |
69 |
Correct |
361 ms |
3400 KB |
Output is correct |
70 |
Correct |
9 ms |
3268 KB |
Output is correct |
71 |
Correct |
306 ms |
3412 KB |
Output is correct |
72 |
Correct |
9 ms |
3284 KB |
Output is correct |
73 |
Correct |
198 ms |
3472 KB |
Output is correct |
74 |
Correct |
963 ms |
8924 KB |
Output is correct |
75 |
Correct |
79 ms |
8908 KB |
Output is correct |
76 |
Correct |
906 ms |
9096 KB |
Output is correct |
77 |
Correct |
77 ms |
9000 KB |
Output is correct |
78 |
Correct |
936 ms |
9108 KB |
Output is correct |
79 |
Correct |
959 ms |
8924 KB |
Output is correct |
80 |
Correct |
67 ms |
8916 KB |
Output is correct |
81 |
Correct |
73 ms |
8964 KB |
Output is correct |
82 |
Correct |
936 ms |
8976 KB |
Output is correct |
83 |
Correct |
911 ms |
8924 KB |
Output is correct |
84 |
Correct |
76 ms |
9012 KB |
Output is correct |
85 |
Correct |
1126 ms |
9032 KB |
Output is correct |
86 |
Correct |
72 ms |
9008 KB |
Output is correct |
87 |
Correct |
920 ms |
9016 KB |
Output is correct |
88 |
Correct |
73 ms |
8908 KB |
Output is correct |
89 |
Correct |
927 ms |
9088 KB |
Output is correct |
90 |
Correct |
72 ms |
8908 KB |
Output is correct |
91 |
Correct |
963 ms |
9036 KB |
Output is correct |
92 |
Correct |
79 ms |
8908 KB |
Output is correct |
93 |
Correct |
924 ms |
9036 KB |
Output is correct |
94 |
Correct |
32 ms |
8996 KB |
Output is correct |
95 |
Correct |
620 ms |
9384 KB |
Output is correct |
96 |
Correct |
587 ms |
9420 KB |
Output is correct |
97 |
Correct |
947 ms |
9008 KB |
Output is correct |
98 |
Correct |
921 ms |
9036 KB |
Output is correct |