zoukankan      html  css  js  c++  java
  • uva 10054 The Necklace(欧拉通路)

    提交了7次,总算AC了。题目不难,就是判断下欧拉通路。注意细节。

    /*
    Status:AC
    Title :The Necklace
    */
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    typedef long long ll;
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int MAXN=1010;
    
    int g[MAXN][MAXN];
    int h[MAXN];//记录每个点出现的次数
    bool vis[MAXN];
    
    int n,num;
    
    
    void euler(int u){
    	for(int v=1;v<=50;v++)if(g[u][v]>0){
    		g[u][v]--;
    		g[v][u]--;
    		euler(v);
    		printf("%d %d
    ",v,u );
    	}
    }
    void dfs(int u){
    	num++;
    	vis[u]=true;
    	for(int v=1;v<=50;v++)if(!vis[v] && g[u][v]>0){
    		
    		dfs(v);
    	}
    }
    
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	for(int cas=1;cas<=t;cas++){
    		printf("Case #%d
    ",cas );
    
    		memset(g,0,sizeof g);
    		memset(h,0,sizeof h);
    		memset(vis,0,sizeof vis);
    		while(!s.empty())s.pop();
    		num=0;
    		int first=0;
    
    		scanf("%d",&n);
    		for(int i=0;i<n;i++){
    			int u,v;
    			scanf("%d %d",&u,&v);
    
    			if(first==0)first=u;
    
    			g[u][v]++;
    			g[v][u]++;
    			h[u]++;
    			h[v]++;
    		}
    
    		bool ok=true;
    		int cnt=0;
    
    		for(int i=1;i<=50;i++){
    			if(h[i])cnt++;
    			if(h[i]%2!=0){ //如果存在奇点
    				ok=false;
    				break;
    			}
    		}
    
    		if(!ok){
    			printf("some beads may be lost
    ");
    		}
    		else{
    			dfs(first);
    
    			if(num==cnt){
    				euler(first);
    			}else{
    				printf("some beads may be lost
    ");
    			}
    		}
    		if(cas<t)printf("
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    教你当主管:如何降低你的员工流失率?
    你问我这算什么
    推荐:职场提升的10条捷径
    压力从何而来呢?千万不要2008年结婚
    怎样“管理”你的上司?
    HTTP.sys
    IIS书籍
    IIS目录
    HTTP服务
    在 IIS 7 中使用配置文件
  • 原文地址:https://www.cnblogs.com/bruce27/p/4737096.html
Copyright © 2011-2022 走看看