zoukankan      html  css  js  c++  java
  • Codeforces Round #484 (Div. 2) C. Cut 'em all!

    Example 1
    input
    4
    2 4
    4 1
    3 1
    output
    1
    
    Example 2
    input
    3
    1 2
    1 3
    output
    -1
    
    Example 3
    input
    10
    7 1
    8 4
    8 10
    4 7
    6 5
    9 3
    3 5
    2 10
    2 5
    output
    4
    
    
    Example 4
    input
    2
    1 2
    output
    0
    

      

    题目大意:

    n个顶点,n-1一条边,将其中的边删除,然遗留下的边都包含偶数个点,求最多可以删多少条边

    分析:

    深搜,只有某一个点的儿子为奇数,ans++

    code:

    #define debug
    #include<bits/stdc++.h>
    #define pb push_back
    #define dbg(x) cout<<#x<<" = "<<(x)<<endl;
    #define lson l,m,rt<<1
    #define cmm(x) cout<<"("<<(x)<<")";
    #define rson m+1,r,rt<<1|1
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    typedef pair<ll,ll>PLL;
    typedef pair<int,ll>Pil;
    const ll INF = 0x3f3f3f3f;
    const ll inf=0x7fffffff;
    const double eps=1e-8;
    const int maxn =1e6+10;
    const int N = 510;
    const ll mod=1e9+7;
    const ll MOD=1e9;
    //------
    //define
    vector<int>G[maxn];
    int subtree[maxn];
    int vis[maxn];
    int ans;
    //dfs
    void dfs(int u){
    	vis[u]=1;
    	for(int i=0;i<G[u].size();i++){
    		if(vis[G[u][i]])continue;
    		dfs(G[u][i]);
    		subtree[u]+=subtree[G[u][i]];
    		if(subtree[G[u][i]]%2==0){
    			ans++;
    		}
    	}
    	subtree[u]+=1;
    }
    //solve
    void solve() {
    	int n;
    	while(cin>>n){
    		for(int i=0;i<n+2;i++){
    			G[i].clear();
    		}
    		for(int i=0;i<n-1;i++){
    			int u,v;
    			cin>>u>>v;
    			G[v].push_back(u);
    			G[u].push_back(v);
    		}
    		if(n%2){
    			cout<<-1<<endl;
    			continue;
    		}
    		ans=0;
    		dfs(1);
    		cout<<ans<<endl;
    		memset(vis,0,sizeof(vis));
    		memset(subtree,0,sizeof(subtree));
    	}
    }
    
    int main() {
    	ios_base::sync_with_stdio(false);
    #ifdef debug
    	freopen("in.txt", "r", stdin);
    //	freopen("out.txt","w",stdout);
    #endif
    	cin.tie(0);
    	cout.tie(0);
    	solve();
    	/*
    		#ifdef debug
    			fclose(stdin);
    			fclose(stdout);
    			system("out.txt");
    		#endif
    	*/
    	return 0;
    }
    

      

  • 相关阅读:
    POJ 1129 深搜&四色染图&模拟
    POJ 1011 很经典的树枝拼凑的深度搜索
    HDU 2564 词组缩写
    Java中的split函数的用法
    java中next和nextline的区别
    简探this和super
    再探Java中的继承加载顺序
    Java面向接口编程小例子 2
    Dos命令整理集(持续更新)
    VMware WorkStation9.0虚拟机如何运行WINPE
  • 原文地址:https://www.cnblogs.com/visualVK/p/9083940.html
Copyright © 2011-2022 走看看