답안 #239673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
239673 2020-06-17T07:09:35 Z NONAME Usmjeri (COCI17_usmjeri) C++14
140 / 140
509 ms 79992 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;

typedef long long ll;
typedef long double ld;
const int N = 3e5 + 10;
const int base = 1e9 + 7;

int n, m, timer, tin[N], tout[N], depth[N], p[N], pr[N], up[N][25], col[N];
vector <pair <int, int> > e;
vector <int> g[N];
bool mk[N];

void dfs(int v, int pred = 0) {
    tin[v] = timer++;

    pr[v] = pred;
    up[v][0] = pred;

    for (int i = 1; i < 20; ++i)
        up[v][i] = up[up[v][i - 1]][i - 1];

    for (int to : g[v])
        if (to != pred) {
            depth[to] = depth[v] + 1;
            dfs(to, v);
        }

    tout[v] = timer++;
}

void paint(int v, int cl) {
    if (mk[v]) {
        if (col[v] != cl) {
            cout << 0;
            exit(0);
        }
        return;
    }

    mk[v] = 1;
    col[v] = cl;

    for (int to : g[v])
        paint(to, cl ^ 1);
}

int f(int x) { return (x == p[x]) ? x : p[x] = f(p[x]); }

void unite(int x, int y) { p[f(x)] = f(y); }

bool upper(int v, int u) { return (tin[v] < tin[u]) && (tout[v] > tout[u]); }

int _lca(int v, int u) {
    if (upper(v, u))
        return v;

    if (upper(u, v))
        return u;

    for (int i = 19; i >= 0; --i)
        if (!upper(up[v][i], u))
            v = up[v][i];

    return up[v][0];
}

void find(int v) {
    if (!v)
        return;

    if (f(v) == f(pr[v])) {
        find(pr[v]);
        pr[v] = pr[pr[v]];
    }
}

void path(int u, int v) {
    find(v);
    while (depth[u] < depth[pr[v]]) {
        unite(v, pr[v]);
        find(v = pr[v]);
    }
}

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

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

        x--; y--;

        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(0);

    for (int i = 0; i < n; ++i)
        g[i].clear(), p[i] = i;

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

        if (x == y)
            continue;

        if (tin[x] > tin[y])
            swap(x, y);

        if (tout[x] > tout[y]) path(x, y);
            else {
                int lca = _lca(x, y);
                path(lca, x);
                path(lca, y);

                e.push_back(make_pair(x, y));
            }
    }

    for (auto a : e) {
        int x = f(a.first);
        int y = f(a.second);
        if (x == y) {
            cout << 0;
            return 0;
        }

        g[x].push_back(y);
        g[y].push_back(x);
    }

    for (int i = 1; i < n; ++i)
        if (!mk[i])
            paint(i, 0);

    for (auto a : e)
        unite(a.first, a.second);

    set <int> s;
    s.clear();
    for (int i = 1; i < n; ++i)
        s.insert(f(i));

    int ans = 1;
    for (int i = 0; i < int(s.size()); ++i)
        (ans <<= 1) %= base;

    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 30712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 79992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 10 ms 7808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 10 ms 7808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 8320 KB Output is correct
2 Correct 12 ms 8192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 8320 KB Output is correct
2 Correct 11 ms 8192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 423 ms 67944 KB Output is correct
2 Correct 421 ms 73324 KB Output is correct
3 Correct 209 ms 42100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 474 ms 71068 KB Output is correct
2 Correct 509 ms 79172 KB Output is correct
3 Correct 257 ms 44384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 485 ms 71516 KB Output is correct
2 Correct 466 ms 73712 KB Output is correct
3 Correct 289 ms 44136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 469 ms 71516 KB Output is correct
2 Correct 474 ms 79836 KB Output is correct
3 Correct 218 ms 41080 KB Output is correct