zoukankan      html  css  js  c++  java
  • POJ 3417 Network

    传送门

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> p;
    const int maxn(1e5 + 10);
    const int maxm(2e5 + 10);
    int n, m, ecnt, head[maxn];
    int d[maxn], dep[maxn], f[maxn][17];
    
    struct edge {
        int to, nxt;
    } edges[maxm];
    
    template<typename T>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    void addEdge(int u, int v)
    {
        edges[ecnt].to = v;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void bfs(int root)
    {
        dep[root] = 1;
        queue<int> q;
        q.push(root);
        while (not q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = head[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to;
                if (not dep[v]) {
                    q.push(v);
                    dep[v] = dep[u] + 1;
                    f[v][0] = u;
                    for (int j = 1; j < 17; ++j) {
                        f[v][j] = f[f[v][j - 1]][j - 1];
                    }
                }
            }
        }
    }
    
    int lca(int u, int v)
    {
        if (dep[u] < dep[v]) {
            swap(u, v);
        }
        for (int i = 16; i >= 0; --i) {
            if (dep[f[u][i]] >= dep[v]) {
                u = f[u][i];
            }
        }
        if (u == v) {
            return u;
        }
        for (int i = 16; i >= 0; --i) {
            if (f[u][i] not_eq f[v][i]) {
                u = f[u][i];
                v = f[v][i];
            }
        }
        return f[u][0];
    }
    
    int dfs(int cur, int pre, int& sum)
    {
        int res = d[cur];
        for (int i = head[cur]; compl i; i = edges[i].nxt) {
            int nxt = edges[i].to;
            if (nxt not_eq pre) {
                int val = dfs(nxt, cur, sum);
                res += val;
                if (not val) {
                    sum += m;
                } else if (val == 1) {
                    sum += 1;
                }
            }
        }
        return res;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        n = read<int>();
        m = read<int>();
        memset(head, -1, sizeof head);
        for (int i = 0; i < n - 1; ++i) {
            int u = read<int>(), v = read<int>();
            addEdge(u, v);
            addEdge(v, u);
        }
        bfs(1);
        for (int i = 0; i < m; ++i) {
            int u = read<int>(), v = read<int>();
            d[u] += 1;
            d[v] += 1;
            d[lca(u, v)] -= 2;
        }
        int sum = 0;
        dfs(1, 0, sum);
        write(sum, true);
        return 0;
    }
    
  • 相关阅读:
    函数
    python操作文件
    POJ-2689-Prime Distance(素数区间筛法)
    POJ-2891-Strange Way to Express Integers(线性同余方程组)
    POJ-2142-The Balance
    POJ-1061-青蛙的约会(扩展欧几里得)
    Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing
    Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
    Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
    Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13887728.html
Copyright © 2011-2022 走看看