답안 #235488

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
235488 2020-05-28T11:09:22 Z VEGAnn Usmjeri (COCI17_usmjeri) C++14
56 / 140
732 ms 70008 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];
int V[N], NM[N], U[N], LC[N], fen[2][N], h[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;

        h[u] = h[v] + 1;
        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]);
}

bool cmp(int _x, int _y){
    return h[LC[_x]] < h[LC[_y]];
}

int sum(int tp, int ps){
    int res = 0;

    for (; ps >= 0; ps = (ps & (ps + 1)) - 1)
        res += fen[tp][ps];

    return res;
}

void update(int tp, int ps, int vl){
    for (; ps < N; ps = (ps | (ps + 1)))
        fen[tp][ps] += vl;
}

void BAD(){
    cout << 0;
    exit(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 (int i = 0; i < m; i++){
        int x, y; cin >> x >> y;
        x--; y--;

        int lc = lca(x, y);

        LC[i] = lc;
        NM[i] = i;

        if (x != lc)
            swap(x, y);

        U[i] = x; V[i] = y;

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

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

    DFS(0);

    sort(NM, NM + m, cmp);

    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;
    }

    for (int it = 0; it < m; it++){
        int i = NM[it];

        if (U[i] == LC[i]){
            int sf = sum(0, tin[V[i]]) - sum(0, tin[LC[i]]);
            int ss = sum(1, tin[V[i]]) - sum(1, tin[LC[i]]);

            if (sf > 0 && ss > 0)
                BAD();

            if (ss == 0){
                update(0, tin[V[i]], 1);
                update(0, tout[V[i]] + 1, -1);
            } else {
                update(1, tin[V[i]], 1);
                update(1, tout[V[i]] + 1, -1);
            }
        } else {
            int ff = sum(0, tin[U[i]]) - sum(0, tin[LC[i]]);
            int fs = sum(1, tin[U[i]]) - sum(1, tin[LC[i]]);

            int sf = sum(0, tin[V[i]]) - sum(0, tin[LC[i]]);
            int ss = sum(1, tin[V[i]]) - sum(1, tin[LC[i]]);

            if (ff > 0 && fs > 0)
                BAD();

            if (sf > 0 && ss > 0)
                BAD();

            if (sf > 0 && ff > 0)
                BAD();

            if (ss > 0 && fs > 0)
                BAD();

            if (ff == 0 && sf == 0){
                if (ss > 0) {
                    update(1, tin[V[i]], 1);
                    update(1, tout[V[i]] + 1, -1);

                    update(0, tin[U[i]], 1);
                    update(0, tout[U[i]] + 1, -1);
                } else {
                    update(0, tin[V[i]], 1);
                    update(0, tout[V[i]] + 1, -1);

                    update(1, tin[U[i]], 1);
                    update(1, tout[U[i]] + 1, -1);
                }
            } else {
                if (ff == 0) {
                    update(0, tin[V[i]], 1);
                    update(0, tout[V[i]] + 1, -1);

                    update(1, tin[U[i]], 1);
                    update(1, tout[U[i]] + 1, -1);
                } else {
                    update(1, tin[V[i]], 1);
                    update(1, tout[V[i]] + 1, -1);

                    update(0, tin[U[i]], 1);
                    update(0, tout[U[i]] + 1, -1);
                }
            }
        }
    }

    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 210 ms 30072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 373 ms 70008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 7680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 7680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 8320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 8320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 586 ms 51860 KB Output is correct
2 Correct 627 ms 54412 KB Output is correct
3 Correct 353 ms 37624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 732 ms 52192 KB Output is correct
2 Correct 698 ms 53880 KB Output is correct
3 Incorrect 482 ms 39288 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 709 ms 52384 KB Output is correct
2 Correct 575 ms 54268 KB Output is correct
3 Incorrect 482 ms 39288 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 728 ms 52448 KB Output is correct
2 Correct 722 ms 54264 KB Output is correct
3 Correct 296 ms 37112 KB Output is correct