Submission #374354

# Submission time Handle Problem Language Result Execution time Memory
374354 2021-03-07T08:10:35 Z Vimmer Svjetlo (COCI20_svjetlo) C++14
0 / 110
215 ms 50176 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")

#define N 500005
#define NN 1005000
#define PB push_back
#define M ll(1e9 + 7)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pri(x) cout << x << endl
#define endl '\n'
#define _ << " " <<
#define F first
#define S second

using namespace std;
//using namespace __gnu_pbds;

//typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> oredered_set;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef short int si;

int n, ans = 0;

vector <int> g[N], vr;

string s;

void dfs(int v, int p)
{
    vr.PB(v);

    for (auto it : g[v])
    {
        if (it == p) continue;

        dfs(it, v);
    }

}

void sub_2()
{
    int ans = 0, v;

    for (int i = 1; i <= n; i++)
        if (sz(g[i]) == 1)
          v = i;

    string t = s;

    dfs(v, -1);

    int l = -1, r = -1e9;

    for (int i = 0; i < n; i++)
        if (s[vr[i] - 1] == '0')
        {
            r = i;

            if (l == -1)
                l = i;
        }

    for (int i = l; i <= r; i++)
    {
        int v = vr[i] - 1;

        ans++;

        if (t[v] == '0')
        {
            t[v] = '1';

            continue;
        }

        ans += 2;

        v = vr[i + 1] - 1;

        if (t[v] == '1')
            t[v] = '0';
                else t[v] = '1';

        if (i + 1 == r)
            break;
    }

    reverse(all(vr));

    l = -1;

    r = -1e9;

    for (int i = 0; i < n; i++)
        if (s[vr[i] - 1] == '0')
        {
            r = i;

            if (l == -1)
                l = i;
        }

    int b = 0;

    for (int i = l; i <= r; i++)
    {
        int v = vr[i] - 1;

        b++;

        if (s[v] == '0')
        {
            s[v] = '1';

            continue;
        }

        b += 2;

        v = vr[i + 1] - 1;

        if (s[v] == '1')
            s[v] = '0';
                else s[v] = '1';

        if (i + 1 == r)
            break;
    }

    pri(min(b, ans));

    exit(0);
}

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    //freopen("1.in", "r", stdin);

    cin >> n;

    cin >> s;

    for (int i = 1; i < n; i++)
    {
        int x, y;

        cin >> x >> y;

        g[x].PB(y);

        g[y].PB(x);
    }

    bool ft = 1;

    for (int i = 1; i <= n; i++)
        if (sz(g[i]) > 2)
        {
            ft = 0;

            break;
        }

    if (ft)
        sub_2();

    assert(0);
}

Compilation message

svjetlo.cpp: In function 'void sub_2()':
svjetlo.cpp:60:8: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
   60 |     dfs(v, -1);
      |     ~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 215 ms 39284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 206 ms 50176 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -