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
Sample Input
3 5 3 1 3 1 2 4 5 6 5 1 3 1 2 1 6 1 5 1 4 3 2 1 2 1 2
Sample Output
1 20
有N个城市,有M张免费的票,票上写了两个城市,每一张票可以任意在两个城市间移动,每张票只能用一次。 现在可以买一些票去使用,问怎样购买最少的票将M张免费票用完,可以在任意起点和终点。 所以就是判断欧拉回路,但题中说“你的奖给你免费的机票去任何城市启动巡航,并免费机票回家到你完成巡航。”, 所以不用考虑最后怎么从终点回到起点,就少了一条边
#include<stdio.h> #include<string.h> int v[100010]; int main() { int n,m,t,a,b; scanf("%d",&t); while(t--) { int sum=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) v[i]=0; for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); v[a]++; v[b]++; } for(int i=1;i<=n;i++) sum+=v[i]%2; sum=(sum-2)/2; if(sum>0) printf("%d\n",sum); else printf("0\n"); } return 0; }