Submission #235467

# Submission time Handle Problem Language Result Execution time Memory
235467 2020-05-28T10:34:30 Z VEGAnn Usmjeri (COCI17_usmjeri) C++14
28 / 140
617 ms 63992 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define MP make_pair
#define ft first
#define sd second
#define PB push_back
#define pli pair<ll,int>
using namespace std;
typedef long long ll;
const int N = 300100;
const int PW = 20;
const int oo = 2e9;
const int md = int(1e9) + 7;
vector<int> g[N];
int n, m, pr[N], ans = 1, up[N][PW], tin[N], tout[N], tt = 0, ad[N];
bool mrk[N];

int get(int x) { return (pr[x] == x ? x : pr[x] = get(pr[x])); }

void dfs(int v, int p){
    up[v][0] = p;

    for (int po = 1; po < PW; po++)
        up[v][po] = up[up[v][po - 1]][po - 1];

    tin[v] = ++tt;

    for (int u : g[v]){
        if (p == u) continue;

        dfs(u, v);
    }

    tout[v] = ++tt;
}

bool upper(int a, int b){
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}

int lca(int a, int b){
    if (upper(a, b)) return a;
    if (upper(b, a)) return b;

    for (int po = PW - 1; po >= 0; po--)
        if (!upper(up[a][po], b))
            a = up[a][po];

    return up[a][0];
}

int pra(int a, int b){
    for (int po = PW - 1; po >= 0; po--)
        if (!upper(up[a][po], b))
            a = up[a][po];

    return a;
}

void DFS(int v){
    for (int u : g[v]){
        if (u == up[v][0]) continue;

        DFS(u);

        ad[v] += ad[u];
    }

    if (ad[v] > 0)
        pr[get(v)] = get(up[v][0]);
}

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

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n >> m;

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

        g[x].PB(y);
        g[y].PB(x);

        pr[i] = i;
    }

    dfs(0, 0);

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

        int lc = lca(x, y);

        if (x == lc){
            ad[y]++;
            ad[pra(y, x)]--;
        } else if (y == lc){
            ad[x]++;
            ad[pra(x, y)]--;
        } else {
            ad[x]++;
            ad[y]++;
            ad[pra(x, y)]--;
            ad[pra(y, x)]--;

            pr[get(pra(x, y))] = get(pra(y, x));
        }
    }

    DFS(0);

    for (int i = 1; i < n; i++){
        int cur = get(i);

        if (mrk[cur]) continue;

        mrk[cur] = 1;

        ans += ans;

        if (ans >= md)
            ans -= md;
    }

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 155 ms 26232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 63992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Incorrect 10 ms 7680 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Incorrect 10 ms 7680 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8064 KB Output is correct
2 Incorrect 14 ms 8064 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8064 KB Output is correct
2 Incorrect 12 ms 8168 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 476 ms 45816 KB Output is correct
2 Correct 520 ms 53600 KB Output is correct
3 Incorrect 320 ms 36600 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 617 ms 46036 KB Output is correct
2 Correct 603 ms 53496 KB Output is correct
3 Incorrect 411 ms 38136 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 608 ms 46072 KB Output is correct
2 Correct 505 ms 53752 KB Output is correct
3 Incorrect 414 ms 38008 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 602 ms 46200 KB Output is correct
2 Correct 589 ms 54136 KB Output is correct
3 Incorrect 286 ms 36604 KB Output isn't correct