# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
491625 |
2021-12-03T14:48:38 Z |
blue |
Logičari (COCI21_logicari) |
C++17 |
|
93 ms |
26588 KB |
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const long long max_n = 100'000;
long long n;
const long long INF = 1'000'000;
long long dp[1+max_n][4];
vector<long long> children[1+max_n];
vector<long long> edge[1+max_n];
vector<long long> degree(1+max_n, 0);
void compute(long long u)
{
// cerr << "compute " << u << '\n';
vector<long long> sm(4, 0);
long long min13 = INF;
long long min02 = INF;
for(long long v: children[u])
{
for(long long k = 0; k < 4; k++)
{
sm[k] += dp[v][k];
}
min13 = min(min13, dp[v][1] - dp[v][3]);
min02 = min(min02, dp[v][0] - dp[v][2]);
}
// cerr << sm[0] << ' ' << sm[1] << ' ' << sm[2] << ' ' << sm[3] << ' ' << min13 << ' ' << min02 << '\n';
dp[u][0] = 1 + sm[3] + min13;
dp[u][1] = 1 + sm[3];
dp[u][2] = sm[2] + min02;
dp[u][3] = sm[2];
for(long long k = 0; k < 4; k++) if(dp[u][k] >= INF) dp[u][k] = INF;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(long long e = 1; e <= n; e++)
{
long long a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
degree[a]++;
degree[b]++;
}
queue<long long> tbv;
for(long long i = 1; i <= n; i++)
{
if(degree[i] == 1) tbv.push(i);
}
vector<bool> visit(1+n, 0);
while(!tbv.empty())
{
long long u = tbv.front();
// cerr << "bfs " << u << '\n';
tbv.pop();
visit[u] = 1;
for(long long v: edge[u])
{
if(degree[v] == 1) continue;
children[v].push_back(u);
degree[v]--;
if(degree[v] == 1)
tbv.push(v);
}
compute(u);
}
long long s = 1;
while(visit[s]) s++;
vector<long long> chain;
// cerr << "s = " << s << '\n';
// cerr << "check\n";
while(1)
{
chain.push_back(s);
visit[s] = 1;
long long nv = -1;
for(long long v: edge[s])
{
if(visit[v]) continue;
nv = v;
break;
}
compute(s);
// cerr << s << ' ' << nv << '\n';
if(nv == -1) break;
s = nv;
}
// for(long long i = 1; i <= n; i++)
// {
// cerr << i << ": ";
// for(long long j = 0; j < 4; j++) cerr << dp[i][j] << ' ';
// cerr << '\n';
// }
// for(long long c: chain) cerr << c << ' ';
// cerr << '\n';
long long q = (long long)chain.size();
//current index, current state, state -1
long long dp2[q][4][4];
/*
0 = satisfied blue
1 = unsatisfied blue
2 = satisfied red
3 = unsatisfied red
*/
for(long long x = 0; x < 4; x++)
for(long long y = 0; y < 4; y++)
dp2[0][x][y] = INF;
dp2[0][0][3] = dp[chain[0]][0];
dp2[0][0][1] = dp[chain[0]][1];
dp2[0][1][3] = dp[chain[0]][1];
dp2[0][2][2] = dp[chain[0]][2];
dp2[0][2][0] = dp[chain[0]][3];
dp2[0][3][2] = dp[chain[0]][3];
for(long long i = 1; i < q; i++)
{
for(long long y = 0; y < 4; y++)
{
dp2[i][0][y] = min(dp[chain[i]][0] + dp2[i-1][3][y], dp[chain[i]][1] + dp2[i-1][1][y]);
dp2[i][1][y] = dp[chain[i]][1] + dp2[i-1][3][y];
dp2[i][2][y] = min(dp[chain[i]][2] + dp2[i-1][2][y], dp[chain[i]][3] + dp2[i-1][0][y]);
dp2[i][3][y] = dp[chain[i]][3] + dp2[i-1][2][y];
}
}
long long ans = INF;
for(long long x = 0; x < 4; x++)
ans = min(ans, dp2[q-1][x][x]);
if(ans == INF) ans = -1;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
93 ms |
26556 KB |
Output is correct |
6 |
Correct |
82 ms |
26588 KB |
Output is correct |
7 |
Correct |
73 ms |
26556 KB |
Output is correct |
8 |
Correct |
79 ms |
26584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
3 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5796 KB |
Output is correct |
8 |
Correct |
3 ms |
5708 KB |
Output is correct |
9 |
Correct |
3 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
5708 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
3 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5796 KB |
Output is correct |
8 |
Correct |
3 ms |
5708 KB |
Output is correct |
9 |
Correct |
3 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
5708 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5836 KB |
Output is correct |
14 |
Correct |
3 ms |
5836 KB |
Output is correct |
15 |
Correct |
3 ms |
5836 KB |
Output is correct |
16 |
Correct |
3 ms |
5836 KB |
Output is correct |
17 |
Correct |
4 ms |
5820 KB |
Output is correct |
18 |
Correct |
4 ms |
5820 KB |
Output is correct |
19 |
Correct |
3 ms |
5824 KB |
Output is correct |
20 |
Correct |
4 ms |
5836 KB |
Output is correct |
21 |
Correct |
3 ms |
5836 KB |
Output is correct |
22 |
Correct |
4 ms |
5836 KB |
Output is correct |
23 |
Correct |
3 ms |
5836 KB |
Output is correct |
24 |
Correct |
3 ms |
5836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
93 ms |
26556 KB |
Output is correct |
6 |
Correct |
82 ms |
26588 KB |
Output is correct |
7 |
Correct |
73 ms |
26556 KB |
Output is correct |
8 |
Correct |
79 ms |
26584 KB |
Output is correct |
9 |
Correct |
3 ms |
5708 KB |
Output is correct |
10 |
Correct |
3 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5708 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
3 ms |
5708 KB |
Output is correct |
15 |
Correct |
3 ms |
5796 KB |
Output is correct |
16 |
Correct |
3 ms |
5708 KB |
Output is correct |
17 |
Correct |
3 ms |
5708 KB |
Output is correct |
18 |
Correct |
3 ms |
5708 KB |
Output is correct |
19 |
Correct |
3 ms |
5708 KB |
Output is correct |
20 |
Correct |
3 ms |
5708 KB |
Output is correct |
21 |
Correct |
3 ms |
5836 KB |
Output is correct |
22 |
Correct |
3 ms |
5836 KB |
Output is correct |
23 |
Correct |
3 ms |
5836 KB |
Output is correct |
24 |
Correct |
3 ms |
5836 KB |
Output is correct |
25 |
Correct |
4 ms |
5820 KB |
Output is correct |
26 |
Correct |
4 ms |
5820 KB |
Output is correct |
27 |
Correct |
3 ms |
5824 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
3 ms |
5836 KB |
Output is correct |
30 |
Correct |
4 ms |
5836 KB |
Output is correct |
31 |
Correct |
3 ms |
5836 KB |
Output is correct |
32 |
Correct |
3 ms |
5836 KB |
Output is correct |
33 |
Correct |
83 ms |
14564 KB |
Output is correct |
34 |
Correct |
84 ms |
14496 KB |
Output is correct |
35 |
Correct |
68 ms |
14580 KB |
Output is correct |
36 |
Correct |
69 ms |
14484 KB |
Output is correct |
37 |
Correct |
13 ms |
8012 KB |
Output is correct |
38 |
Correct |
66 ms |
14664 KB |
Output is correct |
39 |
Correct |
6 ms |
6476 KB |
Output is correct |
40 |
Correct |
79 ms |
14472 KB |
Output is correct |
41 |
Correct |
41 ms |
15568 KB |
Output is correct |
42 |
Correct |
44 ms |
15856 KB |
Output is correct |
43 |
Correct |
68 ms |
16252 KB |
Output is correct |
44 |
Correct |
70 ms |
15512 KB |
Output is correct |