程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 建圖 FZU 2112

建圖 FZU 2112

編輯:C++入門知識

建圖 FZU 2112


Description

You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.

You love to sail and don't want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?

Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.

Input

There is one integer T (T≤100) in the first line of the input.

Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.

Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.

Output

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

Sample Input

35 31 31 24 56 51 31 21 61 51 43 21 21 2

Sample Output

120 建圖,看有幾個圖塊,然後每個圖塊單獨考慮
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define MAXN 100000 + 1000
int vis[MAXN],jiou[MAXN][2],head[MAXN],du[MAXN];
int n,m;
int tol;

struct Edge{
    int u,v,next;
}edge[MAXN*2];

void addedge(int u,int v){//建圖
    edge[tol].u = u;
    edge[tol].v = v;
    edge[tol].next = head[u];
    head[u]=tol++;
    edge[tol].u = v;
    edge[tol].v = u;
    edge[tol].next = head[v];
    head[v] = tol++;
}

void DFS(int u,int pre,int num){
    vis[u] = 1;
    if(du[u]%2 == 0){
        jiou[num][0]++;
    }
    else{
        jiou[num][1]++;
    }
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v = edge[i].v;
        if(vis[v]==1 || v==pre){
            continue;
        }
        DFS(v,u,num);
    }
}

int main(){
    int T;
    int a,b;

    while(~scanf("%d",&T)){
        while(T--){
            scanf("%d%d",&n,&m);
            memset(head,-1,sizeof(head));
            memset(vis,0,sizeof(vis));
            memset(jiou,0,sizeof(jiou));
            memset(du,0,sizeof(du));
            tol = 0;
            for(int i=0;i 2){
                    sum+=(jiou[i][1]-2)/2;
                }
            }
            printf("%d\n",sum);
        }
    }

    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved