#include <bits/stdc++.h>
#define sz(x) (int)(x.size())
const int MAXN = 6e4+10 ;
const int MAX = 3e4+10 ;
using namespace std ;
int N , K , s_left , s_tudo ;
int S[MAXN] , unnamed[MAX*40] ;
pair<int,int> p[MAXN] , loc[MAXN] ;
vector<int> freq[MAXN] ;
bool dp[MAX*20] ;
bool cmp(int x, int y) { return make_pair(sz(freq[x]),x) < make_pair(sz(freq[y]),y) ; }
set<int, bool(*)(int, int) > s(cmp) ;
void impossible()
{
printf("NO\n") ;
exit(0) ;
}
int solve()
{
int lef = 0 , rig = 0 ;
while(!s.empty() && sz(freq[*s.begin()]) <= 1 )
{
int id = *s.begin() ;
s.erase(s.begin());
if( sz(freq[id]) == 0 ) impossible() ;
int P = freq[id][0] ;
if( id == p[P].first )
{
swap(p[P].first, p[P].second ) ;
swap(loc[P].first ,loc[P].second ) ;
}
int otherId = p[P].first;
if( id<= N ) lef += S[P] ;
else rig += S[P] ;
if( s.find(otherId) == s.end() ) continue ;
s.erase( s.find(otherId) ) ;
int tam = sz(freq[otherId]) ;
int V = freq[otherId][tam-1] ;
if( otherId == p[V].second )
{
swap(p[V].first, p[V].second ) ;
swap(loc[V].first, loc[V].second ) ;
}
loc[ V ].first = loc[P].first;
freq[otherId][ loc[V].first ] = V ;
freq[otherId].pop_back() ;
s.insert(otherId) ;
}
return lef-rig ;
}
int main()
{
scanf("%d %d", &N, &K ) ;
for(int i = 1 , a , b ; i <= (N<<1) ; i++ )
{
scanf("%d %d %d", &a, &b , &S[i] ) ;
b += N ;
p[i] = make_pair(a,b) ;
loc[i] = make_pair( sz(freq[a]) , sz(freq[b]) ) ;
freq[a].push_back(i) ;
freq[b].push_back(i) ;
}
for(int i = 1 ; i <= (N<<1); i++ ) s.insert(i) ;
int S = abs(solve()) ;
dp[S] = dp[0] = true ;
while(!s.empty())
{
int x = *s.begin() ;
s.erase(s.find(x)) ;
freq[x].pop_back() ;
s.insert(x) ;
int k= abs(solve()) ;
unnamed[ k ]++ ;
S += k ;
}
for(int i = 0 ; i <= 20*N ; i++ )
{
if( unnamed[i] == 0 ) continue ;
for(int j = 20*N ; j >= i ; j-- )
if( dp[j-i] )
for(int b = j , q = 0 ; b <= 20*N && q < unnamed[i] && !dp[b] ; b += i , q++ )
dp[ b ] = true ;
}
for(int i = 0 ; i <= 20*N ; i++ )
{
int ans = i-(S-i) ;
if( ans < -K || ans > K || !dp[i] ) continue ;
printf("YES\n") ;
return 0 ;
}
impossible() ;
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d %d", &N, &K ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~
tug.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d %d %d", &a, &b , &S[i] ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1720 KB |
Output is correct |
2 |
Correct |
2 ms |
1740 KB |
Output is correct |
3 |
Correct |
3 ms |
1740 KB |
Output is correct |
4 |
Correct |
2 ms |
1740 KB |
Output is correct |
5 |
Correct |
2 ms |
1720 KB |
Output is correct |
6 |
Correct |
2 ms |
1740 KB |
Output is correct |
7 |
Correct |
2 ms |
1716 KB |
Output is correct |
8 |
Correct |
2 ms |
1720 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1720 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1720 KB |
Output is correct |
13 |
Correct |
2 ms |
1720 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1716 KB |
Output is correct |
20 |
Correct |
2 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1740 KB |
Output is correct |
24 |
Correct |
2 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
1724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1720 KB |
Output is correct |
2 |
Correct |
2 ms |
1740 KB |
Output is correct |
3 |
Correct |
3 ms |
1740 KB |
Output is correct |
4 |
Correct |
2 ms |
1740 KB |
Output is correct |
5 |
Correct |
2 ms |
1720 KB |
Output is correct |
6 |
Correct |
2 ms |
1740 KB |
Output is correct |
7 |
Correct |
2 ms |
1716 KB |
Output is correct |
8 |
Correct |
2 ms |
1720 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1720 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1720 KB |
Output is correct |
13 |
Correct |
2 ms |
1720 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1716 KB |
Output is correct |
20 |
Correct |
2 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1740 KB |
Output is correct |
24 |
Correct |
2 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
1724 KB |
Output is correct |
26 |
Correct |
7 ms |
2124 KB |
Output is correct |
27 |
Correct |
14 ms |
2124 KB |
Output is correct |
28 |
Correct |
6 ms |
2112 KB |
Output is correct |
29 |
Correct |
16 ms |
2124 KB |
Output is correct |
30 |
Correct |
7 ms |
2184 KB |
Output is correct |
31 |
Correct |
15 ms |
2124 KB |
Output is correct |
32 |
Correct |
7 ms |
2124 KB |
Output is correct |
33 |
Correct |
14 ms |
2200 KB |
Output is correct |
34 |
Correct |
7 ms |
2124 KB |
Output is correct |
35 |
Correct |
15 ms |
2192 KB |
Output is correct |
36 |
Correct |
7 ms |
2124 KB |
Output is correct |
37 |
Correct |
15 ms |
2180 KB |
Output is correct |
38 |
Correct |
7 ms |
2124 KB |
Output is correct |
39 |
Correct |
15 ms |
2184 KB |
Output is correct |
40 |
Correct |
7 ms |
2124 KB |
Output is correct |
41 |
Correct |
15 ms |
2124 KB |
Output is correct |
42 |
Correct |
7 ms |
2124 KB |
Output is correct |
43 |
Correct |
15 ms |
2124 KB |
Output is correct |
44 |
Correct |
7 ms |
2176 KB |
Output is correct |
45 |
Correct |
15 ms |
2196 KB |
Output is correct |
46 |
Correct |
7 ms |
2124 KB |
Output is correct |
47 |
Correct |
5 ms |
2124 KB |
Output is correct |
48 |
Correct |
10 ms |
2124 KB |
Output is correct |
49 |
Correct |
8 ms |
2124 KB |
Output is correct |
50 |
Correct |
7 ms |
2124 KB |
Output is correct |
51 |
Correct |
6 ms |
2172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3872 KB |
Output is correct |
2 |
Correct |
25 ms |
3780 KB |
Output is correct |
3 |
Correct |
40 ms |
3916 KB |
Output is correct |
4 |
Correct |
23 ms |
3792 KB |
Output is correct |
5 |
Correct |
40 ms |
3896 KB |
Output is correct |
6 |
Correct |
22 ms |
3788 KB |
Output is correct |
7 |
Correct |
28 ms |
3788 KB |
Output is correct |
8 |
Correct |
17 ms |
3896 KB |
Output is correct |
9 |
Correct |
27 ms |
3896 KB |
Output is correct |
10 |
Correct |
19 ms |
3856 KB |
Output is correct |
11 |
Correct |
27 ms |
3780 KB |
Output is correct |
12 |
Correct |
18 ms |
3788 KB |
Output is correct |
13 |
Correct |
33 ms |
3912 KB |
Output is correct |
14 |
Correct |
29 ms |
3908 KB |
Output is correct |
15 |
Correct |
18 ms |
3816 KB |
Output is correct |
16 |
Correct |
31 ms |
3916 KB |
Output is correct |
17 |
Correct |
17 ms |
3880 KB |
Output is correct |
18 |
Correct |
27 ms |
3860 KB |
Output is correct |
19 |
Correct |
17 ms |
3776 KB |
Output is correct |
20 |
Correct |
28 ms |
3788 KB |
Output is correct |
21 |
Correct |
18 ms |
3852 KB |
Output is correct |
22 |
Correct |
34 ms |
3872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1720 KB |
Output is correct |
2 |
Correct |
2 ms |
1740 KB |
Output is correct |
3 |
Correct |
3 ms |
1740 KB |
Output is correct |
4 |
Correct |
2 ms |
1740 KB |
Output is correct |
5 |
Correct |
2 ms |
1720 KB |
Output is correct |
6 |
Correct |
2 ms |
1740 KB |
Output is correct |
7 |
Correct |
2 ms |
1716 KB |
Output is correct |
8 |
Correct |
2 ms |
1720 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1720 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1720 KB |
Output is correct |
13 |
Correct |
2 ms |
1720 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1716 KB |
Output is correct |
20 |
Correct |
2 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1612 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1740 KB |
Output is correct |
24 |
Correct |
2 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
1724 KB |
Output is correct |
26 |
Correct |
7 ms |
2124 KB |
Output is correct |
27 |
Correct |
14 ms |
2124 KB |
Output is correct |
28 |
Correct |
6 ms |
2112 KB |
Output is correct |
29 |
Correct |
16 ms |
2124 KB |
Output is correct |
30 |
Correct |
7 ms |
2184 KB |
Output is correct |
31 |
Correct |
15 ms |
2124 KB |
Output is correct |
32 |
Correct |
7 ms |
2124 KB |
Output is correct |
33 |
Correct |
14 ms |
2200 KB |
Output is correct |
34 |
Correct |
7 ms |
2124 KB |
Output is correct |
35 |
Correct |
15 ms |
2192 KB |
Output is correct |
36 |
Correct |
7 ms |
2124 KB |
Output is correct |
37 |
Correct |
15 ms |
2180 KB |
Output is correct |
38 |
Correct |
7 ms |
2124 KB |
Output is correct |
39 |
Correct |
15 ms |
2184 KB |
Output is correct |
40 |
Correct |
7 ms |
2124 KB |
Output is correct |
41 |
Correct |
15 ms |
2124 KB |
Output is correct |
42 |
Correct |
7 ms |
2124 KB |
Output is correct |
43 |
Correct |
15 ms |
2124 KB |
Output is correct |
44 |
Correct |
7 ms |
2176 KB |
Output is correct |
45 |
Correct |
15 ms |
2196 KB |
Output is correct |
46 |
Correct |
7 ms |
2124 KB |
Output is correct |
47 |
Correct |
5 ms |
2124 KB |
Output is correct |
48 |
Correct |
10 ms |
2124 KB |
Output is correct |
49 |
Correct |
8 ms |
2124 KB |
Output is correct |
50 |
Correct |
7 ms |
2124 KB |
Output is correct |
51 |
Correct |
6 ms |
2172 KB |
Output is correct |
52 |
Correct |
27 ms |
3872 KB |
Output is correct |
53 |
Correct |
25 ms |
3780 KB |
Output is correct |
54 |
Correct |
40 ms |
3916 KB |
Output is correct |
55 |
Correct |
23 ms |
3792 KB |
Output is correct |
56 |
Correct |
40 ms |
3896 KB |
Output is correct |
57 |
Correct |
22 ms |
3788 KB |
Output is correct |
58 |
Correct |
28 ms |
3788 KB |
Output is correct |
59 |
Correct |
17 ms |
3896 KB |
Output is correct |
60 |
Correct |
27 ms |
3896 KB |
Output is correct |
61 |
Correct |
19 ms |
3856 KB |
Output is correct |
62 |
Correct |
27 ms |
3780 KB |
Output is correct |
63 |
Correct |
18 ms |
3788 KB |
Output is correct |
64 |
Correct |
33 ms |
3912 KB |
Output is correct |
65 |
Correct |
29 ms |
3908 KB |
Output is correct |
66 |
Correct |
18 ms |
3816 KB |
Output is correct |
67 |
Correct |
31 ms |
3916 KB |
Output is correct |
68 |
Correct |
17 ms |
3880 KB |
Output is correct |
69 |
Correct |
27 ms |
3860 KB |
Output is correct |
70 |
Correct |
17 ms |
3776 KB |
Output is correct |
71 |
Correct |
28 ms |
3788 KB |
Output is correct |
72 |
Correct |
18 ms |
3852 KB |
Output is correct |
73 |
Correct |
34 ms |
3872 KB |
Output is correct |
74 |
Correct |
103 ms |
8472 KB |
Output is correct |
75 |
Correct |
668 ms |
8828 KB |
Output is correct |
76 |
Correct |
115 ms |
8508 KB |
Output is correct |
77 |
Correct |
614 ms |
8836 KB |
Output is correct |
78 |
Correct |
111 ms |
8516 KB |
Output is correct |
79 |
Correct |
115 ms |
8504 KB |
Output is correct |
80 |
Correct |
631 ms |
8872 KB |
Output is correct |
81 |
Correct |
154 ms |
8776 KB |
Output is correct |
82 |
Correct |
78 ms |
8844 KB |
Output is correct |
83 |
Correct |
109 ms |
8500 KB |
Output is correct |
84 |
Correct |
757 ms |
8908 KB |
Output is correct |
85 |
Correct |
107 ms |
8516 KB |
Output is correct |
86 |
Correct |
615 ms |
8924 KB |
Output is correct |
87 |
Correct |
118 ms |
8572 KB |
Output is correct |
88 |
Correct |
617 ms |
8884 KB |
Output is correct |
89 |
Correct |
102 ms |
8500 KB |
Output is correct |
90 |
Correct |
582 ms |
8892 KB |
Output is correct |
91 |
Correct |
103 ms |
8644 KB |
Output is correct |
92 |
Correct |
732 ms |
8880 KB |
Output is correct |
93 |
Correct |
109 ms |
8516 KB |
Output is correct |
94 |
Correct |
57 ms |
8216 KB |
Output is correct |
95 |
Correct |
157 ms |
8520 KB |
Output is correct |
96 |
Correct |
129 ms |
8580 KB |
Output is correct |
97 |
Correct |
100 ms |
8884 KB |
Output is correct |
98 |
Correct |
134 ms |
8884 KB |
Output is correct |