#include <bits/stdc++.h>
using namespace std;
int n,k,dif0;
multiset<pair<int,int>> g[60005];
bool viz[60005];
int vll;
void dfs(int nod,int parval)
{
//cout << nod << ' ' << vll << endl;
viz[nod] = true;
for (auto it : g[nod])
{
if (!viz[it.first])
{
if (nod <= n)
vll += it.second;
else
vll -= it.second;
dfs(it.first,it.second);
return;
}
}
for (auto it : g[nod])
{
if (nod <= n)
vll += it.second;
else
vll -= it.second;
}
if (nod <= n)
vll -= parval;
else
vll += parval;
}
bitset<1200005> dp;///shiftat cu 600000
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= 2 * n; i++)
{
int x,y,z;
cin >> x >> y >> z;
g[x].insert({y + n,z});
g[y + n].insert({x,z});
}
set<pair<int,int>> degmin;
for (int i = 1; i <= 2 * n; i++)
{
degmin.insert({g[i].size(),i});
}
while (!degmin.empty() and (*degmin.begin()).first < 2)
{
pair<int,int> pr = *degmin.begin();
degmin.erase(degmin.begin());
if (pr.first == 0)
{
cout << "NO";
return 0;
}
int nod = pr.second;
int vecin = (*g[nod].begin()).first;
int cost = (*g[nod].begin()).second;
if (nod <= n)
dif0 += cost;
else
dif0 -= cost;
g[nod].clear();
g[vecin].erase(g[vecin].find({nod,cost}));
int dgg = (int)g[vecin].size() + 1;
degmin.erase({dgg,vecin});
degmin.insert({dgg - 1,vecin});
}
vector<int> noduri;
for (auto it : degmin)
noduri.push_back(it.second);
vector<int> vals;
for (auto it : noduri)
{
if (!viz[it])
{
vll = 0;
dfs(it,0);
vals.push_back(abs(vll));
//cout << abs(vll) << endl;
}
}
dp[dif0 + 600000] = 1;
//cout << dif0 << endl;
for (auto it : vals)
{
dp = (dp << it) | (dp >> it);
}
for (int i = 600000 - k; i <= 600000 + k; i++)
{
if (dp[i])
{
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
/**
Fac un graf bipartit cu muchie L[i] -- R[j] pentru un om cu i sau j
deg(i) = 0 => NO
deg(i) = 1 => fac muchia aia sa fie asa si o elimin din graf, impreuna cu nodul evident
daca orice deg(i) >= 2, sum(deg(i)) >= 4N dar sum(deg(i)) == 4N deci deg(i) = 2 pentru fiecare i
graful poate fi impartit in cicluri, la fiecare ciclu am doua variante
Fie D = diferenta care s-a creat pana sa ajung la graful asta din cicluri
Raspunsul pentru s[i] = 1 este clar YES, acum sa vad cand am si k si s-uri diferite
Fiecare ciclu poate sa imi genereze doua valori, a[i] si b[i] sa zicem (fie m numarul de cicluri)
dp[i][j] = pot sa fac din primele i cicluri valoarea j (M * N * 20 de stari)
M e O(N) worst case (gen, N / 2) deci tot pot avea N^2 * 10 stari
Average bitset W lol, gen acum am aprox N^2 / 6 sau cv care ar trebui sa intre oricum
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3672 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 |
3676 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 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 |
3672 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
2 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 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 |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 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 |
3672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3672 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 |
3676 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 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 |
3672 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
2 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 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 |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 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 |
3672 KB |
Output is correct |
26 |
Correct |
106 ms |
4444 KB |
Output is correct |
27 |
Correct |
19 ms |
4700 KB |
Output is correct |
28 |
Correct |
106 ms |
4444 KB |
Output is correct |
29 |
Correct |
22 ms |
4440 KB |
Output is correct |
30 |
Correct |
106 ms |
4444 KB |
Output is correct |
31 |
Correct |
17 ms |
4440 KB |
Output is correct |
32 |
Correct |
106 ms |
4524 KB |
Output is correct |
33 |
Correct |
17 ms |
4440 KB |
Output is correct |
34 |
Correct |
11 ms |
4444 KB |
Output is correct |
35 |
Correct |
18 ms |
4444 KB |
Output is correct |
36 |
Correct |
108 ms |
4444 KB |
Output is correct |
37 |
Correct |
17 ms |
4444 KB |
Output is correct |
38 |
Correct |
109 ms |
4512 KB |
Output is correct |
39 |
Correct |
18 ms |
4440 KB |
Output is correct |
40 |
Correct |
111 ms |
4692 KB |
Output is correct |
41 |
Correct |
17 ms |
4440 KB |
Output is correct |
42 |
Correct |
106 ms |
4440 KB |
Output is correct |
43 |
Correct |
17 ms |
4444 KB |
Output is correct |
44 |
Correct |
105 ms |
4444 KB |
Output is correct |
45 |
Correct |
17 ms |
4444 KB |
Output is correct |
46 |
Correct |
106 ms |
4528 KB |
Output is correct |
47 |
Correct |
3 ms |
4188 KB |
Output is correct |
48 |
Correct |
55 ms |
4280 KB |
Output is correct |
49 |
Correct |
56 ms |
4508 KB |
Output is correct |
50 |
Correct |
119 ms |
4440 KB |
Output is correct |
51 |
Correct |
106 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
7080 KB |
Output is correct |
2 |
Correct |
10 ms |
6744 KB |
Output is correct |
3 |
Correct |
289 ms |
7084 KB |
Output is correct |
4 |
Correct |
11 ms |
6744 KB |
Output is correct |
5 |
Correct |
297 ms |
7076 KB |
Output is correct |
6 |
Correct |
16 ms |
6744 KB |
Output is correct |
7 |
Correct |
290 ms |
7080 KB |
Output is correct |
8 |
Correct |
9 ms |
6744 KB |
Output is correct |
9 |
Correct |
294 ms |
7080 KB |
Output is correct |
10 |
Correct |
10 ms |
6748 KB |
Output is correct |
11 |
Correct |
291 ms |
7080 KB |
Output is correct |
12 |
Correct |
10 ms |
6744 KB |
Output is correct |
13 |
Correct |
295 ms |
7004 KB |
Output is correct |
14 |
Correct |
282 ms |
7080 KB |
Output is correct |
15 |
Correct |
10 ms |
6744 KB |
Output is correct |
16 |
Correct |
284 ms |
7004 KB |
Output is correct |
17 |
Correct |
10 ms |
6748 KB |
Output is correct |
18 |
Correct |
285 ms |
7156 KB |
Output is correct |
19 |
Correct |
9 ms |
6748 KB |
Output is correct |
20 |
Correct |
273 ms |
7084 KB |
Output is correct |
21 |
Correct |
10 ms |
6744 KB |
Output is correct |
22 |
Correct |
154 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3672 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 |
3676 KB |
Output is correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 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 |
3672 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
2 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 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 |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 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 |
3672 KB |
Output is correct |
26 |
Correct |
106 ms |
4444 KB |
Output is correct |
27 |
Correct |
19 ms |
4700 KB |
Output is correct |
28 |
Correct |
106 ms |
4444 KB |
Output is correct |
29 |
Correct |
22 ms |
4440 KB |
Output is correct |
30 |
Correct |
106 ms |
4444 KB |
Output is correct |
31 |
Correct |
17 ms |
4440 KB |
Output is correct |
32 |
Correct |
106 ms |
4524 KB |
Output is correct |
33 |
Correct |
17 ms |
4440 KB |
Output is correct |
34 |
Correct |
11 ms |
4444 KB |
Output is correct |
35 |
Correct |
18 ms |
4444 KB |
Output is correct |
36 |
Correct |
108 ms |
4444 KB |
Output is correct |
37 |
Correct |
17 ms |
4444 KB |
Output is correct |
38 |
Correct |
109 ms |
4512 KB |
Output is correct |
39 |
Correct |
18 ms |
4440 KB |
Output is correct |
40 |
Correct |
111 ms |
4692 KB |
Output is correct |
41 |
Correct |
17 ms |
4440 KB |
Output is correct |
42 |
Correct |
106 ms |
4440 KB |
Output is correct |
43 |
Correct |
17 ms |
4444 KB |
Output is correct |
44 |
Correct |
105 ms |
4444 KB |
Output is correct |
45 |
Correct |
17 ms |
4444 KB |
Output is correct |
46 |
Correct |
106 ms |
4528 KB |
Output is correct |
47 |
Correct |
3 ms |
4188 KB |
Output is correct |
48 |
Correct |
55 ms |
4280 KB |
Output is correct |
49 |
Correct |
56 ms |
4508 KB |
Output is correct |
50 |
Correct |
119 ms |
4440 KB |
Output is correct |
51 |
Correct |
106 ms |
4444 KB |
Output is correct |
52 |
Correct |
280 ms |
7080 KB |
Output is correct |
53 |
Correct |
10 ms |
6744 KB |
Output is correct |
54 |
Correct |
289 ms |
7084 KB |
Output is correct |
55 |
Correct |
11 ms |
6744 KB |
Output is correct |
56 |
Correct |
297 ms |
7076 KB |
Output is correct |
57 |
Correct |
16 ms |
6744 KB |
Output is correct |
58 |
Correct |
290 ms |
7080 KB |
Output is correct |
59 |
Correct |
9 ms |
6744 KB |
Output is correct |
60 |
Correct |
294 ms |
7080 KB |
Output is correct |
61 |
Correct |
10 ms |
6748 KB |
Output is correct |
62 |
Correct |
291 ms |
7080 KB |
Output is correct |
63 |
Correct |
10 ms |
6744 KB |
Output is correct |
64 |
Correct |
295 ms |
7004 KB |
Output is correct |
65 |
Correct |
282 ms |
7080 KB |
Output is correct |
66 |
Correct |
10 ms |
6744 KB |
Output is correct |
67 |
Correct |
284 ms |
7004 KB |
Output is correct |
68 |
Correct |
10 ms |
6748 KB |
Output is correct |
69 |
Correct |
285 ms |
7156 KB |
Output is correct |
70 |
Correct |
9 ms |
6748 KB |
Output is correct |
71 |
Correct |
273 ms |
7084 KB |
Output is correct |
72 |
Correct |
10 ms |
6744 KB |
Output is correct |
73 |
Correct |
154 ms |
7260 KB |
Output is correct |
74 |
Correct |
1590 ms |
13664 KB |
Output is correct |
75 |
Correct |
94 ms |
13524 KB |
Output is correct |
76 |
Correct |
1597 ms |
13784 KB |
Output is correct |
77 |
Correct |
100 ms |
13420 KB |
Output is correct |
78 |
Correct |
1615 ms |
13656 KB |
Output is correct |
79 |
Correct |
1563 ms |
13656 KB |
Output is correct |
80 |
Correct |
91 ms |
13524 KB |
Output is correct |
81 |
Correct |
109 ms |
13420 KB |
Output is correct |
82 |
Correct |
1592 ms |
13664 KB |
Output is correct |
83 |
Correct |
1611 ms |
13664 KB |
Output is correct |
84 |
Correct |
94 ms |
13516 KB |
Output is correct |
85 |
Correct |
1621 ms |
13660 KB |
Output is correct |
86 |
Correct |
93 ms |
13536 KB |
Output is correct |
87 |
Correct |
1608 ms |
13656 KB |
Output is correct |
88 |
Correct |
97 ms |
13528 KB |
Output is correct |
89 |
Correct |
1569 ms |
13660 KB |
Output is correct |
90 |
Correct |
93 ms |
13528 KB |
Output is correct |
91 |
Correct |
1692 ms |
13668 KB |
Output is correct |
92 |
Correct |
105 ms |
13412 KB |
Output is correct |
93 |
Correct |
1658 ms |
13664 KB |
Output is correct |
94 |
Correct |
39 ms |
12876 KB |
Output is correct |
95 |
Correct |
847 ms |
13400 KB |
Output is correct |
96 |
Correct |
865 ms |
13412 KB |
Output is correct |
97 |
Correct |
1691 ms |
13660 KB |
Output is correct |
98 |
Correct |
1715 ms |
13656 KB |
Output is correct |