hdu 3018 并查集+顶点度数判定

2014-11-24 11:21:37 · 作者: · 浏览: 0

Ant Trip

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1329 Accepted Submission(s): 514


Problem Description Ant Country consist of N towns.There are M roads connecting the towns.

Ant Tony,together with his friends,wants to go through every part of the country.

They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.
\

Input Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
Output FZ http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciBlYWNoIHRlc3QgY2FzZSAsb3V0cHV0IHRoZSBsZWFzdCBncm91cHMgdGhhdCBuZWVkcyB0byBmb3JtIHRvIGFjaGlldmUgdGhlaXIgZ29hbC4KCiAKPGJyPgpTYW1wbGUgSW5wdXQKCjxwcmUgY2xhc3M9"brush:java;">3 3 1 2 2 3 1 3 4 2 1 2 3 4
Sample Output
1
2


一个连通图不会有奇数个奇数度顶点,画一画图可以知道,假如一个连通图所有点度数为偶数,答案为1,否则就是奇数度顶点的一半,累加求和。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/26 20:53:22
File Name :7.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
                using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; const int maxn=100100; int du[maxn],fa[maxn],ss[maxn]; int find(int x){ if(fa[x]!=x)fa[x]=find(fa[x]); return fa[x]; } void bin(int u,int v){ int t1=find(u),t2=find(v); if(t1!=t2)fa[t1]=t2; } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,m; while(~scanf("%d%d",&n,&m)){ memset(du,0,sizeof(du)); memset(ss,-1,sizeof(ss)); for(int i=1;i<=n;i++)fa[i]=i; while(m--){ int u,v; scanf("%d%d",&u,&v); du[u]++;du[v]++; bin(u,v); } for(int i=1;i<=n;i++){ int t=find(i); if(ss[t]==-1&&du[i]>0)ss[t]=0; if(du[i]&1)ss[t]++; } int ans=0; for(int i=1;i<=n;i++) if(ss[i]>0)ans+=ss[i]/2; else if(ss[i]==0)ans++; cout<