zoukankan      html  css  js  c++  java
  • ACM: FZU 2112 Tickets

     FZU 2112 Tickets
    Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

    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
    2
    0

    /*/
    题意:
    有N个城市,有M张免费的票,票上写了两个城市,每一张票可以任意在两个城市间移动,每张票只能用一次。现在可以买一些票去使用,问怎样购买最少的票将M张免费票用完,可以在任意起点和终点。
    一开始秒想到欧拉回路,再加上并查集,但是因为一个小地方,改搓了,一直拖了2个小时中间还A了2题才过这个题目。
    将所有的票连起来后,直接查找奇数入度的点,如果奇数入度的点超过2个,把奇数入度的点两两相连剩下两个,作为起点和终点。然后判断并查集,看下有多少个点是走过了,并且是根节点。

    加一下就行了。要注意的是题目可能有从同一个 出发回到同一个点,即n==1&&m==1的情况。。。
    AC代码: /
    */

    #include"algorithm"
    #include"iostream"
    #include"cstring"
    #include"cstdlib"
    #include"cstdio"
    #include"string"
    #include"vector"
    #include"stack"
    #include"queue"
    #include"cmath"
    #include"map"
    using namespace std;
    typedef long long LL ;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define FK(x) cout<<"["<<x<<"]
    "
    #define memset(x,y) memset(x,y,sizeof(x))
    #define memcpy(x,y) memcpy(x,y,sizeof(x))
    #define bigfor(T)  for(int qq=1;qq<= T ;qq++)
    
    const int MX = 1111111;
    int n,m;
    bool tem[MX];
    int p[MX],indegree[MX];
    
    int find(int x) {
    	return p[x]==x?x:(p[x]=find(p[x]));
    }
    void union_town(int u,int v) {
    	int rt1=find(v);
    	int rt2=find(u);
    	if(rt1!=rt2) {
    		p[rt2]=rt1;
    		tem[rt1]=1;
    	}
    }
    void add(int u,int v) {
    	indegree[v]++;
    	indegree[u]++;
    	union_town(u,v);
    }
    
    
    void init() {
    	for(int i=0; i<=n; i++) p[i]=i;
    	memset(indegree,0);
    	memset(tem,0);
    }
    
    
    int main() {
    	int T;
    	scanf("%d",&T);
    	bigfor(T) {
    		scanf("%d%d",&n,&m);
    		init();
    		int u,v;
    		for(int i=0; i<m; i++) {
    			scanf("%d%d",&u,&v);
    			add(u,v);
    		}
    		if(m<=1||n==1) {
    			puts("0");
    			continue;
    		}
    		int ans=0;
    		int town=0;
    		int kuai=0;
    		int c[10];
    		for(int i=1; i<=n; i++) {
    			if(indegree[i]&1) {
    				c[++town]=i;
    //				FK(town);
    //				FK("i=="<<i);
    			}
    			if(town==4) {
    				ans++;
    //				FK("p[c[1]]=="<<p[c[1]]<<"p[c[3]]=="<<p[c[3]]);
    				if(p[c[2]]==p[c[4]]) {
    					union_town(c[3],c[4]);
    				} else {
    					union_town(c[2],c[4]);
    					c[2]=c[3];
    				}
    				town-=2;
    			}
    		}
    		for(int i=1; i<=n; i++)if(p[i]==i&&tem[i]) kuai++;
    		ans+=kuai-1;
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
    
    /*/
    
    100
    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
    
    5 5
    1 2
    2 3
    4 5
    5 5
    4 4
    
    5 5
    1 1
    2 2
    3 3
    4 4
    5 5
    
    7 7
    1 7
    2 7
    3 7
    4 7
    5 7
    6 7
    1 4
    
    
    
    
    /*/
    
    
    

      

     
  • 相关阅读:
    springMVC整合Mybatis
    关于GenericJDBCException的问题
    【加载外部文件】
    【父元素parent】【子元素children】【同胞siblings】【过滤】
    【提取元素的值 】【追加文本append】【删除文本remove】【class的操作】【读取元素的宽度,高度】
    【jquery隐藏、显示事件and提示callback】【淡入淡出fadeToggle】【滑入滑出slideToggle】【动画animate】【停止动画stop】
    【如何使用jQuery】【jQuery弹出框】【jQuery对div进行操作】【jQuery对class,id,type的操作】【jquery选择器】
    【提示框】【计时事件】【cookie】
    【节点-添加标签】【节点-删除标签】【显示年-月-日-星期】【math算数】【正则表达式】
    【try..catch..】【判断输入是否为空】【onchange事件】【onmouseover和onmouseout事件】【onmousedown和onmouseup事件】
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5789493.html
Copyright © 2011-2022 走看看