• Codeforces 982C(dfs+思维)


    C. Cut 'em all!
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You're given a tree with nn vertices.

    Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

    Input

    The first line contains an integer nn (1n1051≤n≤105) denoting the size of the tree.

    The next n1n−1 lines contain two integers uu, vv (1u,vn1≤u,v≤n) each, describing the vertices connected by the ii-th edge.

    It's guaranteed that the given edges form a tree.

    Output

    Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or 1−1 if it is impossible to remove edges in order to satisfy this property.

    Examples
    input
    Copy
    4
    2 4
    4 1
    3 1
    output
    Copy
    1
    input
    Copy
    3
    1 2
    1 3
    output
    Copy
    -1
    input
    Copy
    10
    7 1
    8 4
    8 10
    4 7
    6 5
    9 3
    3 5
    2 10
    2 5
    output
    Copy
    4
    input
    Copy
    2
    1 2
    output
    Copy
    0
    Note

    In the first example you can remove the edge between vertices 11 and 44. The graph after that will have two connected components with two vertices in each.

    In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is 1

    二、大致题意

        给出n个点,有n-1条边让他们相连。

        询问最多删除多少边,让他们成为偶数的块

    三、思路

        DFS,只要能找到一个偶数的块,就使答案++,因为是偶数块的话,就可以在这里断开一次

    (其实感觉一点都不裸,太菜,没想到dfs)

    #include <iostream>
    #include<cstring>
    #include<string>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<deque>
    #include<vector>
    #define ll unsigned long long
    #define inf 0x3f3f3f3f
    using namespace std;
    bool used[100005];
    int n;
    vector<int>v[100005];
    int ans=0;
    int dfs(int fa)
    {
        used[fa]=1;
        int s=0;
        for(int i=0;i<v[fa].size ();i++)
        {
            if(used[v[fa][i]]) continue;
            s=s+dfs(v[fa][i]);
        }
        if((s+1)%2==0) ans++;//发现是偶数块,就可以减一刀
        return s+1;
    }
    int main()
    {
        int n;
        cin>>n;
        ans=0;
        memset(used,0,sizeof(used));
        for(int i=1;i<=n-1;i++)
        {
            int x,y;
            cin>>x>>y;
            v[x].push_back (y);
            v[y].push_back (x);
        }
        if(n&1) cout<<"-1";
        else
        {
            used[1]=1;
            int s=dfs(1);
            cout<<ans-1;//原本自己就是偶数,所以要减1
        }
        return 0;
    }
  • 相关阅读:
    《SPFA算法的优化及应用》——姜碧野(学习笔记)
    hdu 4691 Front compression
    hdu 4690 EBCDIC
    UVA 11478 Halum(用bellman-ford解差分约束)
    UVA 11090 Going in Cycle!!(二分答案+判负环)
    UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
    hdu 4674 Trip Advisor(缩点+倍增lca)
    canny算子原理
    二值图像连通区域标记
    C++全局变量的声明和定义
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9085213.html
Copyright © 2020-2023  润新知