Submission #235948

# Submission time Handle Problem Language Result Execution time Memory
235948 2020-05-30T13:00:12 Z Vimmer Usmjeri (COCI17_usmjeri) C++14
0 / 140
88 ms 91272 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")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 300001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
//using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


vector <array <int, 2> > g[N];

bool mkr[N];

int pr[N], rk[N], t[N + N][20], in[N], tin[N], tout[N], id, h[N], logs[N + N], mnr[N];

int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}

void link(int x, int y)
{
    if (rk[x] < rk[y]) swap(x, y);

    pr[y] = x;

    rk[x] += rk[y];
}

vector <int> path[N], vr, to[N], glub[N], lnk[N], a[N];

ll ans = 1;

array <int, 2> pred[N];

void dfser(int v, int p)
{
    mkr[v] = 1;

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

        if (mkr[it]) {cout << 0 << endl; exit(0);}

        dfser(it, v);
    }
}

void spusk(int v, int p)
{
    tin[v] = id++;

    if (p != -1) h[v] = h[p] + 1; else pred[v] = {-1, -1};

    glub[h[v]].pb(v);

    in[v] = sz(vr);

    vr.pb(v);

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

        spusk(it[0], v);

        pred[it[0]] = {v, it[1]};

        vr.pb(v);
    }

    tout[v] = id++;
}

int lca(int a, int b)
{
    if (in[a] > in[b]) swap(a, b);

    int j = logs[in[b] - in[a] + 1];

    a = t[in[a]][j];

    b = t[in[b] - (1 << j) + 1][j];

    if (h[a] > h[b]) return b;

    return a;
}

int main()
{
    freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

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

    int n, m;

    cin >> n >> m;

    for (int i = 2; i < N + N; i++) logs[i] = 1 + logs[i >> 1];

    bool f = 1;

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

        cin >> x >> y;

        g[x].pb({y, i});

        g[y].pb({x, i});

        pr[i] = i;

        rk[i] = 1;
    }

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

    if (f)
    {
        int beg;

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

            for (; m > 0 ; m--)
            {
                int x, y;

                cin >> x >> y;

                a[x].pb(m);

                a[y].pb(m);
            }

            vector <int> vr; vr.clear();

            int v = beg, p = -1;

            while (v != -1)
            {
                int nxt = -1;

                vr.pb(v);

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

                    nxt = it[0];
                }

                p = v;

                v = nxt;
            }

            ll ans = 1;

            set <int> se; se.clear();

            for (int i = 0; i < sz(vr); i++)
            {
                bool f = 1;

                if (i != 0 && sz(se) == 0) {f = 0; ans = (ans + ans) % M;}

                for (auto it : a[vr[i]])
                {
                    if (se.find(it) == se.end()) se.insert(it);
                      else se.erase(it);
                }

                if (i != 0 && f && sz(se) == 0) ans = (ans + ans) % M;
            }
            cout << ans << endl;

            exit(0);
    }

    spusk(1, -1);

    for (int i = 0; i < sz(vr); i++) t[i][0] = vr[i];

    for (int j = 1; j < 20; j++)
        for (int i = 0; i < sz(vr); i++)
        {
            int a = t[i][j - 1];

            int b = t[min(sz(vr) - 1, i + (1 << (j - 1)))][j - 1];

            if (h[a] > h[b]) t[i][j] = b; else t[i][j] = a;
        }

    for (int i = 1; i <= n; i++) mnr[i] = -1;

    for (int i = 0; i < m; i++)
    {
        int x, y;

        cin >> x >> y;

        path[x].pb(y);

        path[y].pb(x);

        int lc = lca(x, y);

        if (lc == x) to[y].pb(x);
          else if (lc == y) to[x].pb(y);
            else {to[x].pb(lc); to[y].pb(lc);}
    }

//    for (int i = 1; i <= n; i++)
//    {
//        if (mkr[i]) continue;
//
//        dfser(i, -1);
//    }

    memset(mkr, 0, sizeof(mkr));

    for (int i = N - 1; i >= 0; i--)
        for (auto v : glub[i])
        {
            int root = -1;

            if (sz(lnk[v]) != 0)
            {
                root = lnk[v][0];

                for (auto it : lnk[v])
                {
                    int x = fnd(root), y = fnd(it);

                    if (x != y) link(x, y);
                }
            }

            int mn = -1;

            for (auto it : to[v])
                if (mn == -1 || h[mn] > h[it]) mn = it;

            if (mnr[v] != -1 && (mn == -1 || h[mn] > h[mnr[v]])) mn = mnr[v];

            if (mn == v || mn == -1) continue;

            int nxt = pred[v][0], nm = pred[v][1];

            if (nxt == -1) continue;

            lnk[nxt].pb(nm);

            if (mnr[nxt] == -1 || h[mnr[nxt]] > h[mn]) mnr[nxt] = mn;

            if (mnr[v] != -1 && mnr[v] != v)
            {
                int x = fnd(root), y = fnd(nm);

                if (x != y) link(x, y);
            }
        }

    for (int i = 0; i < n - 1; i++)
    {
        int id = fnd(i);

        if (mkr[id]) continue;

        mkr[id] = 1;

        ans = (ans + ans) % M;
    }

    cout << ans << endl;
}

Compilation message

usmjeri.cpp: In function 'int main()':
usmjeri.cpp:108:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
usmjeri.cpp:156:17: warning: 'beg' may be used uninitialized in this function [-Wmaybe-uninitialized]
             int v = beg, p = -1;
                 ^
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 91000 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 87 ms 91128 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 91128 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 82 ms 91132 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 87 ms 91036 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 88 ms 91000 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 86 ms 91272 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 91128 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 84 ms 91004 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 91048 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -